SlideShare a Scribd company logo
1 of 22
1
Parse Trees
Definitions
Relationship to lm and rm Derivations
Ambiguity in Grammars
2
Parse Trees: graphical representations of
derivations
Parse trees have nodes labeled by symbols of
a CFG.
Leaves: labeled by a terminal or ε.
Interior nodes: labeled by variables.
Children are labeled by the right side of a
production for the parent.
Root: labeled by the start symbol.
Yield: string produced by concatenation of
leaves left-to-right
3
Example: Parse Tree for
Derivation of (())() in S -> SS | (S) | ()
S => SS => (S)S => (())S => (())()
S
S
S
S )
(
( )
( )
Yield: (())()
4
For CFG with start symbol S and productions
S->A1B
A->0A|e
B->0B|1B|e
Show that the parse trees of lm and rm derivations of string
00101 are identical
Work on board
5
S->A1B A->0A|e B->0B|1B|e
Show that the parse trees of lm and rm derivations of string 00101 are identical
The sequence of the derivation is root to leaves.
Hence, parse trees have no “leftmost” or “rightmost” distinction.
6
Ambiguous CFG’s
3 equivalent properties of “ambiguous” CFGs
1. There is a string in the L(CFG) that has two
different leftmost derivations.
2. There is a string in the L(CFG) that has two
different rightmost derivations.
3. There is a string in the L(CFG) that is the yield of 2
distinct parse trees
Proof of any one of these shows that CFG is ambiguous
7
Prove S -> SS | (S) | () is ambiguous
by 2 left-most derivations of ()()()
Work on board
8
S -> SS | (S) | () is ambiguous
Prove by 2 lm derivations of ()()()
S=>SS=>SSS=>()SS=>()()S=>()()()
S=>SS=>()S=>()SS=>()()S=>()()()
9
CptS 317 Fall 2023
Assignment 12, Exercises 5.4.1 text p 215
Use string aab to show that S->aS|aSbS|e is ambiguous
by all 3 methods: 2 left-most, 2 right-most, and 2 parse
trees.
10
Exercise 5.4.3 p216
Remove the ambiguity in S->aS|aSbS|e
As you will demonstrate in HW12, derivations of aab can
start with either S=>aS or S=>aSbS.
To remove ambiguity in derivation of aab we change the
production S=>aSbS to one that forces only one way to
start.
Example: add variable T with productions such that starting
with S=>aTbS cannot produce aab by lm steps.
11
Show on board that in CFG
S->aS
S->aTbS
S->e
T->aTbT
T->e
the left-most derivation of aab is unique
13
Removing ambiguity in derivation of aab does not prove
that S=>aS|aTbS|e with T=>aTbT|e is unambiguous.
Difficult to know if changes designed to avoid ambiguity
in derivation of aab apply to all strings in L(CFG)
Change to CFG must preserve the content of L(CFG)
Easy to prove ambiguous. Only requires one string
Hard to prove unambiguous
14
Ambiguity is a Property of Grammars, not
Languages
• Same CFL may have ambiguous and
unambiguous CFGs
• Example: balanced-parentheses CFL
• Recall, S -> SS | (S) | () shown to be
ambiguous by 2 lm derivations of ()()()
• Balanced-parentheses CFL also has an
unambiguous CFG
15
Unambiguous balanced-parenthesis CFG
B -> (RB | ε R -> ) | (RR
B is the start symbol
Derivations must start with B ->(RB
To expand with “(“, use R->(RR
To expand with “)”, use R ->)
End with B->e
Example of LL(1) CFG: “Leftmost derivation, left-to-right
scan, one symbol of look-a-head.”
16
Unambiguous balanced-parenthesis CFG
B -> (RB | ε R -> ) | (RR
Example derivation of (())() by LL(1)
Work on board
17
Unambiguous balanced-parenthesis CFG
B -> (RB | ε R -> ) | (RR
Example derivation of (())()
B =>lm (RB =>lm ((RRB =>lm (()RB =>lm (())B
(())B =>lm (())(RB =>lm (())()B =>lm (())()
At each lm step, the production needed is uniquely
defined.
18
LL(1) Grammars
• By construction obviously unambiguous
• You can always figure out the production to
use in a leftmost derivation by scanning the
given string left-to-right and looking at the
next symbol only
• Most programming languages have LL(1)
grammars.
19
Inherent Ambiguity
• Not every ambiguous grammar can be
“fixed” as was the case with the balanced-
parentheses grammar.
• Some CFL’s are inherently ambiguous; every
grammar for the language is ambiguous.
20
Example: Inherent Ambiguity
• The language {0i1j2k | i = j or j = k} is inherently
ambiguous.
• At least some of the strings of the form 0n1n2n
can be generated by two left-most derivations
depending on which you check first: equal
number of 0’s and 1’s or equal number of 1’s
and 2’s.
21
Example of ambiguous grammar for
{0i1j2k | i = j or j = k}
S -> AB | CD
A -> 0A1 | 01
B -> 2B | 2
C -> 0C | 0
D -> 1D2 | 12
A generates equal 0’s and 1’s
B generates any number of 2’s
C generates any number of 0’s
D generates equal 1’s and 2’s
There are two leftmost derivations of every string with
equal numbers of 0’s, 1’s, and 2’s.
Show true for n=2, w=001122, with 2 lm derivations
Show on board
22
Example of ambiguous grammar for
{0i1j2k | i = j or j = k}
S -> AB | CD
A -> 0A1 | 01
B -> 2B | 2
C -> 0C | 0
D -> 1D2 | 12
A generates equal 0’s and 1’s
B generates any number of 2’s
C generates any number of 0’s
D generates equal 1’s and 2’s
lm: S => AB => 0A1B =>0011B => 0112B => 001122
lm: S => CD => 0CD=>00D => 001D2 => 001122

More Related Content

Similar to L11 context-free grammers 2.pptx parse tree

Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal formsAkila Krishnamoorthy
 
Lecture 3 RE NFA DFA
Lecture 3   RE NFA DFA Lecture 3   RE NFA DFA
Lecture 3 RE NFA DFA Rebaz Najeeb
 
Chapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptxChapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptxDrIsikoIsaac
 
Conteext-free Grammer
Conteext-free GrammerConteext-free Grammer
Conteext-free GrammerHASHIR RAZA
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in CompilersMahbubur Rahman
 
Natural Language Processing - Writing Grammar
Natural Language Processing - Writing GrammarNatural Language Processing - Writing Grammar
Natural Language Processing - Writing GrammarJasmine Peniel
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLPkartikaVashisht
 
Lefmost rightmost TOC.pptx
Lefmost rightmost TOC.pptxLefmost rightmost TOC.pptx
Lefmost rightmost TOC.pptxJisock
 
Mod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptxMod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptxRaviAr5
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Iffat Anjum
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysisraosir123
 
Combinational logic circuits design and implementation
Combinational logic circuits design and implementationCombinational logic circuits design and implementation
Combinational logic circuits design and implementationssuserca5764
 

Similar to L11 context-free grammers 2.pptx parse tree (20)

Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
 
Lecture 3 RE NFA DFA
Lecture 3   RE NFA DFA Lecture 3   RE NFA DFA
Lecture 3 RE NFA DFA
 
Chapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptxChapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptx
 
Conteext-free Grammer
Conteext-free GrammerConteext-free Grammer
Conteext-free Grammer
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Natural Language Processing - Writing Grammar
Natural Language Processing - Writing GrammarNatural Language Processing - Writing Grammar
Natural Language Processing - Writing Grammar
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
Unit iii
Unit iiiUnit iii
Unit iii
 
Lefmost rightmost TOC.pptx
Lefmost rightmost TOC.pptxLefmost rightmost TOC.pptx
Lefmost rightmost TOC.pptx
 
Regular Expressions To Finite Automata
Regular Expressions To Finite AutomataRegular Expressions To Finite Automata
Regular Expressions To Finite Automata
 
Mod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptxMod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptx
 
Huffman Encoding Pr
Huffman Encoding PrHuffman Encoding Pr
Huffman Encoding Pr
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Lec1.pptx
Lec1.pptxLec1.pptx
Lec1.pptx
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
 
Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 
Combinational logic circuits design and implementation
Combinational logic circuits design and implementationCombinational logic circuits design and implementation
Combinational logic circuits design and implementation
 

Recently uploaded

Data Visualization Exploring and Explaining with Data 1st Edition by Camm sol...
Data Visualization Exploring and Explaining with Data 1st Edition by Camm sol...Data Visualization Exploring and Explaining with Data 1st Edition by Camm sol...
Data Visualization Exploring and Explaining with Data 1st Edition by Camm sol...ssuserf63bd7
 
如何办理加州大学伯克利分校毕业证(UCB毕业证)成绩单留信学历认证
如何办理加州大学伯克利分校毕业证(UCB毕业证)成绩单留信学历认证如何办理加州大学伯克利分校毕业证(UCB毕业证)成绩单留信学历认证
如何办理加州大学伯克利分校毕业证(UCB毕业证)成绩单留信学历认证a8om7o51
 
SCI8-Q4-MOD11.pdfwrwujrrjfaajerjrajrrarj
SCI8-Q4-MOD11.pdfwrwujrrjfaajerjrajrrarjSCI8-Q4-MOD11.pdfwrwujrrjfaajerjrajrrarj
SCI8-Q4-MOD11.pdfwrwujrrjfaajerjrajrrarjadimosmejiaslendon
 
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证pwgnohujw
 
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一fztigerwe
 
Northern New England Tableau User Group (TUG) May 2024
Northern New England Tableau User Group (TUG) May 2024Northern New England Tableau User Group (TUG) May 2024
Northern New England Tableau User Group (TUG) May 2024patrickdtherriault
 
obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...
obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...
obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...yulianti213969
 
Bios of leading Astrologers & Researchers
Bios of leading Astrologers & ResearchersBios of leading Astrologers & Researchers
Bios of leading Astrologers & Researchersdarmandersingh4580
 
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证zifhagzkk
 
How to Transform Clinical Trial Management with Advanced Data Analytics
How to Transform Clinical Trial Management with Advanced Data AnalyticsHow to Transform Clinical Trial Management with Advanced Data Analytics
How to Transform Clinical Trial Management with Advanced Data AnalyticsBrainSell Technologies
 
The Significance of Transliteration Enhancing
The Significance of Transliteration EnhancingThe Significance of Transliteration Enhancing
The Significance of Transliteration Enhancingmohamed Elzalabany
 
Genuine love spell caster )! ,+27834335081) Ex lover back permanently in At...
Genuine love spell caster )! ,+27834335081)   Ex lover back permanently in At...Genuine love spell caster )! ,+27834335081)   Ex lover back permanently in At...
Genuine love spell caster )! ,+27834335081) Ex lover back permanently in At...BabaJohn3
 
What is Insertion Sort. Its basic information
What is Insertion Sort. Its basic informationWhat is Insertion Sort. Its basic information
What is Insertion Sort. Its basic informationmuqadasqasim10
 
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证acoha1
 
Data Analytics for Digital Marketing Lecture for Advanced Digital & Social Me...
Data Analytics for Digital Marketing Lecture for Advanced Digital & Social Me...Data Analytics for Digital Marketing Lecture for Advanced Digital & Social Me...
Data Analytics for Digital Marketing Lecture for Advanced Digital & Social Me...Valters Lauzums
 
Credit Card Fraud Detection: Safeguarding Transactions in the Digital Age
Credit Card Fraud Detection: Safeguarding Transactions in the Digital AgeCredit Card Fraud Detection: Safeguarding Transactions in the Digital Age
Credit Card Fraud Detection: Safeguarding Transactions in the Digital AgeBoston Institute of Analytics
 
Seven tools of quality control.slideshare
Seven tools of quality control.slideshareSeven tools of quality control.slideshare
Seven tools of quality control.slideshareraiaryan448
 
edited gordis ebook sixth edition david d.pdf
edited gordis ebook sixth edition david d.pdfedited gordis ebook sixth edition david d.pdf
edited gordis ebook sixth edition david d.pdfgreat91
 
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证acoha1
 

Recently uploaded (20)

Data Visualization Exploring and Explaining with Data 1st Edition by Camm sol...
Data Visualization Exploring and Explaining with Data 1st Edition by Camm sol...Data Visualization Exploring and Explaining with Data 1st Edition by Camm sol...
Data Visualization Exploring and Explaining with Data 1st Edition by Camm sol...
 
Abortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotec
Abortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotecAbortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotec
Abortion pills in Riyadh Saudi Arabia (+966572737505 buy cytotec
 
如何办理加州大学伯克利分校毕业证(UCB毕业证)成绩单留信学历认证
如何办理加州大学伯克利分校毕业证(UCB毕业证)成绩单留信学历认证如何办理加州大学伯克利分校毕业证(UCB毕业证)成绩单留信学历认证
如何办理加州大学伯克利分校毕业证(UCB毕业证)成绩单留信学历认证
 
SCI8-Q4-MOD11.pdfwrwujrrjfaajerjrajrrarj
SCI8-Q4-MOD11.pdfwrwujrrjfaajerjrajrrarjSCI8-Q4-MOD11.pdfwrwujrrjfaajerjrajrrarj
SCI8-Q4-MOD11.pdfwrwujrrjfaajerjrajrrarj
 
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
原件一样(UWO毕业证书)西安大略大学毕业证成绩单留信学历认证
 
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一
如何办理哥伦比亚大学毕业证(Columbia毕业证)成绩单原版一比一
 
Northern New England Tableau User Group (TUG) May 2024
Northern New England Tableau User Group (TUG) May 2024Northern New England Tableau User Group (TUG) May 2024
Northern New England Tableau User Group (TUG) May 2024
 
obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...
obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...
obat aborsi Tarakan wa 081336238223 jual obat aborsi cytotec asli di Tarakan9...
 
Bios of leading Astrologers & Researchers
Bios of leading Astrologers & ResearchersBios of leading Astrologers & Researchers
Bios of leading Astrologers & Researchers
 
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
如何办理(Dalhousie毕业证书)达尔豪斯大学毕业证成绩单留信学历认证
 
How to Transform Clinical Trial Management with Advanced Data Analytics
How to Transform Clinical Trial Management with Advanced Data AnalyticsHow to Transform Clinical Trial Management with Advanced Data Analytics
How to Transform Clinical Trial Management with Advanced Data Analytics
 
The Significance of Transliteration Enhancing
The Significance of Transliteration EnhancingThe Significance of Transliteration Enhancing
The Significance of Transliteration Enhancing
 
Genuine love spell caster )! ,+27834335081) Ex lover back permanently in At...
Genuine love spell caster )! ,+27834335081)   Ex lover back permanently in At...Genuine love spell caster )! ,+27834335081)   Ex lover back permanently in At...
Genuine love spell caster )! ,+27834335081) Ex lover back permanently in At...
 
