Flavius
Flavius

Reputation: 13816

LALR(2) dangling else

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

Thanks

Upvotes: 6

Views: 927

Answers (2)

user1524750
user1524750

Reputation:

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.

Upvotes: 3

Chris Dodd
Chris Dodd

Reputation: 126120

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

Upvotes: 5

Related Questions