0
$\begingroup$

The syntax of dice notation follows the following grammar:

$$ Roll \rightarrow Dice | Dice'x'Int | More \quad Mod \quad More $$ $$ Dice \rightarrow Int_{1}'d'Int_2$$ $$ Mod \rightarrow '+'|'-'$$ $$ More \rightarrow Int | Roll$$ $$ Int \rightarrow <integer>$$

Give a leftmost derivation for the sample string: $3d8 + 2d4x3 + 1d12 + 567$

I was able to parse the two first operands $3d8 + 2d4x3$ but I couldn't find a reasonable derivation for the rest.

My solution is like:

E -> More Mod More
E -> Roll Mod More
E -> Dice Mod More
E -> Int1’d’Int2 Mod More
E -> <integer>’d’Int2 Mod More
...
...
E -> <integer>’d’<integer> + <integer>’d’<integer>’x’<integer> + More Mod More
E -> <integer>’d’<integer> + <integer>’d’<integer>’x’<integer> + Roll Mod More
E -> <integer>’d’<integer> + <integer>’d’<integer>’x’<integer> + Dice Mod More
E -> <integer>’d’<integer> + <integer>’d’<integer>’x’<integer> + Int1’d’Int2 Mod More

I was thinking that the given string is not a valid string for this grammar.

Does any have any idea for this problem. I'm newbie in Compiler Design :-)

$\endgroup$
1
$\begingroup$

Hint: Your string is of the form Dice+Dice+Dice+Int. Figure out how to obtain this from Roll using only the first, third and fourth rule.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you! Actually, in the third and fourth string is 1d12 + 567 12 and 567 is an integer, not three consecutive integers. After that clarity, everything is much easier for me. $\endgroup$ – DucCuong Nov 27 '13 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.