What is Insertion Sort. Its basic information
What is Insertion Sort. Its basic informationWhat is Insertion Sort. Its basic information
What is Insertion Sort. Its basic information
 
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单本科硕士学位证留信学历认证
 
Data Analytics for Digital Marketing Lecture for Advanced Digital & Social Me...
Data Analytics for Digital Marketing Lecture for Advanced Digital & Social Me...Data Analytics for Digital Marketing Lecture for Advanced Digital & Social Me...
Data Analytics for Digital Marketing Lecture for Advanced Digital & Social Me...
 
Credit Card Fraud Detection: Safeguarding Transactions in the Digital Age
Credit Card Fraud Detection: Safeguarding Transactions in the Digital AgeCredit Card Fraud Detection: Safeguarding Transactions in the Digital Age
Credit Card Fraud Detection: Safeguarding Transactions in the Digital Age
 
Seven tools of quality control.slideshare
Seven tools of quality control.slideshareSeven tools of quality control.slideshare
Seven tools of quality control.slideshare
 
edited gordis ebook sixth edition david d.pdf
edited gordis ebook sixth edition david d.pdfedited gordis ebook sixth edition david d.pdf
edited gordis ebook sixth edition david d.pdf
 
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(WashU毕业证书)圣路易斯华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
 

L11 context-free grammers 2.pptx parse tree

  • 1. 1 Parse Trees Definitions Relationship to lm and rm Derivations Ambiguity in Grammars
  • 2. 2 Parse Trees: graphical representations of derivations Parse trees have nodes labeled by symbols of a CFG. Leaves: labeled by a terminal or ε. Interior nodes: labeled by variables. Children are labeled by the right side of a production for the parent. Root: labeled by the start symbol. Yield: string produced by concatenation of leaves left-to-right
  • 3. 3 Example: Parse Tree for Derivation of (())() in S -> SS | (S) | () S => SS => (S)S => (())S => (())() S S S S ) ( ( ) ( ) Yield: (())()
  • 4. 4 For CFG with start symbol S and productions S->A1B A->0A|e B->0B|1B|e Show that the parse trees of lm and rm derivations of string 00101 are identical Work on board
  • 5. 5 S->A1B A->0A|e B->0B|1B|e Show that the parse trees of lm and rm derivations of string 00101 are identical The sequence of the derivation is root to leaves. Hence, parse trees have no “leftmost” or “rightmost” distinction.
  • 6. 6 Ambiguous CFG’s 3 equivalent properties of “ambiguous” CFGs 1. There is a string in the L(CFG) that has two different leftmost derivations. 2. There is a string in the L(CFG) that has two different rightmost derivations. 3. There is a string in the L(CFG) that is the yield of 2 distinct parse trees Proof of any one of these shows that CFG is ambiguous
  • 7. 7 Prove S -> SS | (S) | () is ambiguous by 2 left-most derivations of ()()() Work on board
  • 8. 8 S -> SS | (S) | () is ambiguous Prove by 2 lm derivations of ()()() S=>SS=>SSS=>()SS=>()()S=>()()() S=>SS=>()S=>()SS=>()()S=>()()()
  • 9. 9 CptS 317 Fall 2023 Assignment 12, Exercises 5.4.1 text p 215 Use string aab to show that S->aS|aSbS|e is ambiguous by all 3 methods: 2 left-most, 2 right-most, and 2 parse trees.
  • 10. 10 Exercise 5.4.3 p216 Remove the ambiguity in S->aS|aSbS|e As you will demonstrate in HW12, derivations of aab can start with either S=>aS or S=>aSbS. To remove ambiguity in derivation of aab we change the production S=>aSbS to one that forces only one way to start. Example: add variable T with productions such that starting with S=>aTbS cannot produce aab by lm steps.
  • 11. 11 Show on board that in CFG S->aS S->aTbS S->e T->aTbT T->e the left-most derivation of aab is unique
  • 12.
  • 13. 13 Removing ambiguity in derivation of aab does not prove that S=>aS|aTbS|e with T=>aTbT|e is unambiguous. Difficult to know if changes designed to avoid ambiguity in derivation of aab apply to all strings in L(CFG) Change to CFG must preserve the content of L(CFG) Easy to prove ambiguous. Only requires one string Hard to prove unambiguous
  • 14. 14 Ambiguity is a Property of Grammars, not Languages • Same CFL may have ambiguous and unambiguous CFGs • Example: balanced-parentheses CFL • Recall, S -> SS | (S) | () shown to be ambiguous by 2 lm derivations of ()()() • Balanced-parentheses CFL also has an unambiguous CFG
  • 15. 15 Unambiguous balanced-parenthesis CFG B -> (RB | ε R -> ) | (RR B is the start symbol Derivations must start with B ->(RB To expand with “(“, use R->(RR To expand with “)”, use R ->) End with B->e Example of LL(1) CFG: “Leftmost derivation, left-to-right scan, one symbol of look-a-head.”
  • 16. 16 Unambiguous balanced-parenthesis CFG B -> (RB | ε R -> ) | (RR Example derivation of (())() by LL(1) Work on board
  • 17. 17 Unambiguous balanced-parenthesis CFG B -> (RB | ε R -> ) | (RR Example derivation of (())() B =>lm (RB =>lm ((RRB =>lm (()RB =>lm (())B (())B =>lm (())(RB =>lm (())()B =>lm (())() At each lm step, the production needed is uniquely defined.
  • 18. 18 LL(1) Grammars • By construction obviously unambiguous • You can always figure out the production to use in a leftmost derivation by scanning the given string left-to-right and looking at the next symbol only • Most programming languages have LL(1) grammars.
  • 19. 19 Inherent Ambiguity • Not every ambiguous grammar can be “fixed” as was the case with the balanced- parentheses grammar. • Some CFL’s are inherently ambiguous; every grammar for the language is ambiguous.
  • 20. 20 Example: Inherent Ambiguity • The language {0i1j2k | i = j or j = k} is inherently ambiguous. • At least some of the strings of the form 0n1n2n can be generated by two left-most derivations depending on which you check first: equal number of 0’s and 1’s or equal number of 1’s and 2’s.
  • 21. 21 Example of ambiguous grammar for {0i1j2k | i = j or j = k} S -> AB | CD A -> 0A1 | 01 B -> 2B | 2 C -> 0C | 0 D -> 1D2 | 12 A generates equal 0’s and 1’s B generates any number of 2’s C generates any number of 0’s D generates equal 1’s and 2’s There are two leftmost derivations of every string with equal numbers of 0’s, 1’s, and 2’s. Show true for n=2, w=001122, with 2 lm derivations Show on board
  • 22. 22 Example of ambiguous grammar for {0i1j2k | i = j or j = k} S -> AB | CD A -> 0A1 | 01 B -> 2B | 2 C -> 0C | 0 D -> 1D2 | 12 A generates equal 0’s and 1’s B generates any number of 2’s C generates any number of 0’s D generates equal 1’s and 2’s lm: S => AB => 0A1B =>0011B => 0112B => 001122 lm: S => CD => 0CD=>00D => 001D2 => 001122