SlideShare a Scribd company logo
3
Most read
5
Most read
6
Most read
Topic: Ambiguous grammar & parse Tree
COURSE TITLE: THEORY OF COMPUTING
COURSE CODE: SE-234
1
Department Of Software Engineering
Ambiguous & Unambiguous Grammar
Depending on Number of Derivation Trees,
CFGs are sub-divided into 2 types:
 Ambiguous Grammars
 Unambiguous Grammars
1/3/2020Ambiguous Grammar & Parse Tree
2
The Context Free Grammars(CFGs) are
classified based on:
 Number of Derivation trees
 Number of strings
What is Ambiguous Grammar?
1/3/2020Ambiguous Grammar & Parse Tree
3
A grammar is said to be Ambiguous if
there exists two or more derivation &
parse tree for a string w.
Figure: 1
s a
s
s
s
a s
aa
a
What is Unambiguous Grammar?
1/3/2020Ambiguous Grammar & Parse Tree
4
A grammar can be unambiguous if the grammar does not contain more than one
leftmost derivation or more than one rightmost derivation or more than one parse
tree for the given input string.
b x
b
x
x
b
1/3/2020Ambiguous Grammar & Parse Tree
5
G = (V,T,P,S) is a CFG is said to be ambiguous if and only if
there exist a string in T* that has more than one parse tree.
where,
• V = finite set of variables.
• T = finite set of terminals.
• P = finite set of productions of the form, A → α, where A is
a variable and α ∈ (V ∪ T)*
• S = designated variable called the start symbol.
Definition of ambiguous & unambiguous
grammar:
1/3/2020Ambiguous Grammar & Parse Tree
6Example - 1
Check the grammar G with production rules −
X → X+X | X*X |X| a
is ambiguous or not?
Let’s find out the derivation tree for the string "a+a*a". It has two leftmost
derivations.
Derivation – 1
X → X*X
→ X+X*X [X→X+X]
→ a+X*X [X →X*X]
→ a+a*X [X→a]
→ a+a*a
Derivation – 2
X → X+X
→ a+X [X→a]
→ a+X*X [X →X*X]
→ a+a*X [X→a]
→ a+a*a
1/3/2020Ambiguous Grammar & Parse Tree
7
Figure: 3Figure: 2
Parse Tree - 1 Parse Tree - 2
Since there are two different parse trees for a single
string "a+a*a",
So the grammar G is ambiguous.
x +
x
x
x
a x
aa
*
x *
x
x
x
+x
a a
a
1/3/2020Ambiguous Grammar & Parse Tree
8Example - 2
The grammar G is with production rules-
S → T
T → 01
T → T T
From the above grammar, Check the ambiguity by the derivation &
parse tree for string “010101”.
Derivation – 1
S → T
→ TT [T→TT]
→ TTT [T→TT]
→ 01TT [T→01]
→ 0101T [T→01]
→ 010101 [T→01]
Derivation – 2
S → T
→ TT [T→TT]
→ TTT [T→TT]
→ TT01 [T→01]
→ T0101 [T→01]
→ 010101 [T→01]
1/3/2020Ambiguous Grammar & Parse Tree
9
Figure: 3Figure: 2
Parse Tree - 1 Parse Tree - 2
S
T
T T
T
T
0 1 0 1
T
0 1
S
T
T T
T
T
0 1 0 1
T
0 1
The grammar has two or more derivation & parse tree. So this is also an Ambiguous.
1/3/2020Ambiguous Grammar & Parse Tree
10
Example - 3
Given a context-free grammar G with start symbol S, terminal symbols T and
productions P, the language L(G) that G generates is defined to be the set of strings
of terminal symbols that can be obtained by derivation from S using the productions
P, i.e., the set {w ∈ T ∗ | S ⇒ w}.
T → R
T → aTc
R → ∈
R → RbR
Check that the grammar is Ambiguous or Unambiguous for the string ‘aabbbcc’ ?
1/3/2020Ambiguous Grammar & Parse Tree
11
T ⇒ aTc
⇒ aaTcc [T ⇒ aTc]
⇒ aaRcc [T ⇒ R]
⇒ aaRbRcc [R ⇒ RbR]
⇒ aaRbRbRcc [R ⇒ RbR]
⇒ aaRbRbRbRcc [R ⇒ RbR]
⇒ aabRbRbRcc [R ⇒ ∈]
⇒ aabbRbRcc [R ⇒ ∈]
⇒ aabbbRcc [R ⇒ ∈]
⇒ aabbbcc [R ⇒ ∈]
Leftmost Derivation of the string ‘aabbbcc’
T ⇒ aTc
⇒ aaTcc [T ⇒ aTc]
⇒ aaRcc [T ⇒ R]
⇒ aaRbRcc [R ⇒ RbR]
⇒ aaRbRbRcc [R ⇒ RbR]
⇒ aaRbRbRbRcc [R ⇒ RbR]
⇒ aaRbRbRbcc [R ⇒ ∈]
⇒ aaRbRbbcc [R ⇒ ∈]
⇒ aaRbbbcc [R ⇒ ∈]
⇒ aabbbcc [R ⇒ ∈]
Rightmost derivation of the string ‘aabbbcc’
1/3/2020Ambiguous Grammar & Parse Tree
12
b R
b
T
R R
R
∈
R
c
a
a T
T c
b R
∈
∈∈
b
R
b
T
R R
R
∈
R
c
a
a T
T c
∈
R
b
∈
∈
Parse Tree-1 Parse Tree-2
R
R
1/3/2020Ambiguous Grammar & Parse Tree
13
Example - 4
Consider VN = {S, E}, VT = {if, then, else} and a grammar G = (VT,VN, S, P) such that the
following grammar
S → if E then S | if E then S else S
Are productions of G. Then the string
W → if E then if E then S else S
Now check the ambiguity.
Derivation – 1
S → if E then S else S
→ if E then if E then S else S
Derivation – 2
S → if E then S
→ if E then if E then S else S
1/3/2020Ambiguous Grammar & Parse Tree
14S
Eif then S else S
if E Sthen
S
Eif then S
else Sif E Sthen
Parse Tree-1
Parse Tree-2
The string has two derivation trees. So it is ambiguous.
1/3/2020Ambiguous Grammar & Parse Tree
15
Consider the following grammar- S → aS | ∈
The language generated by this grammar-
L = { an , n>=0 } or a*
Example - 5
Let us consider a string w = “aaa”.
First Derivation-
S → aS
→ aaS [S → aS]
→ aaaS [S → aS]
→ aaa∈ [S → ∈ ]
→ aaa
Second Derivation-
S → aS
→ aaS [S → aS]
→ aaaS [S → aS]
→ aaa∈ [S → ∈]
→ aaa
Figure - 4
Parse Tree
S
Sa
a S
Sa
∈
 Here first derivation and second derivation are exactly same.
