0% found this document useful (0 votes)
62 views5 pages

Handling Ambiguous Grammars with LR Parsers

This document discusses advantages of using ambiguous grammars and creating LR parsers for ambiguous grammars. It provides an example of an ambiguous grammar for expressions and describes steps to construct an LR(0) parser for it. Conflicts that arise in the LR parsing table due to ambiguity are resolved by applying precedence and associativity rules.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views5 pages

Handling Ambiguous Grammars with LR Parsers

This document discusses advantages of using ambiguous grammars and creating LR parsers for ambiguous grammars. It provides an example of an ambiguous grammar for expressions and describes steps to construct an LR(0) parser for it. Conflicts that arise in the LR parsing table due to ambiguity are resolved by applying precedence and associativity rules.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Advantages of using Ambiguous Grammars:

 Ambiguous grammars provide a shorter and more natural specification for


certain constructs when compared to the equivalent unambiguous grammars

 One can isolate a construct for optimization purposes using ambiguous


grammars

 One can incorporate a special construct by adding new productions to an existing


ambiguous grammar

Ambiguous grammar can be handled by bottom up parser.

Creating LR Parsers for Ambiguous Grammars:

 No ambiguous grammar can be LR. But by carefully resolving conflicts an LR


parser for an ambiguous grammar can be designed

 Resolving conflicts can be done by either one of the two following methods

Method 1: By using associativity and precedence in case of expression


Method 2: By keeping in view the correct sentences in the language
Method 1:

 Consider the following ambiguous grammar for expressions

                                E → E + E | E * E | ( E ) | id
                We know that ‘+’ and ‘*’ are left associative and ‘*’ is having precedence over ‘+’

 The unambiguous version of the equivalent grammar is

                                E → E + T | T   T → T * F | F   F → ( E ) | id


Reasons to use Ambiguous version:

 We can change operator precedence

 Parser will have less number of states compared to unambiguous version

 Parser never wastes time in performing reductions like E → T or T → F

Steps for Creating LR parser

 Step 1: Construct LR(0) items.

 Step 2: Construct LR parsing table with the help of step 1

 Step 3: Parse an input string with the help of step 2


Steps for Constructing LR(0) Items

 Step 1: Add augmented grammar.

 Step 2: Apply closure and goto operations

Method 1:
E’ → E   E → E + E   E → E * E   E → ( E )  E → id
and  the LR(0) items are:
I0:
E’→.E
E→.E+E I5:
E→.E*E E→E*.E
E→.id E→.E+E
I1: E→.E*E
E’→E. E→.(E)
E→E.+E E→.id
E→E.*E I6:
I2: E→(E.)
E’→.E E→E.+E
E→.E+E E→E.*E
E→.E*E I7:
E→.(E) E→E+E.
E→.id E→E.+E
I3: E→E.*E
E→.id I8:
I4: E→E*E.
E→E+.E E→E.+E
E→.E+E E→E.*E
E→.E*E I9:
E→.(E) E→(E).
E→id
 Creating LR parser for Ambiguous Grammar:

Method 1:

 While designing SLR parser table using it’s algorithm, conflicts arise as the
grammar is ambiguous

 The states involved in conflicts are


                I7: reduction by E→E+E and shift on ‘+’ and ‘*’
                I8: reduction by E→E*E and shift on ‘+’ and ‘*’

Resolving Conflicts with I7 on Top:


Let the input is id+id*id$. Then after consuming input upto ‘*’ the stack content will be
                                O E1 + 4 E7
Now on ‘*’ shift must happen as ‘*’ is having precedence over ‘+’
Let the input be id+id+id$. After consuming input upto second ‘+’, the stack content
must be
                                0E1 + 4E7
Now ‘+’ reduction must happen as + is left associative
So, I7 must reduce on’+’ and must shift on ‘*’.

Resolving Conflicts with I8 on Top:


Let the input is id+id*id$. After consuming input upto ‘+’ the stack content is
                                O E1 + 5 E8
Now on ‘+’ reduction must happen as ‘*’ is having precedence over ‘+’
Let the input be id*id*id$. After consuming input upto second ‘*’, the stack content will
be
                                0E1 + 5E8
Now on ‘*’ reduction must happen as ‘*’ is left associative. 
So, I8 must reduce on’+’ and must shift on ‘*’.

After resolving conflicts, the SLR parsing table is as shown in the table.

Common questions

Powered by AI

Left associativity and precedence dictate the order of operations during parsing, affecting whether a shift or reduction occurs when constructing LR parsing tables. These concepts ensure that operators like '+' and '*' are processed correctly, with '+' being left associative and '*' having higher precedence, leading to shifts for '*' over '+'. This tailored resolution is crucial for deciding conflicts in states like I7 and I8, ensuring operator precedence is respected in expression evaluation .

To parse 'id+id*id' with an SLR parser for an ambiguous grammar, recognize '*''s precedence over '+'. After processing 'id+', the stack would require shifting on '*', following 'id*'. Post processing '*', reduction of 'id+id' happens because '+' has a left associative rule, finalizing the SLR parser's correct parse sequence .

Resolving conflicts using associativity and precedence is crucial as it ensures the correct evaluation order for operators in expressions. For instance, '*' having precedence over '+' and both being left associative defines the evaluation hierarchy, avoiding incorrect reductions or shifts that could result in syntactically valid but semantically incorrect interpretation of expressions .

Conflicts in LR parsers for ambiguous grammars can be resolved using associativity and precedence rules or by considering the correct sentences in the language. These methods rely on understanding operator characteristics, such as left associativity of '+' and precedence of '*' over '+', to decide whether to shift or reduce in states like I7 and I8 .

Conflict resolution in an SLR parser for ambiguous grammars relies on detailed analysis of operator precedence and associativity. It distinguishes itself by resolving shift-reduce and reduce-reduce conflicts through an understanding of these characteristics, ensuring the parser evaluates expressions in a language-appropriate order, as demonstrated when managing conflicts in states I7 and I8 .

Ambiguous grammars provide a shorter and more natural specification for certain constructs compared to equivalent unambiguous grammars. They allow isolation of constructs for optimization purposes and enable incorporating special constructs by adding new productions. Additionally, they allow changing operator precedence and result in parsers with fewer states without performing unnecessary reductions like E → T or T → F .

An ambiguous grammar can't be inherently LR because LR parsers require unambiguous grammar to maintain consistency in state transitions. However, an LR parser can still be designed through careful conflict resolution by employing associativity and precedence rules. This resolution tailors the parser to manage potential shifts and reductions correctly, maintaining operational consistency despite the ambiguous nature .

LR(0) items are fundamental to constructing an LR parser as they help define states in the parsing table. For ambiguous grammars, these items need to be carefully designed to manage ambiguity by defining shift and reduce actions that consider associativity and precedence, facilitating the resolution of conflicts inherent in ambiguous grammars .

Ambiguous grammars affect parser construction by potentially reducing the number of states needed, as they avoid unnecessary derivations like E → T or T → F. This reduction happens because ambiguous grammars allow changes in operator precedence within the grammar, leading to a more compact parser structure compared to unambiguous constructs which strictly adhere to derivation sequences .

An LR parser for an ambiguous grammar can be designed by resolving conflicts using methods such as applying associativity and precedence rules, or constructing the grammar with correct sentences in mind. This involves designing an SLR parser table where conflicts arise and are resolved by determining reduction and shift actions based on the precedence and associativity rules as demonstrated with the states I7 and I8 .

You might also like