4 ms·
I'm not sure I get the point about "hacking" around operator precedence. Isn't the normal way to get around this to define the grammar differently? For example:
by lyudmil 16y ago
I'm not sure I get the point about "hacking" around operator precedence. Isn't the normal way to get around this to define the grammar differently? For example:
expression -> factor | factor + expression
factor -> number | term * factor
term -> number | (expression)
number -> [0..9] | [1..9][0..9]+
Is this the hack the author is referring to? Why is it a hack?
- kragen 16y agoThat's one way to hack it. Another way to hack it is to write an ambiguous grammar, but annotate the productions with precedences to tell your LALR parser generator how to resolve the ambiguities. And of course you can do what apenwarr suggests — parse with a grammar that is unambiguous but assigns the wrong precedence, and then rewrite the parse tree once it's built.