Professional Documents
Culture Documents
Laxmi Institute of Technology Scanning and Parsing: Guided BY: Rinkal Prajapati
Laxmi Institute of Technology Scanning and Parsing: Guided BY: Rinkal Prajapati
CSE Department
5th Sem
analysis(string of symbols).
Programming language grammars
Grammar is a collection of four tuples, that are
NT(or V),T,P,S.
NT(or V):A set of symbol are called non-
terminals or variables.
S SS aSbS abS ab
14
Ambiguity in grammatic specification
A string x in L(G) may have two or more parse
tree witness S * x. The grammar G is said to
be ambiguous.
A CFG is called ambiguous if for at least one
E E E E a E a EE
a a E a a*a
E E leftmost derivation
a E E
a a
16
Example:
E E E | E E | (E) | a
a aa
E EE E EE a EE
a aE a aa E
E E
rightmost derivation
E E a
a a
17
E E E | E E | (E) | a
a aa
Two derivation trees
E E
E E E E
a E E E E a
a a a a
18
The grammar E E E | E E | (E) | a
is ambiguous because both parse
trees are not same.
string a aa has two derivation trees
E E
E E E E
a E E E E a
a a a a
19
Solution to remove Ambiguity
Let two grammar G1 and G2
G1 : A aα | aβ (contains ambiguity)
S aB | aA
A aAB | a | b
B Abb | b
Removing Ambiguity From Grammars
:
To remove Ambiguity,grammar required to be
rewrite
Example :
Grammar :S aB | aA
A aAB | a | b
B Abb | b
This grammar is Ambiguous.
So, finally we have grammar with production:
S aS’
S’ B | A
A aA’ | b
A’ AB | Є
B Abb | b