So, this is an example of an unambiguous grammar.
1/3/2020Ambiguous Grammar & Parse Tree
16Example - 6
The following Grammar generates prefix expressions with
operands x and y and binary operators +, -, and *:
E → +EE | *EE | -EE | x | y
Now find leftmost and rightmost derivations and derivation
tree for the string ‘+*-xyxy’.
+ E E
E
y
* E E
EE-
yx
x
Parse Tree:
E → +EE
→ +*EEE [E → *EE]
→+*-EEEE [E → -EE]
→+*-xEEE [E → x]
→+*-xyEE [E → y]
→+*-xyxE [E → x]
→+*-xyxy [E → y]
Left Derrivation:
In this grammar, there is no more derivation and parse tree.
So this is also unambiguous.
1/3/2020Ambiguous Grammar & Parse Tree
17Example - 7
Consider the given grammar
S → S+S | S-S | S*S | S/S | (S) | x | y | z
is the productions of G. Then for the string W → (x+y)*x-z*y/(x+x) ; check the ambiguity.
S → S*S
→ (S)*S
→ (S+S)*S
→ (x+y)*S
→ (x+y)*S-S
→ (x+y)*x-S
→ (x+y)*x-S*S
→ (x+y)*x-z*S
→ (x+y)*x-z*S/S
→ (x+y)*x-z*y/S
→ (x+y)*x-z*y/(S)
→ (x+y)*x-z*y/(S+S)
→ (x+y)*x-z*y/(x+x)
S → S/S
→ S/(S)
→ S/(S+S)
→ S/(x+x)
→ S*S/(x+x)
→ S*y/(x+x)
→ S-S*y/(x+x)
→ S-z*y/(x+x)
→ S*S-z*y/(x+x)
→ S*x-z*y/(x+x)
→ (S)*x-z*y/(x+x)
→ (S+S)*x-z*y/(x+x)
→ (x+y)*x-z*y/(x+x)
Derivation – 1 Derivation – 2
In this grammar, there are more than two derivation and parse tree. So this is an ambiguous.
1/3/2020Ambiguous Grammar & Parse Tree
18
Reference from:
 Basics of Compiler Design - by Torben Ægidius Mogensen
 Introduction to the Theory of Computation_3rd Edition – by Michael Sipser
 Introduction to Automata Theory, Language, and Computation – by John E. Hopcroft
 https://www.javatpoint.com/automata-ambiguity-in-grammar
 https://www.geeksforgeeks.org/ambiguous-grammar/
