0% found this document useful (0 votes)
1K views

Compiler Design Ambiguous Grammar and Making Them Unmbiguous

The document discusses types of grammars and describes ambiguous and unambiguous grammars. It provides examples of ambiguous grammars where multiple parse trees can be drawn for a given string. It also provides examples of unambiguous grammars where only one parse tree can be drawn for each string. The document then discusses how to check if a grammar is ambiguous by trying to find strings that have more than one parse tree. Finally, it provides some examples of checking if given grammars are ambiguous or unambiguous.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Compiler Design Ambiguous Grammar and Making Them Unmbiguous

The document discusses types of grammars and describes ambiguous and unambiguous grammars. It provides examples of ambiguous grammars where multiple parse trees can be drawn for a given string. It also provides examples of unambiguous grammars where only one parse tree can be drawn for each string. The document then discusses how to check if a grammar is ambiguous by trying to find strings that have more than one parse tree. Finally, it provides some examples of checking if given grammars are ambiguous or unambiguous.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

1 1

Types of Grammar
On the basis of number of derivation trees, grammars are classified as-

1.Ambiguous Grammar
2.Unambiguous Grammar
1. Ambiguous Grammar-

A grammar is said to ambiguous if for any string generated by it, it


produces more than one-
• Parse tree
• Or derivation tree
• Or syntax tree
• Or leftmost derivation
• Or rightmost derivation
Example-

Consider the following grammar-


E → E + E | E x E | id
Ambiguous Grammar
 
This grammar is an example of ambiguous grammar.
Any of the following reasons can be stated to prove the grammar
ambiguous-
Reason-01:
Let us consider a string w generated by the grammar-
w = id + id x id
Now, let us draw the parse trees for this string w.
Reason-02:
Let us consider a string w generated by the grammar-
w = id + id x id
Now, let us draw the syntax trees for this string w.

Since two syntax trees exist for string w, therefore the grammar is ambiguous.
Reason-03:
Let us consider a string w generated by the grammar-
w = id + id x id
Now, let us write the leftmost derivations for this string w.

Since two leftmost derivations exist for string w, therefore the grammar is ambiguous.
Reason-04:

Let us consider a string w generated by the grammar-


w = id + id x id
Now, let us write the rightmost derivations for this string w.

Since two rightmost derivations exist for string w, therefore the grammar is ambiguous.
2. Unambiguous Grammar-
A grammar is said to unambiguous if for every string generated by it, it produces exactly one-
• Parse tree
• Or derivation tree
• Or syntax tree
• Or leftmost derivation
• Or rightmost derivation
Example-
Consider the following grammar-
E → E + T / T
T → T x F / F
F → id
Unambiguous Grammar
This grammar is an example of unambiguous grammar.
Grammar Ambiguity-
There exists no algorithm to check whether any given grammar is
ambiguous or not.
• This general decision problem is undecidable-
“Whether a grammar is ambiguous or not?”
• This is because it can be shown that this problem is equivalent to Post
Correspondence Problem.
General Approach To Check Grammar Ambiguity-

To check whether a given grammar is ambiguous or not, we follow the following steps-
Step-01:
We try finding a string from the Language of Grammar such that for the string
there exists more than one-
• parse tree
• or derivation tree
• or syntax tree
• or leftmost derivation
• or rightmost derivation
Step-02:
• If there exists at least one such string, then the grammar is ambiguous otherwise
unambiguous.
PROBLEMS BASED ON CHECKING WHETHER GRAMMAR IS
AMBIGUOUS-

Problem-01:

Check whether the given grammar is ambiguous or not-


S → SS
S → a
S → b
Solution-

Let us consider a string w generated by the given grammar-


w = abba
Now, let us draw parse trees for this string w.

Since two different parse trees exist for string w, therefore the given grammar is ambiguous.
Problem-02:

• Check whether the given grammar is ambiguous or not-


S → A / B
A → aAb / ab
B → abB / ∈
Solution-

Let us consider a string w generated by the given grammar-


w = ab
Now, let us draw parse trees for this string w.

Since two different parse trees exist for string w, therefore the given grammar is ambiguous.
Problem-03:

Check whether the given grammar is ambiguous or not-


S → AB / C
A → aAb / ab
B → cBd / cd
C → aCd / aDd
D → bDc / bc
Solution-

Let us consider a string w generated by the given grammar-


w = aabbccdd
Now, let us draw parse trees for this string w.

Since two different parse trees exist for string w, therefore the given grammar is ambiguous.
Problem-04:

