# Parsing ambiguos grammars using LR parser

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 I_{5} and I_{6}. So the parsing table is as follows:

There are both shift and reduce moves in I_{5} and I_{6} 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 I
_{5}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 I
_{5}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 I_{5} state on symbol “*” will give “*” higher precedence over “+”, as “*” will be reduced before “+”. Taking reduce move of I_{5} state on symbol “*” will give “+” higher precedence over “*”, as “+” will be reduced before “*”. Similar to I_{5}, conflicts from I_{6} can also be resolved.

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

- The shift/reduce conflict at I
_{5}on “+” is resolved by keeping the reduce move and discarding the shift move, which makes “+” left associative. - The shift/reduce conflict at I
_{5}on “*” is resolved by keeping the shift move and discarding the reduce move, which will give “*” higher precedence over “+”. - The shift/reduce conflict at I
_{6}on “+” is resolved by keeping the reduce move and discarding the shift move, which will give “*” higher precedence over “+”. - The shift/reduce conflict at I
_{6}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:**

## Recommended Posts:

- Difference between Top down parsing and Bottom up parsing
- Classification of Context Free Grammars
- Difference between LL and LR parser
- Shift Reduce Parser in Compiler
- Parsing | Set 3 (SLR, CLR and LALR Parsers)
- Compiler Design | Recursive Descent Parser
- Parsing | Set 1 (Introduction, Ambiguity and Parsers)
- Theory of Computation | Operator grammar and precedence parser
- Compiler Design | Construction of LL(1) Parsing Table
- Parsing | Set 2 (Bottom Up or Shift Reduce Parsers)
- Static and Dynamic Linking in Operating Systems
- Removing Direct and Indirect Left Recursion in a Grammar
- Total Recursive Functions and Partial Recursive Functions in Automata
- Introduction of Shortest Remaining Time First (SRTF) algorithm

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.