Question

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

Thanks

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top