SlideShare a Scribd company logo
1 of 20
Download to read offline
Lecture 6
Syntax Analysis II
Derivation
• A derivation is the sequence of productions
S … … … …
• A derivation can be drawn as a tree• A derivation can be drawn as a tree
– Start symbol is the tree’s root
– For a production X Y1 … … Yn add children
Y1 … … Yn to node X
X
Y1 … … Yn
28-Jan-15 2CS 346 Lecture 5
Example: Left-most derivation
• Grammar: E E + E | E * E | ( E ) | id
• String : id*id+id E
E
E + E E + EE + E E + E
E * E + E
id * E + E E * E id
id * id + E
id * id + id id id Parse Tree
28-Jan-15 3CS 346 Lecture 5
Parse Tree
• A parse tree has
– Terminals at the leaves
– Non-terminal at the interior nodes
• An in-order traversal of the leaves is the
original input
• The parse tree shows the association of
operations, the input string does not
28-Jan-15 4CS 346 Lecture 5
Right-most derivation
• The example is a left-
most derivation
– At each step, replace the
left most non terminals
E
E + E
E * E + E
• There is a equivalent
notion of right-most
derivation
E * E + E
id * E + E
id * id + E
id * id + id
28-Jan-15 5CS 346 Lecture 5
Parse Tree Derivations
• Note that the left-most and right-most
derivation have the same parse tree
• Every parse tree has the left-most and right-• Every parse tree has the left-most and right-
most derivations
28-Jan-15 6CS 346 Lecture 5
Ambiguity
• Grammar: E E + E | E * E | ( E ) | id
• String : id*id+id
• This string has two parse trees
E E
E + E E * E
E * E id id E + E
id id id id
28-Jan-15 7CS 346 Lecture 5
Ambiguous Grammar
• A grammar is ambiguous if it has more than
one parse tree for some string
– Equivalently, there is more than one right-most or
left-most derivation for some stringleft-most derivation for some string
• Ambiguity means that some programs are ill-
defined
28-Jan-15 8CS 346 Lecture 5
How to handle Ambiguous Grammar
• Several ways to handle
• Rewrite the grammar unambiguously
E E’+ E| E’
E’ id * E’| id| ( E ) * E | ( E )
• Enforce precedence of * over +
28-Jan-15 9CS 346 Lecture 5
• Grammar: E E’+ E | E’
E’ id * E’ | id | ( E ) * E’ | ( E )
• String : id*id+id
E
Enforce precedence of * over + by
divide the productions in two classes;
one handle + and one handles *; so one
non terminals for each operators
How to handle Ambiguous Grammar
E
E’ + E
id * E’ E’
id id
non terminals for each operators
E E’+ E E’ + E’ + E
E’+E’+E’+E E’+… +E’
Handles +
E’ id * E’ id*id*E’
id*id*id*E’ id*…*id
Handles *28-Jan-15 10CS 346 Lecture 5
How it works
• The grammar has two separate groups of
productions
• All the pluses (+) must be generated before any of
the times (*); so time(s) (*) are nested more
deeply inside the parse tree; pluses (+) aredeeply inside the parse tree; pluses (+) are
generated in the outermost levels and times (*)
are generated inside the pluses (+).
• So the grammar enforces that the time (*) has
higher precedence than the plus (+)
28-Jan-15 11CS 346 Lecture 5
Ambiguity
• Grammar: E E + E | E * E | ( E ) | id
• String : id*id+id
• This string has two parse trees
E E
E + E E * E
E * E id id E + E
id id id id
28-Jan-15 12CS 346 Lecture 5
Ambiguity
• Grammar: E E + E | E * E | ( E ) | id
• String : id*id+id
• Now, this string has only one parse trees
E
E
E + E
E * E id
id id
E’ + E
id * E’ E’
id id
28-Jan-15 13CS 346 Lecture 5
Example
• Another Expression
E if E then E
| if E then E else E
| OTHER
• The expression: if E1 then if E2 then E3 else E4 has two separate
parse treesparse trees
if if
E1 if E4 E1 if
E2 E3 E2 E3 E4
28-Jan-15 14CS 346 Lecture 5
How to handle the Ambiguity
• The property that we want is: else matches the
closest unmatched then
• Can be resolved as:
E MIF| UIFE MIF| UIF
MIF if E then MIF else MIF
| OTHER
UIF if E then E
| if E MIF else UIF
28-Jan-15 15CS 346 Lecture 5
Associativity of Operators
• Let a Grammar : S S + S | S – S | 0 – 9
• Input: 9 – 5 + 2
S S
S + S S - SS + S S - S
S - S 2 9 S + S
9 5 5 2
(9-5)+2 = 6 9-(5+2) = 2
28-Jan-15 16CS 346 Lecture 5
How to handle
• When an operand like 5 has operators to its left and
right, conventions are needed for deciding which
operator applies to that operand.
• Rule: operator + is associates to the left. Arithmetic• Rule: operator + is associates to the left. Arithmetic
operators (+, -, *, /) are left associative.
• Assignment operator (“=“) in C is right associative.
28-Jan-15 17CS 346 Lecture 5
How to handle
• Left Associative
S S + d | S – d | d
d [0-9]
• Input: 9 – 5 + 2
S
• Right Associative
r l = r | l
l [a-z]
• Input: a = b = c
r
S + d
S – d 2
d 5
9
Left-most derivation
l = r
a l = r
b l
c
Right-most derivation
28-Jan-15 18CS 346 Lecture 5
Push Down Automata
• The language for balanced parenthesis, i.e.
{( i ) i | i ≥ 0}, CFG productions are
S (S)|S
S ɛ
• The situation can be handled if the DFA is• The situation can be handled if the DFA is
augmented with memory
– Memory implemented as a stack
– Such an automata is called Push Down Automata
(PDA)
28-Jan-15 19CS 346 Lecture 5
Lecture6 syntax analysis_2

More Related Content

What's hot

Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Nikhil Sharma
 

What's hot (20)

Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Disjoint sets
Disjoint setsDisjoint sets
Disjoint sets
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
OpenGL 3D Drawing
OpenGL 3D DrawingOpenGL 3D Drawing
OpenGL 3D Drawing
 
Linked stack-and-linked-queue
Linked stack-and-linked-queueLinked stack-and-linked-queue
Linked stack-and-linked-queue
 
Pipelining & All Hazards Solution
Pipelining  & All Hazards SolutionPipelining  & All Hazards Solution
Pipelining & All Hazards Solution
 
Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automata
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithm
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadline
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
3.9 external sorting
3.9 external sorting3.9 external sorting
3.9 external sorting
 
Peephole Optimization
Peephole OptimizationPeephole Optimization
Peephole Optimization
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Linked list
Linked listLinked list
Linked list
 
Compilers
CompilersCompilers
Compilers
 
Red black tree
Red black treeRed black tree
Red black tree
 
Priority queues
Priority queuesPriority queues
Priority queues
 

Viewers also liked

Viewers also liked (13)

Lecture5 syntax analysis_1
Lecture5 syntax analysis_1Lecture5 syntax analysis_1
Lecture5 syntax analysis_1
 
تعرف إلى-الهندسة المعلوماتية
تعرف إلى-الهندسة المعلوماتيةتعرف إلى-الهندسة المعلوماتية
تعرف إلى-الهندسة المعلوماتية
 
Lecture2 general structure of a compiler
Lecture2 general structure of a compilerLecture2 general structure of a compiler
Lecture2 general structure of a compiler
 
Symbolic Automata = Automata + SMT solvers at ExCape14
Symbolic Automata = Automata + SMT solvers at ExCape14Symbolic Automata = Automata + SMT solvers at ExCape14
Symbolic Automata = Automata + SMT solvers at ExCape14
 
ف 2 الدرس الأول والثانى والثالث
ف 2 الدرس الأول والثانى والثالثف 2 الدرس الأول والثانى والثالث
ف 2 الدرس الأول والثانى والثالث
 
Lecture3 lexical analysis
Lecture3 lexical analysisLecture3 lexical analysis
Lecture3 lexical analysis
 
Lecture1 introduction compilers
Lecture1 introduction compilersLecture1 introduction compilers
Lecture1 introduction compilers
 
DFA minimization algorithms in map reduce
DFA minimization algorithms in map reduceDFA minimization algorithms in map reduce
DFA minimization algorithms in map reduce
 
DFA Minimization
DFA MinimizationDFA Minimization
DFA Minimization
 
Lecture4 lexical analysis2
Lecture4 lexical analysis2Lecture4 lexical analysis2
Lecture4 lexical analysis2
 
optimization of DFA
optimization of DFAoptimization of DFA
optimization of DFA
 
نموذج مسار هندسة الأفكار
نموذج مسار هندسة الأفكارنموذج مسار هندسة الأفكار
نموذج مسار هندسة الأفكار
 
هندسة الأفكار
هندسة الأفكارهندسة الأفكار
هندسة الأفكار
 

Similar to Lecture6 syntax analysis_2

6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
movocode
 

Similar to Lecture6 syntax analysis_2 (20)

Lecture8 syntax analysis_4
Lecture8 syntax analysis_4Lecture8 syntax analysis_4
Lecture8 syntax analysis_4
 
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
 
Left factor put
Left factor putLeft factor put
Left factor put
 
3b. LMD & RMD.pdf
3b. LMD & RMD.pdf3b. LMD & RMD.pdf
3b. LMD & RMD.pdf
 
04 Syntax Analysis.pdf
04 Syntax Analysis.pdf04 Syntax Analysis.pdf
04 Syntax Analysis.pdf
 
Assignment statements
Assignment statementsAssignment statements
Assignment statements
 
Assignment statements
Assignment statementsAssignment statements
Assignment statements
 
Learn from LL(1) to PEG parser the hard way
Learn from LL(1) to PEG parser the hard wayLearn from LL(1) to PEG parser the hard way
Learn from LL(1) to PEG parser the hard way
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniques
 
3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx
 
introduction to data structures and types
introduction to data structures and typesintroduction to data structures and types
introduction to data structures and types
 
Lecture7 syntax analysis_3
Lecture7 syntax analysis_3Lecture7 syntax analysis_3
Lecture7 syntax analysis_3
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Ch2 (1).ppt
Ch2 (1).pptCh2 (1).ppt
Ch2 (1).ppt
 
Lexical 2
Lexical 2Lexical 2
Lexical 2
 
Mathematical Modeling With Maple
Mathematical Modeling With MapleMathematical Modeling With Maple
Mathematical Modeling With Maple
 
Syntaxdirected
SyntaxdirectedSyntaxdirected
Syntaxdirected
 
Syntaxdirected
SyntaxdirectedSyntaxdirected
Syntaxdirected
 
Syntaxdirected (1)
Syntaxdirected (1)Syntaxdirected (1)
Syntaxdirected (1)
 
Algorithms summary (English version)
Algorithms summary (English version)Algorithms summary (English version)
Algorithms summary (English version)
 

More from Mahesh Kumar Chelimilla (13)

Lecture11 syntax analysis_7
Lecture11 syntax analysis_7Lecture11 syntax analysis_7
Lecture11 syntax analysis_7
 
Lecture10 syntax analysis_6
Lecture10 syntax analysis_6Lecture10 syntax analysis_6
Lecture10 syntax analysis_6
 
Lecture9 syntax analysis_5
Lecture9 syntax analysis_5Lecture9 syntax analysis_5
Lecture9 syntax analysis_5
 
Transportlayer tanenbaum
Transportlayer tanenbaumTransportlayer tanenbaum
Transportlayer tanenbaum
 
Network layer tanenbaum
Network layer tanenbaumNetwork layer tanenbaum
Network layer tanenbaum
 
Forouzan x25
Forouzan x25Forouzan x25
Forouzan x25
 
Forouzan ppp
Forouzan pppForouzan ppp
Forouzan ppp
 
Forouzan isdn
Forouzan isdnForouzan isdn
Forouzan isdn
 
Forouzan frame relay
Forouzan frame relayForouzan frame relay
Forouzan frame relay
 
Forouzan data link_2
Forouzan data link_2Forouzan data link_2
Forouzan data link_2
 
Forouzan data link_1
Forouzan data link_1Forouzan data link_1
Forouzan data link_1
 
Forouzan atm
Forouzan atmForouzan atm
Forouzan atm
 
Datalinklayer tanenbaum
Datalinklayer tanenbaumDatalinklayer tanenbaum
Datalinklayer tanenbaum
 

Recently uploaded

Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
dharasingh5698
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Recently uploaded (20)

(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 

Lecture6 syntax analysis_2

  • 2. Derivation • A derivation is the sequence of productions S … … … … • A derivation can be drawn as a tree• A derivation can be drawn as a tree – Start symbol is the tree’s root – For a production X Y1 … … Yn add children Y1 … … Yn to node X X Y1 … … Yn 28-Jan-15 2CS 346 Lecture 5
  • 3. Example: Left-most derivation • Grammar: E E + E | E * E | ( E ) | id • String : id*id+id E E E + E E + EE + E E + E E * E + E id * E + E E * E id id * id + E id * id + id id id Parse Tree 28-Jan-15 3CS 346 Lecture 5
  • 4. Parse Tree • A parse tree has – Terminals at the leaves – Non-terminal at the interior nodes • An in-order traversal of the leaves is the original input • The parse tree shows the association of operations, the input string does not 28-Jan-15 4CS 346 Lecture 5
  • 5. Right-most derivation • The example is a left- most derivation – At each step, replace the left most non terminals E E + E E * E + E • There is a equivalent notion of right-most derivation E * E + E id * E + E id * id + E id * id + id 28-Jan-15 5CS 346 Lecture 5
  • 6. Parse Tree Derivations • Note that the left-most and right-most derivation have the same parse tree • Every parse tree has the left-most and right-• Every parse tree has the left-most and right- most derivations 28-Jan-15 6CS 346 Lecture 5
  • 7. Ambiguity • Grammar: E E + E | E * E | ( E ) | id • String : id*id+id • This string has two parse trees E E E + E E * E E * E id id E + E id id id id 28-Jan-15 7CS 346 Lecture 5
  • 8. Ambiguous Grammar • A grammar is ambiguous if it has more than one parse tree for some string – Equivalently, there is more than one right-most or left-most derivation for some stringleft-most derivation for some string • Ambiguity means that some programs are ill- defined 28-Jan-15 8CS 346 Lecture 5
  • 9. How to handle Ambiguous Grammar • Several ways to handle • Rewrite the grammar unambiguously E E’+ E| E’ E’ id * E’| id| ( E ) * E | ( E ) • Enforce precedence of * over + 28-Jan-15 9CS 346 Lecture 5
  • 10. • Grammar: E E’+ E | E’ E’ id * E’ | id | ( E ) * E’ | ( E ) • String : id*id+id E Enforce precedence of * over + by divide the productions in two classes; one handle + and one handles *; so one non terminals for each operators How to handle Ambiguous Grammar E E’ + E id * E’ E’ id id non terminals for each operators E E’+ E E’ + E’ + E E’+E’+E’+E E’+… +E’ Handles + E’ id * E’ id*id*E’ id*id*id*E’ id*…*id Handles *28-Jan-15 10CS 346 Lecture 5
  • 11. How it works • The grammar has two separate groups of productions • All the pluses (+) must be generated before any of the times (*); so time(s) (*) are nested more deeply inside the parse tree; pluses (+) aredeeply inside the parse tree; pluses (+) are generated in the outermost levels and times (*) are generated inside the pluses (+). • So the grammar enforces that the time (*) has higher precedence than the plus (+) 28-Jan-15 11CS 346 Lecture 5
  • 12. Ambiguity • Grammar: E E + E | E * E | ( E ) | id • String : id*id+id • This string has two parse trees E E E + E E * E E * E id id E + E id id id id 28-Jan-15 12CS 346 Lecture 5
  • 13. Ambiguity • Grammar: E E + E | E * E | ( E ) | id • String : id*id+id • Now, this string has only one parse trees E E E + E E * E id id id E’ + E id * E’ E’ id id 28-Jan-15 13CS 346 Lecture 5
  • 14. Example • Another Expression E if E then E | if E then E else E | OTHER • The expression: if E1 then if E2 then E3 else E4 has two separate parse treesparse trees if if E1 if E4 E1 if E2 E3 E2 E3 E4 28-Jan-15 14CS 346 Lecture 5
  • 15. How to handle the Ambiguity • The property that we want is: else matches the closest unmatched then • Can be resolved as: E MIF| UIFE MIF| UIF MIF if E then MIF else MIF | OTHER UIF if E then E | if E MIF else UIF 28-Jan-15 15CS 346 Lecture 5
  • 16. Associativity of Operators • Let a Grammar : S S + S | S – S | 0 – 9 • Input: 9 – 5 + 2 S S S + S S - SS + S S - S S - S 2 9 S + S 9 5 5 2 (9-5)+2 = 6 9-(5+2) = 2 28-Jan-15 16CS 346 Lecture 5
  • 17. How to handle • When an operand like 5 has operators to its left and right, conventions are needed for deciding which operator applies to that operand. • Rule: operator + is associates to the left. Arithmetic• Rule: operator + is associates to the left. Arithmetic operators (+, -, *, /) are left associative. • Assignment operator (“=“) in C is right associative. 28-Jan-15 17CS 346 Lecture 5
  • 18. How to handle • Left Associative S S + d | S – d | d d [0-9] • Input: 9 – 5 + 2 S • Right Associative r l = r | l l [a-z] • Input: a = b = c r S + d S – d 2 d 5 9 Left-most derivation l = r a l = r b l c Right-most derivation 28-Jan-15 18CS 346 Lecture 5
  • 19. Push Down Automata • The language for balanced parenthesis, i.e. {( i ) i | i ≥ 0}, CFG productions are S (S)|S S ɛ • The situation can be handled if the DFA is• The situation can be handled if the DFA is augmented with memory – Memory implemented as a stack – Such an automata is called Push Down Automata (PDA) 28-Jan-15 19CS 346 Lecture 5