1/3/2020Ambiguous Grammar & Parse Tree
19
1/3/2020Ambiguous Grammar & Parse Tree
20

More Related Content

PPTX
Compiler design syntax analysis
PPTX
CONTEXT FREE GRAMMAR
PPTX
Automata theory - CFG and normal forms
PDF
Parse Tree
PPTX
Structure of the compiler
PPTX
Top down parsing
PPTX
Specification-of-tokens
PPT
Context free grammars
Compiler design syntax analysis
CONTEXT FREE GRAMMAR
Automata theory - CFG and normal forms
Parse Tree
Structure of the compiler
Top down parsing
Specification-of-tokens
Context free grammars

What's hot (20)

PPTX
Lexical Analysis - Compiler Design
PPTX
Parsing LL(1), SLR, LR(1)
PPTX
Theory of automata and formal language
PPTX
Regular expressions
PPTX
Lexical analysis - Compiler Design
PDF
Algorithms Lecture 2: Analysis of Algorithms I
PDF
Regular language and Regular expression
PPTX
Analysis of algorithm
PPT
Lecture 3,4
PPT
Chomsky Hierarchy.ppt
PPTX
Context free grammar
PPT
Parsing
PPTX
Parsing in Compiler Design
PPTX
6-Practice Problems - LL(1) parser-16-05-2023.pptx
PPTX
Context free grammar
PPTX
Skip lists (Advance Data structure)
PPTX
First order predicate logic(fopl)
PPTX
PPTX
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
PDF
LR Parsing
Lexical Analysis - Compiler Design
Parsing LL(1), SLR, LR(1)
Theory of automata and formal language
Regular expressions
Lexical analysis - Compiler Design
Algorithms Lecture 2: Analysis of Algorithms I
Regular language and Regular expression
Analysis of algorithm
Lecture 3,4
Chomsky Hierarchy.ppt
Context free grammar
Parsing
Parsing in Compiler Design
6-Practice Problems - LL(1) parser-16-05-2023.pptx
Context free grammar
Skip lists (Advance Data structure)
First order predicate logic(fopl)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
LR Parsing
Ad

Similar to Ambiguous & Unambiguous Grammar (20)

