Saturday, 31 August 2013

Arithmetic expression grammar in prefix notation (Java Cup)

Arithmetic expression grammar in prefix notation (Java Cup)

I'm writting a grammar for arithmetic expression in prefix notation.
However I have an issue when parsing negative numbers or substraction.
Grammar example is this:
precedence right +, -;
precedence right *, /;
precedence right uminus;
E ::= + E E
| - E E
| * E E
| / E E
| ( E )
| - E %prec uminus
| id
| digit
;
But if my input is - 5 4, it reduces 5 as E, next it reduces - E
(negative) and then parser gives me a syntax error at 4. The correct one
should be 5 as E, next 4 as E and then - E E as E. How can I solve this
problem using associativity? or do I need to rewrite my grammar?

No comments:

Post a Comment