0

I was writing an LALR(1) parser for a simple arithmetic grammar

E -> E ('+' | '-') T
E -> T
T -> T ('*' | '/') F
T -> F
F -> '( E ')'
F -> int

How would I handle operator precedence for an expression such as 1 * 1 + 2 so that the 1+2 is evaluated before the multiplication?

3
  • Do you mean that you want to invert the preecedence? If so, what's wrong with the obvious solution where you just change the operator symbols in that grammar? Commented Nov 12, 2021 at 20:12
  • No like when I implement the parser, how would I enforce the precedence? Commented Nov 13, 2021 at 20:13
  • The precedence is defined by the grammar. But the grammar makes * bind more tightly. Which is the way arithmetic works. 1*1+2 isn't a good example because it has the same result both ways. Commented Nov 13, 2021 at 20:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.