PPTX
L11 context-free grammers 2.pptx parse tree
PDF
Lecture6 syntax analysis_2
PPTX
Context Free Grammar presentation 2025.pptx
PPT
Context free grammar
PPTX
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
DOCX
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
PPTX
Ambiguity Grammar.pptx
PPTX
Lexhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.pptx
PPTX
2.5 ambiguity in context free grammars
PDF
cfl2.pdf
PDF
Syntax Analysis_73 pages notes IIIT Manipur.pdf
PPTX
4. languages and grammars
PDF
ambiguity grammar.pdf
PPTX
Lefmost rightmost TOC.pptx
DOCX
8-Practice problems on operator precedence parser-24-05-2023.docx
PDF
syntaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.pdf
PDF
Ch2_Compilers A Simple One-Pass Compiler.pdf
PPTX
Ayan Das_25300121057.pptx
PDF
3b. LMD & RMD.pdf
L11 context-free grammers 2.pptx parse tree
Lecture6 syntax analysis_2
Context Free Grammar presentation 2025.pptx
Context free grammar
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
9-Removal of ambiguity, precedence and associativity-26-05-2023.docx
Ambiguity Grammar.pptx
Lexhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.pptx
2.5 ambiguity in context free grammars
cfl2.pdf
Syntax Analysis_73 pages notes IIIT Manipur.pdf
4. languages and grammars
ambiguity grammar.pdf
Lefmost rightmost TOC.pptx
8-Practice problems on operator precedence parser-24-05-2023.docx
syntaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.pdf
Ch2_Compilers A Simple One-Pass Compiler.pdf
Ayan Das_25300121057.pptx
3b. LMD & RMD.pdf
Ad

Recently uploaded (20)

PPTX
Intro to ISO 9001 2015.pptx wareness raising
PPTX
Learning-Plan-5-Policies-and-Practices.pptx
PPTX
Primary and secondary sources, and history
PDF
oil_refinery_presentation_v1 sllfmfls.pdf
PPTX
Self management and self evaluation presentation
PPTX
Emphasizing It's Not The End 08 06 2025.pptx
PPTX
_ISO_Presentation_ISO 9001 and 45001.pptx
PPTX
Effective_Handling_Information_Presentation.pptx
PPTX
worship songs, in any order, compilation
PPTX
Presentation for DGJV QMS (PQP)_12.03.2025.pptx
DOCX
"Project Management: Ultimate Guide to Tools, Techniques, and Strategies (2025)"
PPTX
Hydrogel Based delivery Cancer Treatment
DOCX
ENGLISH PROJECT FOR BINOD BIHARI MAHTO KOYLANCHAL UNIVERSITY
PDF
Swiggy’s Playbook: UX, Logistics & Monetization
PPTX
Phrase, structure, use, definition in sentence
PDF
Instagram's Product Secrets Unveiled with this PPT
PPTX
2025-08-10 Joseph 02 (shared slides).pptx
PPTX
Relationship Management Presentation In Banking.pptx
PPTX
Biography Text about someone important in life
PPTX
Called To More (Final I Think) 08 03 2025.pptx
Intro to ISO 9001 2015.pptx wareness raising
Learning-Plan-5-Policies-and-Practices.pptx
Primary and secondary sources, and history
oil_refinery_presentation_v1 sllfmfls.pdf
Self management and self evaluation presentation
Emphasizing It's Not The End 08 06 2025.pptx
_ISO_Presentation_ISO 9001 and 45001.pptx
Effective_Handling_Information_Presentation.pptx
worship songs, in any order, compilation
Presentation for DGJV QMS (PQP)_12.03.2025.pptx
"Project Management: Ultimate Guide to Tools, Techniques, and Strategies (2025)"
Hydrogel Based delivery Cancer Treatment
ENGLISH PROJECT FOR BINOD BIHARI MAHTO KOYLANCHAL UNIVERSITY
Swiggy’s Playbook: UX, Logistics & Monetization
Phrase, structure, use, definition in sentence
Instagram's Product Secrets Unveiled with this PPT
2025-08-10 Joseph 02 (shared slides).pptx
Relationship Management Presentation In Banking.pptx
Biography Text about someone important in life
Called To More (Final I Think) 08 03 2025.pptx

