| Refresh | Home EGTry.com

ambiguous grammars resolving in LR parsing


example of dangling else

if expr1 then if expr2 then s1 else s2


ambiguous grammar

Statement :=
   if Expression then Statement else Statement |
   if Expression then Statement |
   Other


unambiguous grammar

Statement :=
    IfThenElseOrOther |
    IfThen

IfThenElseOrOther :=
    if Expression then IfThenElseOrOther else IfThenElseOrOther |
    Other

IfThen :=
    if Expression then Statement |
    if Expression then IfThenElseOrOther else IfThen


result

if expr1 then {if expr2 then s1 else s2}