Check whether the given grammar is ambiguous or not-


S → AB / aaB
A → a / Aa
B→b
Solution-

Let us consider a string w generated by the given grammar-


w = aab
Now, let us draw parse trees for this string w.

Since two different parse trees exist for string w, therefore the given grammar is ambiguous.
Problem-05:

Check whether the given grammar is ambiguous or not-


S → a / abSb / aAb
A → bS / aAAb
Solution-
Let us consider a string w generated by the given grammar-
w = abababb
Now, let us draw parse trees for this string w.

Since two different parse trees exist for string w, therefore the given grammar is ambiguous.
Problem-06:

Check whether the given grammar is ambiguous or not-


E→E+T/T
T→TxF/F
F → id
Solution-

• There exists no string belonging to the language of grammar which


has more than one parse tree.
• Since a unique parse tree exists for all the strings, therefore the given
grammar is unambiguous.
Problem-07:

Check whether the given grammar is ambiguous or not-


S → aSbS / bSaS / ∈
Solution-
Let us consider a string w generated by the given grammar-
w = abab
Now, let us draw parse trees for this string w.

Since two different parse trees exist for string w, therefore the given grammar is ambiguous.
Problem-08:

Check whether the given grammar is ambiguous or not-


R → R + R / R . R / R* / a / b
Solution-
Let us consider a string w generated by the given grammar-
w = ab + a
Now, let us draw parse trees for this string w.

Since two different parse trees exist for string w, therefore the given grammar is ambiguous.
Removing Ambiguity | Ambiguous to Unambiguous

Converting Ambiguous Grammar Into Unambiguous Grammar-

• Causes such as left recursion, common prefixes etc makes the


grammar ambiguous.
• The removal of these causes may convert the grammar into
unambiguous grammar.
•  However, it is not always compulsory.
NOTE
It is not always possible to convert an ambiguous grammar into an
unambiguous grammar.
Methods To Remove Ambiguity-
The ambiguity from the grammar may be removed using the following methods-

• By fixing the grammar


• By adding grouping rules
• By using semantics and choosing the parse that makes the most sense
• By adding the precedence rules or other context sensitive parsing rules
Removing Ambiguity By Precedence & Associativity Rules-
An ambiguous grammar may be converted into an unambiguous grammar by implementing-
• Precedence Constraints
• Associativity Constraints
These constraints are implemented using the following rules-
Rule-01:
The precedence constraint is implemented using the following rules-
• The level at which the production is present defines the priority of the operator contained in it.
• The higher the level of the production, the lower the priority of operator.
• The lower the level of the production, the higher the priority of operator.
Rule-02:
The associativity constraint is implemented using the following rules-
• If the operator is left associative, induce left recursion in its production.
• If the operator is right associative, induce right recursion in its production.
PROBLEMS BASED ON CONVERSION INTO UNAMBIGUOUS GRAMMAR-

Problem-01:
Convert the following ambiguous grammar into unambiguous
grammar-
R → R + R / R . R / R* / a / b

where * is kleen closure and . is concatenation.


Solution-
To convert the given grammar into its corresponding unambiguous grammar, we implement the
precedence and associativity constraints.
 
We have-
• Given grammar consists of the following operators-
+ , . , *
• Given grammar consists of the following operands-
a , b
 
The priority order is-
(a , b) > * > . > +
where-
• . operator is left associative
• + operator is left associative
Using the precedence and associativity rules, we write the corresponding
unambiguous grammar as-
 
E → E + T | T
T → T . F | F
F → F* | G
G → a | b
Unambiguous Grammar
 
OR
 
E → E + T | T
T → T . F | F
F → F* | a | b
Unambiguous Grammar
Problem-02:

Convert the following ambiguous grammar into unambiguous grammar-


bexp → bexp or bexp / bexp and bexp / not bexp / T / F
where bexp represents Boolean expression, T represents True and F
represents False.
Solution-

To convert the given grammar into its corresponding unambiguous grammar, we


implement the precedence and associativity constraints. 
We have-
• Given grammar consists of the following operators-
• or , and , not
• Given grammar consists of the following operands-
• T , F 
The priority order is-
• (T , F) > not > and > or
where-
• and operator is left associative
• or operator is left associative
Using the precedence and associativity rules, we write the corresponding
unambiguous grammar as-
 
bexp → bexp or M / M
M → M and N / N
N → not N / G
G → T / F
Unambiguous Grammar
 
OR
 
bexp → bexp or M / M
M → M and N / N
N → not N / T / F
Unambiguous Grammar

You might also like