SlideShare a Scribd company logo
1 of 15
Download to read offline
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

What's hot (20)

First and follow set
First and follow setFirst and follow set
First and follow set
 
COMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax AnalysisCOMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax Analysis
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Compiler: Syntax Analysis
Compiler: Syntax AnalysisCompiler: Syntax Analysis
Compiler: Syntax Analysis
 
Lecture11 syntax analysis_7
Lecture11 syntax analysis_7Lecture11 syntax analysis_7
Lecture11 syntax analysis_7
 
Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)
 
Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)
 
Chapter Five(2)
Chapter Five(2)Chapter Five(2)
Chapter Five(2)
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
07 top-down-parsing
07 top-down-parsing07 top-down-parsing
07 top-down-parsing
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Pcd(Mca)
Pcd(Mca)Pcd(Mca)
Pcd(Mca)
 
Nonrecursive predictive parsing
Nonrecursive predictive parsingNonrecursive predictive parsing
Nonrecursive predictive parsing
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
 
Lecture 03 lexical analysis
Lecture 03 lexical analysisLecture 03 lexical analysis
Lecture 03 lexical analysis
 
Queues-handouts
Queues-handoutsQueues-handouts
Queues-handouts
 

Viewers also liked

Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTanzeela_Hussain
 
Symbol table format
Symbol table formatSymbol table format
Symbol table formatJK Knowledge
 
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolCompiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolMashaelQ
 
Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Iffat Anjum
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up ParsingGerwin Ocsena
 
compiler ppt on symbol table
 compiler ppt on symbol table compiler ppt on symbol table
compiler ppt on symbol tablenadarmispapaulraj
 
Assigment # 1 telecommunication system
Assigment # 1 telecommunication systemAssigment # 1 telecommunication system
Assigment # 1 telecommunication systemNadeem Khan
 
Comiler construction Notes
Comiler construction NotesComiler construction Notes
Comiler construction NotesNadeem Khan
 
Symbol table design (Compiler Construction)
Symbol table design (Compiler Construction)Symbol table design (Compiler Construction)
Symbol table design (Compiler Construction)Tech_MX
 

Viewers also liked (16)

Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
Parsing
ParsingParsing
Parsing
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Cs419 lec11 bottom-up parsing
Cs419 lec11   bottom-up parsingCs419 lec11   bottom-up parsing
Cs419 lec11 bottom-up parsing
 
Symbol table format
Symbol table formatSymbol table format
Symbol table format
 
What is symbol table?
What is symbol table?What is symbol table?
What is symbol table?
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolCompiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
 
Parsing
ParsingParsing
Parsing
 
Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3
 
Module 11
Module 11Module 11
Module 11
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
 
compiler ppt on symbol table
 compiler ppt on symbol table compiler ppt on symbol table
compiler ppt on symbol table
 
Assigment # 1 telecommunication system
Assigment # 1 telecommunication systemAssigment # 1 telecommunication system
Assigment # 1 telecommunication system
 
Comiler construction Notes
Comiler construction NotesComiler construction Notes
Comiler construction Notes
 
Symbol table design (Compiler Construction)
Symbol table design (Compiler Construction)Symbol table design (Compiler Construction)
Symbol table design (Compiler Construction)
 

Similar to Lecture7 syntax analysis_3

Similar to Lecture7 syntax analysis_3 (20)

Left factor put
Left factor putLeft factor put
Left factor put
 
3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniques
 
Lecture5 syntax analysis_1
Lecture5 syntax analysis_1Lecture5 syntax analysis_1
Lecture5 syntax analysis_1
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
 
Lecture4 lexical analysis2
Lecture4 lexical analysis2Lecture4 lexical analysis2
Lecture4 lexical analysis2
 
LL(1) Parsers
LL(1) ParsersLL(1) Parsers
LL(1) Parsers
 
Parsing
ParsingParsing
Parsing
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
 
compiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.pptcompiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.ppt
 
Ch4a
Ch4aCh4a
Ch4a
 
Theory of automata and formal language lab manual
Theory of automata and formal language lab manualTheory of automata and formal language lab manual
Theory of automata and formal language lab manual
 
Brief introduction to Algorithm analysis
Brief introduction to Algorithm analysis Brief introduction to Algorithm analysis
Brief introduction to Algorithm analysis
 
Ch2 (1).ppt
Ch2 (1).pptCh2 (1).ppt
Ch2 (1).ppt
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysis
 
ALF 5 - Parser Top-Down
ALF 5 - Parser Top-DownALF 5 - Parser Top-Down
ALF 5 - Parser Top-Down
 

More from Mahesh Kumar Chelimilla (13)

Lecture3 lexical analysis
Lecture3 lexical analysisLecture3 lexical analysis
Lecture3 lexical analysis
 
Lecture2 general structure of a compiler
Lecture2 general structure of a compilerLecture2 general structure of a compiler
Lecture2 general structure of a compiler
 
Lecture1 introduction compilers
Lecture1 introduction compilersLecture1 introduction compilers
Lecture1 introduction compilers
 
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 Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 

Recently uploaded (20)

Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 

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