LALR(2) dangling else

893 Views Asked by At

Is LALR(2) able to handle the dangling else case naturally (without any special rules, as with LALR(1))?

Thanks

2

There are 2 best solutions below

1
On BEST ANSWER

It's ambiguous, however, it's not a problem, because a well behaved LALR parser generator will resolve the ambiguity by choosing the shift instead of the reduce choice. This is what you want -- the "else" to be matched with the previous "if" statement. So the conclusion is: there is no problem. You just have to understand the default choice which the parser generator makes for all shift-reduce ambiguities. This default can be overridden with disambiguating rules or something like the "%prec" operator in "yacc".
But that is a more advanced topic.

0
On

No, the dangling else problem is an ambiguity, so no amount of lookahead helps.