Open In App

Parsing ambiguous grammars using LR parser

Last Updated : 23 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

LR parser can be used to parse ambiguous grammars. LR parser resolves the conflicts (shift/reduce or reduce/reduce) in parsing table of ambiguous grammars based on certain rules (precedence and/or associativity of operators) of the grammar. 

Example: 
Lets take the following ambiguous grammar: 
 

E -> E+E
E -> E*E
E -> id 

Lets assume, the precedence and associativity of the operators (+ and *) of the grammar are as follows: 
 

  • “+” and “*” both are left associative,
  • Precedence of “*” is higher than the precedence of “+”.

If we use LALR(1) parser, the LR(1) item DFA will be: 

 

From the LR(1) item DFA we can see that there are shift/reduce conflicts in the state I5 and I6. So the parsing table is as follows: 

 

There are both shift and reduce moves in I5 and I6 on “+ and “*”. To resolve this conflict, that is to determine which move to keep and which to discard from the table we shall use the precedence and associativity of the operators. 
Consider the input string: 
 

id + id + id 

Lets look at the parser moves till the conflict state according to the above parsing table. 

 

 

  • If we take the reduce move of I5 state on symbol “+” as in parser 1, then the left “+” of the input string is reduced before the right “+”, which makes “+” left associative. 
     
  • If we take the shift move of I5 state on symbol “+” as in parser 2, then the right “+” of the input string is reduced before the left “+”, which makes “+” right associative. 
     

Similarly, Taking shift move of I5 state on symbol “*” will give “*” higher precedence over “+”, as “*” will be reduced before “+”. Taking reduce move of I5 state on symbol “*” will give “+” higher precedence over “*”, as “+” will be reduced before “*”. Similar to I5, conflicts from I6 can also be resolved. 

According to the precedence and associativity of our example, the conflict is resolved as follows, 
 

  • The shift/reduce conflict at I5 on “+” is resolved by keeping the reduce move and discarding the shift move, which makes “+” left associative.
  • The shift/reduce conflict at I5 on “*” is resolved by keeping the shift move and discarding the reduce move, which will give “*” higher precedence over “+”.
  • The shift/reduce conflict at I6 on “+” is resolved by keeping the reduce move and discarding the shift move, which will give “*” higher precedence over “+”.
  • The shift/reduce conflict at I6 on “*” is resolved by keeping the reduce move and discarding the shift move, which makes “*” left associative.

Generally, the parser generator tool YAAC resolves conflicts due to ambiguous grammars as follows, 
 

  • Shift/reduce conflict in the parsing table is resolved by giving priority to shift move over reduce move. If the string is accepted for shift move, then reduce move is removed, otherwise shift move is removed.
  • Reduce/reduce conflict in the parsing table is resolved by giving priority to first reduce move over second reduce move. If the string is accepted for first reduce move, then second reduce move is removed, otherwise first reduce move is removed.

Related GATE questions: 
 

 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads