SlideShare a Scribd company logo
Lecture 7
Syntax Analysis III
Top Down Parsing
Top Down Parsing
• A top-down parser starts with the root of the parse tree, labeled with
the start or goal symbol of the grammar.
• To build a parse, it repeats the following steps until the fringe of the
parse tree matches the input string
1. At a node labeled A, select a production A α and construct the
appropriate child for each symbol of α
2. When a terminal is added to the fringe that does’nt match the input
string, backtrack (Some grammars are backtrack free (predictive))
3. Find the next node to be expanded
• The key is selecting the right production in step 1
– should be guided by input string
28-Jan-15 2CS 346 Lecture 5
Recursive Descent Parsing
• Parse tree is constructed
– From the top level non-terminal
– Try productions in order from left to right
• Terminals are seen in order of appearance in the token
stream.
• When productions fail, backtrack to try other alternatives
• Example:
– Consider the parse of the string: (int5)
– The grammar is : E T | T + E
T int | int * T | (E)
28-Jan-15 3CS 346 Lecture 5
Recursive Descent Parsing Algorithm
• TOKEN – type of tokens
– In our case, let the tokens be: INT, OPEN, CLOSE, PLUS,
TIMES
– *next – points to the next input token
• Define boolean functions that check for a match of:
– A given token terminal– A given token terminal
bool term(TOKEN tok) { return *next++ == tok; }
• The nth production of a particular non-terminal S:
bool Sn() { … }
• Try all productions of S:
bool S() { … }
28-Jan-15 4CS 346 Lecture 5
Recursive Descent Parsing Algorithm
• For production E T
bool E1() { return T(); }
• For production E T + E
bool E2() { return T() && term(PLUS) && E(); }
Functions for non-terminal ‘E’
[E T | T + E]
• For all productions of E (with backtracking)
bool E() {
TOKEN *save = next;
return (next = save, E1())
|| (next = save, E2());
}
28-Jan-15 5CS 346 Lecture 5
• Functions for non-terminal T : [T int | int * T | (E)]
– bool T1() { return term(INT); }
– bool T2() { return term(INT) && term(TIMES) && T(); }
– bool T3() { return term(OPEN) && E() && term(CLOSE);
}
Recursive Descent Parsing Algorithm
• bool T() {
TOKEN *save = next;
return (next = save, T1())
|| (next = save, T2())
|| (next = save, T3());
}
28-Jan-15 6CS 346 Lecture 5
• To start the parser
– Initialize next to
point to first token
– Invoke E()
bool term(TOKEN tok) { return *next++ == tok; }
bool E1() { return T(); }
bool E2() { return T() && term(PLUS) && E(); }
bool E() {
TOKEN *save = next;
return (next = save, E1()) || (next = save, E2());
}
Recursive Descent Parsing Algorithm
• Try parsing by hand:
– (int)
}
bool T1() { return term(INT); }
bool T2() { return term(INT) && term(TIMES) && T( ); }
bool T3() { return term(OPEN) && E() && term(CLOSE); }
bool T() {
TOKEN *save = next;
return (next = save, T1())
|| (next = save, T2())
|| (next = save, T3());
}
28-Jan-15 7CS 346 Lecture 5
Limitations of RD Parser
Grammar
E T | T + E
T int | int * T | (E)
Input String:
int * int
bool term(TOKEN tok) { return *next++ == tok; }
bool E1() { return T(); }
bool E2() { return T() && term(PLUS) && E(); }
bool E() {
TOKEN *save = next;
return (next = save, E1()) || (next = save, E2());
}
int * int
}
bool T1() { return term(INT); }
bool T2() { return term(INT) && term(TIMES) && T( ); }
bool T3() { return term(OPEN) && E() && term(CLOSE); }
bool T() {
TOKEN *save = next;
return (next = save, T1())
|| (next = save, T2())
|| (next = save, T3());
}
28-Jan-15 8CS 346 Lecture 5
Limitations
• If a production for non- terminal X succeedes
– Can’t backtrack to try a different production for X
later
• General recursive-descent algorithms supports
such “full” backtracking
– Can implement any grammar
28-Jan-15 9CS 346 Lecture 5
Countermeasures
• Discussed RD algorithm is not general
– But easy to implement by hand
• Sufficient for the grammars where for any non-
terminal at most one production can succeed.terminal at most one production can succeed.
• The example grammar can be rewritten to
work with the presented algorithm
– Left factoring
28-Jan-15 10CS 346 Lecture 5
Left Recursive Grammar
• Grammar: S Sa
– bool S1() {return S() && terminal (a);}
– bool S() { return S1();}
• S( ) goes into an infinite loop
• A left recursive grammar has a non-terminal S such that
S S α for some αS S α for some α
S Sa Saa Saaa … … Sa… …a
• Recursive Descent does not work in such cases.
• Consider the grammar: A A α| β
• A generates all string starting with a β and followed by any
number of α’s.
28-Jan-15 11CS 346 Lecture 5
Eliminating Left-Recursion
• Direct Left-Recursion:
A Aα | β A Aα1 | ... | Aαn | β1|...|βn
A β A' A β1 A' | ... | βn A'
A' α A' | ɛ A' α1A' | ... | αn A' | ɛ
A generates all the strings with a β
and followed by any number of α’s
All strings derived from A start with one of
β1… βn and continue with several instances of
α1 … αn
28-Jan-15 12CS 346 Lecture 5
• Indirect Left-Recursion
S A α | δ
A Sβ
• Algorithm:
1. Arrange the non-terminals in some order A1 ,...,An
2. for (i in 1..n) {
3. for (j in 1..i-1) {
Eliminating Left-Recursion
The grammar is also left recursive
because S + S β α
3. for (j in 1..i-1) {
4. replace each production of the form Ai Ajγ by the
productions Ai δ1γ | δ2γ |... | δkγ where Aj δ1 | δ2 |... | δk
5. }
6. eliminate the immediate left recursion among Ai productions
7. }
The above algorithm guaranteed to work if the grammar has no cycle [derivation of the form
A+ A or ɛ production A ɛ]. Cycles can be eliminated systematically from a grammar as
can ɛ productions.
28-Jan-15 13CS 346 Lecture 5
• S Aa | b
• A Ac | Sd | ɛ
• Out loop (2 to 7) eliminates any left recursion among A1
productions. Any remaining A1 productions of the form A1 Alα
must therefore have l > 1.
The grammar is also left recursive
because S + Sda
Eliminating Left-Recursion
• After i-1st iteration of the outer for loop, all non terminal Ak, k < i, is
cleaned i.e. any production Ak Alα, must have l>k
• At the ith iteration, inner loop 3to5, progressively raises the lower
limit in any productions Ai Amα, until we have m>i.
• Line 6, eliminating left recursion for Ai forces m to be greater than i
28-Jan-15 14CS 346 Lecture 5
Lecture7 syntax analysis_3

More Related Content

PDF
Lecture10 syntax analysis_6
PDF
Lecture8 syntax analysis_4
PDF
Lecture9 syntax analysis_5
PDF
Lecture6 syntax analysis_2
PPT
Lecture 05 syntax analysis 2
PPTX
LL(1) parsing
PPT
Ll(1) Parser in Compilers
PPTX
Lecture 07 08 syntax analysis-4
Lecture10 syntax analysis_6
Lecture8 syntax analysis_4
Lecture9 syntax analysis_5
Lecture6 syntax analysis_2
Lecture 05 syntax analysis 2
LL(1) parsing
Ll(1) Parser in Compilers
Lecture 07 08 syntax analysis-4

What's hot (20)

PPTX
First and follow set
PPT
Chapter 5 Syntax Directed Translation
PPT
Top down parsing
PDF
Topdown parsing
PPTX
Compiler: Syntax Analysis
PDF
Lecture11 syntax analysis_7
PPTX
Top down parsing(sid) (1)
PPTX
Parsing LL(1), SLR, LR(1)
PPT
Chapter Five(2)
PDF
PDF
07 top-down-parsing
PDF
Topdown parsing
PPT
Bottom - Up Parsing
PPTX
Top down parsing
DOC
Pcd(Mca)
DOCX
Nonrecursive predictive parsing
PPTX
LR(1) and SLR(1) parsing
PPT
Lecture 03 lexical analysis
PDF
Queues-handouts
First and follow set
Chapter 5 Syntax Directed Translation
Top down parsing
Topdown parsing
Compiler: Syntax Analysis
Lecture11 syntax analysis_7
Top down parsing(sid) (1)
Parsing LL(1), SLR, LR(1)
Chapter Five(2)
07 top-down-parsing
Topdown parsing
Bottom - Up Parsing
Top down parsing
Pcd(Mca)
Nonrecursive predictive parsing
LR(1) and SLR(1) parsing
Lecture 03 lexical analysis
Queues-handouts
Ad

Viewers also liked (16)

PPTX
Top Down Parsing, Predictive Parsing
PPTX
Parsing
PDF
Bottom up parser
PPTX
Cs419 lec11 bottom-up parsing
PPTX
What is symbol table?
PDF
Operator precedence
PPTX
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
PPTX
Parsing
PPT
Lecture 06 syntax analysis 3
PPT
Module 11
PPTX
Top down and botttom up Parsing
PPTX
compiler ppt on symbol table
DOCX
Assigment # 1 telecommunication system
PPTX
Comiler construction Notes
PPTX
Symbol table design (Compiler Construction)
Top Down Parsing, Predictive Parsing
Parsing
Bottom up parser
Cs419 lec11 bottom-up parsing
What is symbol table?
Operator precedence
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Parsing
Lecture 06 syntax analysis 3
Module 11
Top down and botttom up Parsing
compiler ppt on symbol table
Assigment # 1 telecommunication system
Comiler construction Notes
Symbol table design (Compiler Construction)
Ad

Similar to Lecture7 syntax analysis_3 (20)

PDF
Syntax Analysis PPTs for Third Year Computer Sc. and Engineering
PPTX
Compiler Design_Syntax Analyzer_Top Down Parsers.pptx
PPT
PARSING.ppt
PPTX
3. Syntax Analyzer.pptx
PPTX
Syntax Analysis.pptx
PDF
Left factor put
PPT
Parsing
PPTX
Natural Language Processing - Writing Grammar
PPTX
PARSER .pptx
PPT
51114.-Compiler-Design-Syntax-Analysis-Top-down.ppt
PPT
51114.-Compiler-Design-Syntax-Analysis-Top-down.ppt
PPTX
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
PPT
Integrated Fundamental and Technical Analysis of Select Public Sector Oil Com...
PPT
PPTX
Syntactic Analysis in Compiler Construction
PPT
Cd2 [autosaved]
PPT
Top_down_Parsing_ full_detail_explanation
PPT
Video lecture for bca
PDF
Topdown parsing
Syntax Analysis PPTs for Third Year Computer Sc. and Engineering
Compiler Design_Syntax Analyzer_Top Down Parsers.pptx
PARSING.ppt
3. Syntax Analyzer.pptx
Syntax Analysis.pptx
Left factor put
Parsing
Natural Language Processing - Writing Grammar
PARSER .pptx
51114.-Compiler-Design-Syntax-Analysis-Top-down.ppt
51114.-Compiler-Design-Syntax-Analysis-Top-down.ppt
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
Integrated Fundamental and Technical Analysis of Select Public Sector Oil Com...
Syntactic Analysis in Compiler Construction
Cd2 [autosaved]
Top_down_Parsing_ full_detail_explanation
Video lecture for bca
Topdown parsing

More from Mahesh Kumar Chelimilla (15)

PDF
Lecture5 syntax analysis_1
PDF
Lecture4 lexical analysis2
PDF
Lecture3 lexical analysis
PDF
Lecture2 general structure of a compiler
PDF
Lecture1 introduction compilers
PPT
Transportlayer tanenbaum
PPT
Network layer tanenbaum
PPT
PPT
PPT
PPT
Forouzan frame relay
PPT
Forouzan data link_2
PPT
Forouzan data link_1
PPT
PPT
Datalinklayer tanenbaum
Lecture5 syntax analysis_1
Lecture4 lexical analysis2
Lecture3 lexical analysis
Lecture2 general structure of a compiler
Lecture1 introduction compilers
Transportlayer tanenbaum
Network layer tanenbaum
Forouzan frame relay
Forouzan data link_2
Forouzan data link_1
Datalinklayer tanenbaum

Recently uploaded (20)

PPTX
Artificial Intelligence
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
PPT on Performance Review to get promotions
PPTX
Current and future trends in Computer Vision.pptx
PPT
Project quality management in manufacturing
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
web development for engineering and engineering
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PPTX
OOP with Java - Java Introduction (Basics)
PDF
composite construction of structures.pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Artificial Intelligence
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Internet of Things (IOT) - A guide to understanding
PPT on Performance Review to get promotions
Current and future trends in Computer Vision.pptx
Project quality management in manufacturing
Foundation to blockchain - A guide to Blockchain Tech
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
web development for engineering and engineering
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Model Code of Practice - Construction Work - 21102022 .pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
OOP with Java - Java Introduction (Basics)
composite construction of structures.pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS

Lecture7 syntax analysis_3

  • 1. Lecture 7 Syntax Analysis III Top Down Parsing
  • 2. Top Down Parsing • A top-down parser starts with the root of the parse tree, labeled with the start or goal symbol of the grammar. • To build a parse, it repeats the following steps until the fringe of the parse tree matches the input string 1. At a node labeled A, select a production A α and construct the appropriate child for each symbol of α 2. When a terminal is added to the fringe that does’nt match the input string, backtrack (Some grammars are backtrack free (predictive)) 3. Find the next node to be expanded • The key is selecting the right production in step 1 – should be guided by input string 28-Jan-15 2CS 346 Lecture 5
  • 3. Recursive Descent Parsing • Parse tree is constructed – From the top level non-terminal – Try productions in order from left to right • Terminals are seen in order of appearance in the token stream. • When productions fail, backtrack to try other alternatives • Example: – Consider the parse of the string: (int5) – The grammar is : E T | T + E T int | int * T | (E) 28-Jan-15 3CS 346 Lecture 5
  • 4. Recursive Descent Parsing Algorithm • TOKEN – type of tokens – In our case, let the tokens be: INT, OPEN, CLOSE, PLUS, TIMES – *next – points to the next input token • Define boolean functions that check for a match of: – A given token terminal– A given token terminal bool term(TOKEN tok) { return *next++ == tok; } • The nth production of a particular non-terminal S: bool Sn() { … } • Try all productions of S: bool S() { … } 28-Jan-15 4CS 346 Lecture 5
  • 5. Recursive Descent Parsing Algorithm • For production E T bool E1() { return T(); } • For production E T + E bool E2() { return T() && term(PLUS) && E(); } Functions for non-terminal ‘E’ [E T | T + E] • For all productions of E (with backtracking) bool E() { TOKEN *save = next; return (next = save, E1()) || (next = save, E2()); } 28-Jan-15 5CS 346 Lecture 5
  • 6. • Functions for non-terminal T : [T int | int * T | (E)] – bool T1() { return term(INT); } – bool T2() { return term(INT) && term(TIMES) && T(); } – bool T3() { return term(OPEN) && E() && term(CLOSE); } Recursive Descent Parsing Algorithm • bool T() { TOKEN *save = next; return (next = save, T1()) || (next = save, T2()) || (next = save, T3()); } 28-Jan-15 6CS 346 Lecture 5
  • 7. • To start the parser – Initialize next to point to first token – Invoke E() bool term(TOKEN tok) { return *next++ == tok; } bool E1() { return T(); } bool E2() { return T() && term(PLUS) && E(); } bool E() { TOKEN *save = next; return (next = save, E1()) || (next = save, E2()); } Recursive Descent Parsing Algorithm • Try parsing by hand: – (int) } bool T1() { return term(INT); } bool T2() { return term(INT) && term(TIMES) && T( ); } bool T3() { return term(OPEN) && E() && term(CLOSE); } bool T() { TOKEN *save = next; return (next = save, T1()) || (next = save, T2()) || (next = save, T3()); } 28-Jan-15 7CS 346 Lecture 5
  • 8. Limitations of RD Parser Grammar E T | T + E T int | int * T | (E) Input String: int * int bool term(TOKEN tok) { return *next++ == tok; } bool E1() { return T(); } bool E2() { return T() && term(PLUS) && E(); } bool E() { TOKEN *save = next; return (next = save, E1()) || (next = save, E2()); } int * int } bool T1() { return term(INT); } bool T2() { return term(INT) && term(TIMES) && T( ); } bool T3() { return term(OPEN) && E() && term(CLOSE); } bool T() { TOKEN *save = next; return (next = save, T1()) || (next = save, T2()) || (next = save, T3()); } 28-Jan-15 8CS 346 Lecture 5
  • 9. Limitations • If a production for non- terminal X succeedes – Can’t backtrack to try a different production for X later • General recursive-descent algorithms supports such “full” backtracking – Can implement any grammar 28-Jan-15 9CS 346 Lecture 5
  • 10. Countermeasures • Discussed RD algorithm is not general – But easy to implement by hand • Sufficient for the grammars where for any non- terminal at most one production can succeed.terminal at most one production can succeed. • The example grammar can be rewritten to work with the presented algorithm – Left factoring 28-Jan-15 10CS 346 Lecture 5
  • 11. Left Recursive Grammar • Grammar: S Sa – bool S1() {return S() && terminal (a);} – bool S() { return S1();} • S( ) goes into an infinite loop • A left recursive grammar has a non-terminal S such that S S α for some αS S α for some α S Sa Saa Saaa … … Sa… …a • Recursive Descent does not work in such cases. • Consider the grammar: A A α| β • A generates all string starting with a β and followed by any number of α’s. 28-Jan-15 11CS 346 Lecture 5
  • 12. Eliminating Left-Recursion • Direct Left-Recursion: A Aα | β A Aα1 | ... | Aαn | β1|...|βn A β A' A β1 A' | ... | βn A' A' α A' | ɛ A' α1A' | ... | αn A' | ɛ A generates all the strings with a β and followed by any number of α’s All strings derived from A start with one of β1… βn and continue with several instances of α1 … αn 28-Jan-15 12CS 346 Lecture 5
  • 13. • Indirect Left-Recursion S A α | δ A Sβ • Algorithm: 1. Arrange the non-terminals in some order A1 ,...,An 2. for (i in 1..n) { 3. for (j in 1..i-1) { Eliminating Left-Recursion The grammar is also left recursive because S + S β α 3. for (j in 1..i-1) { 4. replace each production of the form Ai Ajγ by the productions Ai δ1γ | δ2γ |... | δkγ where Aj δ1 | δ2 |... | δk 5. } 6. eliminate the immediate left recursion among Ai productions 7. } The above algorithm guaranteed to work if the grammar has no cycle [derivation of the form A+ A or ɛ production A ɛ]. Cycles can be eliminated systematically from a grammar as can ɛ productions. 28-Jan-15 13CS 346 Lecture 5
  • 14. • S Aa | b • A Ac | Sd | ɛ • Out loop (2 to 7) eliminates any left recursion among A1 productions. Any remaining A1 productions of the form A1 Alα must therefore have l > 1. The grammar is also left recursive because S + Sda Eliminating Left-Recursion • After i-1st iteration of the outer for loop, all non terminal Ak, k < i, is cleaned i.e. any production Ak Alα, must have l>k • At the ith iteration, inner loop 3to5, progressively raises the lower limit in any productions Ai Amα, until we have m>i. • Line 6, eliminating left recursion for Ai forces m to be greater than i 28-Jan-15 14CS 346 Lecture 5