Linda
Linda

Reputation: 251

LL1 and unambiguous grammar for dangling else

I am trying to write a simple compiler using Flex for scanner and a special tool named PGen to define grammars.

Now, I am trying to solve unambiguous grammar for dangling else. I have searched for this.

This is a grammar with dangling else problem:

S → if E then S
  | if E then S else S
  | OTHER

This is a grammar that solves this problem (via slides from Berkeley):

S → MIF            /* all then are matched */
  | UIF            /* some then are unmatched */
MIF → if E then MIF else MIF
    | OTHER
UIF → if E then S
    | if E then MIF else UIF

But this grammar is still not LL1 and is still ambiguous.

When I tried to solve this ambiguity and make it LL1, I faced the dangling else problem again.

Can anyone please help me find an unambiguous LL1 grammar for nested if?

I have read in a Q&A on Stack Overflow that this grammar can't become LL1. But I can't realize how I can solve this ambiguity.

Upvotes: 2

Views: 1181

Answers (0)

Related Questions