Ambiguous & Unambiguous Grammar

  • 1. Topic: Ambiguous grammar & parse Tree COURSE TITLE: THEORY OF COMPUTING COURSE CODE: SE-234 1 Department Of Software Engineering
  • 2. Ambiguous & Unambiguous Grammar Depending on Number of Derivation Trees, CFGs are sub-divided into 2 types:  Ambiguous Grammars  Unambiguous Grammars 1/3/2020Ambiguous Grammar & Parse Tree 2 The Context Free Grammars(CFGs) are classified based on:  Number of Derivation trees  Number of strings
  • 3. What is Ambiguous Grammar? 1/3/2020Ambiguous Grammar & Parse Tree 3 A grammar is said to be Ambiguous if there exists two or more derivation & parse tree for a string w. Figure: 1 s a s s s a s aa a
  • 4. What is Unambiguous Grammar? 1/3/2020Ambiguous Grammar & Parse Tree 4 A grammar can be unambiguous if the grammar does not contain more than one leftmost derivation or more than one rightmost derivation or more than one parse tree for the given input string. b x b x x b
  • 5. 1/3/2020Ambiguous Grammar & Parse Tree 5 G = (V,T,P,S) is a CFG is said to be ambiguous if and only if there exist a string in T* that has more than one parse tree. where, • V = finite set of variables. • T = finite set of terminals. • P = finite set of productions of the form, A → α, where A is a variable and α ∈ (V ∪ T)* • S = designated variable called the start symbol. Definition of ambiguous & unambiguous grammar:
  • 6. 1/3/2020Ambiguous Grammar & Parse Tree 6Example - 1 Check the grammar G with production rules − X → X+X | X*X |X| a is ambiguous or not? Let’s find out the derivation tree for the string "a+a*a". It has two leftmost derivations. Derivation – 1 X → X*X → X+X*X [X→X+X] → a+X*X [X →X*X] → a+a*X [X→a] → a+a*a Derivation – 2 X → X+X → a+X [X→a] → a+X*X [X →X*X] → a+a*X [X→a] → a+a*a
  • 7. 1/3/2020Ambiguous Grammar & Parse Tree 7 Figure: 3Figure: 2 Parse Tree - 1 Parse Tree - 2 Since there are two different parse trees for a single string "a+a*a", So the grammar G is ambiguous. x + x x x a x aa * x * x x x +x a a a
  • 8. 1/3/2020Ambiguous Grammar & Parse Tree 8Example - 2 The grammar G is with production rules- S → T T → 01 T → T T From the above grammar, Check the ambiguity by the derivation & parse tree for string “010101”. Derivation – 1 S → T → TT [T→TT] → TTT [T→TT] → 01TT [T→01] → 0101T [T→01] → 010101 [T→01] Derivation – 2 S → T → TT [T→TT] → TTT [T→TT] → TT01 [T→01] → T0101 [T→01] → 010101 [T→01]
  • 9. 1/3/2020Ambiguous Grammar & Parse Tree 9 Figure: 3Figure: 2 Parse Tree - 1 Parse Tree - 2 S T T T T T 0 1 0 1 T 0 1 S T T T T T 0 1 0 1 T 0 1 The grammar has two or more derivation & parse tree. So this is also an Ambiguous.
  • 10. 1/3/2020Ambiguous Grammar & Parse Tree 10 Example - 3 Given a context-free grammar G with start symbol S, terminal symbols T and productions P, the language L(G) that G generates is defined to be the set of strings of terminal symbols that can be obtained by derivation from S using the productions P, i.e., the set {w ∈ T ∗ | S ⇒ w}. T → R T → aTc R → ∈ R → RbR Check that the grammar is Ambiguous or Unambiguous for the string ‘aabbbcc’ ?
  • 11. 1/3/2020Ambiguous Grammar & Parse Tree 11 T ⇒ aTc ⇒ aaTcc [T ⇒ aTc] ⇒ aaRcc [T ⇒ R] ⇒ aaRbRcc [R ⇒ RbR] ⇒ aaRbRbRcc [R ⇒ RbR] ⇒ aaRbRbRbRcc [R ⇒ RbR] ⇒ aabRbRbRcc [R ⇒ ∈] ⇒ aabbRbRcc [R ⇒ ∈] ⇒ aabbbRcc [R ⇒ ∈] ⇒ aabbbcc [R ⇒ ∈] Leftmost Derivation of the string ‘aabbbcc’ T ⇒ aTc ⇒ aaTcc [T ⇒ aTc] ⇒ aaRcc [T ⇒ R] ⇒ aaRbRcc [R ⇒ RbR] ⇒ aaRbRbRcc [R ⇒ RbR] ⇒ aaRbRbRbRcc [R ⇒ RbR] ⇒ aaRbRbRbcc [R ⇒ ∈] ⇒ aaRbRbbcc [R ⇒ ∈] ⇒ aaRbbbcc [R ⇒ ∈] ⇒ aabbbcc [R ⇒ ∈] Rightmost derivation of the string ‘aabbbcc’
  • 12. 1/3/2020Ambiguous Grammar & Parse Tree 12 b R b T R R R ∈ R c a a T T c b R ∈ ∈∈ b R b T R R R ∈ R c a a T T c ∈ R b ∈ ∈ Parse Tree-1 Parse Tree-2 R R
  • 13. 1/3/2020Ambiguous Grammar & Parse Tree 13 Example - 4 Consider VN = {S, E}, VT = {if, then, else} and a grammar G = (VT,VN, S, P) such that the following grammar S → if E then S | if E then S else S Are productions of G. Then the string W → if E then if E then S else S Now check the ambiguity. Derivation – 1 S → if E then S else S → if E then if E then S else S Derivation – 2 S → if E then S → if E then if E then S else S
  • 14. 1/3/2020Ambiguous Grammar & Parse Tree 14S Eif then S else S if E Sthen S Eif then S else Sif E Sthen Parse Tree-1 Parse Tree-2 The string has two derivation trees. So it is ambiguous.
  • 15. 1/3/2020Ambiguous Grammar & Parse Tree 15 Consider the following grammar- S → aS | ∈ The language generated by this grammar- L = { an , n>=0 } or a* Example - 5 Let us consider a string w = “aaa”. First Derivation- S → aS → aaS [S → aS] → aaaS [S → aS] → aaa∈ [S → ∈ ] → aaa Second Derivation- S → aS → aaS [S → aS] → aaaS [S → aS] → aaa∈ [S → ∈] → aaa Figure - 4 Parse Tree S Sa a S Sa ∈  Here first derivation and second derivation are exactly same. So, this is an example of an unambiguous grammar.
  • 16. 1/3/2020Ambiguous Grammar & Parse Tree 16Example - 6 The following Grammar generates prefix expressions with operands x and y and binary operators +, -, and *: E → +EE | *EE | -EE | x | y Now find leftmost and rightmost derivations and derivation tree for the string ‘+*-xyxy’. + E E E y * E E EE- yx x Parse Tree: E → +EE → +*EEE [E → *EE] →+*-EEEE [E → -EE] →+*-xEEE [E → x] →+*-xyEE [E → y] →+*-xyxE [E → x] →+*-xyxy [E → y] Left Derrivation: In this grammar, there is no more derivation and parse tree. So this is also unambiguous.
  • 17. 1/3/2020Ambiguous Grammar & Parse Tree 17Example - 7 Consider the given grammar S → S+S | S-S | S*S | S/S | (S) | x | y | z is the productions of G. Then for the string W → (x+y)*x-z*y/(x+x) ; check the ambiguity. S → S*S → (S)*S → (S+S)*S → (x+y)*S → (x+y)*S-S → (x+y)*x-S → (x+y)*x-S*S → (x+y)*x-z*S → (x+y)*x-z*S/S → (x+y)*x-z*y/S → (x+y)*x-z*y/(S) → (x+y)*x-z*y/(S+S) → (x+y)*x-z*y/(x+x) S → S/S → S/(S) → S/(S+S) → S/(x+x) → S*S/(x+x) → S*y/(x+x) → S-S*y/(x+x) → S-z*y/(x+x) → S*S-z*y/(x+x) → S*x-z*y/(x+x) → (S)*x-z*y/(x+x) → (S+S)*x-z*y/(x+x) → (x+y)*x-z*y/(x+x) Derivation – 1 Derivation – 2 In this grammar, there are more than two derivation and parse tree. So this is an ambiguous.
  • 18. 1/3/2020Ambiguous Grammar & Parse Tree 18 Reference from:  Basics of Compiler Design - by Torben Ægidius Mogensen  Introduction to the Theory of Computation_3rd Edition – by Michael Sipser  Introduction to Automata Theory, Language, and Computation – by John E. Hopcroft  https://www.javatpoint.com/automata-ambiguity-in-grammar  https://www.geeksforgeeks.org/ambiguous-grammar/