SlideShare a Scribd company logo
1 of 23
Download to read offline
Syntax Analysis IV
Lecture 8
Syntax Analysis IV
Predictive Parsing
Predictive Parsing
• Like recursive-descent but parser can “predict” which
production to use
– By looking at the next few tokens (lookahead)
– No backtracking
• Predictive parsers restrict CFGs and accept LL(k)
grammars
– 1st L – The input is scanned from left to right– 1st L – The input is scanned from left to right
– 2nd L – Create left-most derivation
– k – Number of symbols of look-ahead
• Most parsers work with one symbol of look-ahead
[LL(1)]
• Informally, LL(1) has no left-recursion and has been
left-factored.
28-Jan-15 2CS 346 Lecture 5
Left Factoring
• At each step, only one choice of production
• Given the grammar:
E T + E | T
T int | int * T | ( E )
• Hard to predict which production to use because of common prefixes.
• We need to left-factor the grammar
• The Left-factored grammar:
E T X
X + E | ɛ
T ( E ) | int Y
Y * T | ɛ
28-Jan-15 3CS 346 Lecture 5
LL(1) Parsing
• Left factored Grammar:
E T X T ( E ) | int Y
X + E | ɛ Y * T | ɛ
• LL(1) parsing table: Next input token
int * + ( ) $
E TX TX
X +E ɛ ɛ
T int Y (E)
Y *T ɛ ɛ ɛLeftmost non-terminal RHS of production to use
28-Jan-15 4CS 346 Lecture 5
• For the leftmost non-terminal X
• We look at the next input token a
• Makes use of an explicit parsing table of the form M[X, a]
• An explicit external stack records frontier of the parse tree
Predictive Parsing Algorithm
• An explicit external stack records frontier of the parse tree
– Non-terminals that have yet to be expanded
– Terminals that have yet to matched against the input
– Top of stack = leftmost pending terminal or non-terminal
• Reject on reaching error state
• Accept on end of input & empty stack
28-Jan-15 5CS 346 Lecture 5
• The parse table entry M[X, a] indicates which production to use if the
top of the stack is a non-terminal ‘X’ and the current input token is ‘a’.
• In that case ‘POP X’ from the stack and ‘PUSH’ all the RHS symbols
of the production M[X, a] in reverse order.
• Assume that '$' is a special token that is at the bottom of the stack and
terminates the input string
Predictive Parsing Algorithm
terminates the input string
if X = a = $ then halt
if X = a $ then pop(x) and ip++
if X is a non terminal
then if M[X, a] = {X UVW}
then begin pop(X); push(W,V,U)
end
else error
28-Jan-15 6CS 346 Lecture 5
LL(1) Parsing
STACK INPUT ACTION
E$ int * int $ TX
TX$ int * int $ intY
intYX$ int * int $ Terminal
(match)
YX$ *int$ *T
*TX$ *int$ Terminal
int * + ( ) $
E TX TX
X +E ɛ ɛ
T int Y (E)
Y *T ɛ ɛ ɛ
E*TX$ *int$ Terminal
(match)
TX$ int$ intY
intYX$ int$ Terminal
(match)
YX$ $ ɛ
X$ $ ɛ
$ $ Accept
E
T X
int Y ɛ
* T
int Y
Ɛ28-Jan-15 7CS 346 Lecture 5
Construction of Parsing Table
• Given the production A α, and a token t
• M[A, t] = α occurs in two cases:
1. If α *tβ
• α can derive a ‘t’ in the first position in 0 or more moves
• We say that t ϵ First(α)• We say that t ϵ First(α)
– ‘t’ is one of the terminals that can produce in the first position
2. If A α and α *ɛ and X * β Atδ
• Stack has A, input is t, and A cannot derive t
• In this case only option is to get rid of A (by deriving )
• Can work only if ‘t’ follows A in at least one derivation
• We say t ϵ Follow(A)
28-Jan-15 8CS 346 Lecture 5
First Sets
• Definition
– First(X) = { t | t is a terminal and X *tα} or {ɛ|X *ɛ}
• To build FIRST(X):
1. If X ϵ terminal, then FIRST(X) is {X}
2. If ((X ɛ) or ((X Y1…Yk) and (ɛ ϵ [Ɐi: 1 < i ≤ k First (Yi)]))
then add ɛ to FIRST(X)
2. If ((X ɛ) or ((X Y1…Yk) and (ɛ ϵ [Ɐi: 1 < i ≤ k First (Yi)]))
then add ɛ to FIRST(X)
3. If ((X Y1Y2…Yk α) and (ɛ ϵ [Ɐi: 1 < i ≤ k First (Yi)]))
then add FIRST(α) to FIRST(X)
• Find the first sets in the grammar:
E TX X + E | ɛ
T ( E ) | int Y Y * T | ɛ
28-Jan-15 9CS 346 Lecture 5
E TX X + E | ɛ
T ( E ) | int Y Y * T | ɛ
• First (+)={+}
• First (E)=First(T)
First Sets
• First (*)={*}
• First (()={(}
• First ())={)}
• First (int)={int}
• First (E)=First(T)
• First (T) = {(, int}
• First(X) = {+, ɛ}
• First (Y) = {*, ɛ}
28-Jan-15 10CS 346 Lecture 5
Follow Set
• Definition:
– Follow(X) = { t | S * αXtβ}
– ‘t’ is said to follow of ‘X’ if we can obtain a sentential form
where the terminal ‘t’ comes immediately after ‘X’
• Follow set for a given symbol never concerns what the
symbol can generate, but depends on where that symbolsymbol can generate, but depends on where that symbol
can appear in the derivations.
• If (X AB) then
– First(B) is in Follow(A) and
– Follow(X) is in Follow(B)
– If (B *ɛ ) then
• Follow(X) is in Follow(A)
28-Jan-15 11CS 346 Lecture 5
• To build FOLLOW(X)
1. Add $ to FOLLOW(S) [ If S is the start symbol]
2. If (A αBβ), then
Add everything in FIRST(β) except ɛ to FOLLOW(B)
3. If ((A αBβ and β *ɛ) or (A αB))
Add everything in FOLLOW(A) to FOLLOW(B)
Follow Set
• ɛ never appears in Follow sets, so Follow sets are just sets
of terminals
• Find the follow sets in the grammar:
E T X X + E | ɛ
T ( E ) | int Y Y * T | ɛ
28-Jan-15 12CS 346 Lecture 5
Parsing Table Construction
• for each production A α {
• for each terminal ‘t’ in FIRST(α)
• M[A, t] = α;
• if ɛ is in FIRST(α) , then
• for each terminal ‘b’ (including ‘$’) in Follow (A)• for each terminal ‘b’ (including ‘$’) in Follow (A)
• M[A, b] = α;
}
• Find the follow sets in the grammar:
E T X X + E | ɛ
T ( E ) | int Y Y * T | ɛ
28-Jan-15 13CS 346 Lecture 5
Recognizing LL(1) Grammars
• Consider the grammar X Xa | b
• First (X) = {b} Follow(X) = {$, a}
• Parsing Table:
Multiply defined entry:a b $
X b
Xa
Multiply defined entry:
This is how we know that
the grammar is not LL(1)
28-Jan-15 14CS 346 Lecture 5
• Formally, a grammar is LL(1) iff:
– It is not left-recursive
– Not ambiguous
– A grammar G is LL(1) iff whenever A u | v are two distinct productions
of G, the following conditions hold:
Recognizing LL(1) Grammars
• For no terminal ‘a’ do both ‘u’ and ‘v’ derive strings beginning with ‘a’ (i.e., FIRST
sets are disjoint)
• At most one of ‘u’ and ‘v’ can derive the empty string
• if v * ɛ then ‘u’ does not derive any string beginning with a terminal in
Follow(A) (i.e., if ɛ ϵ FIRST(v), then FIRST(u) and FOLLOW(A) are disjoint sets)
Most programming languages are not LL(1)
28-Jan-15 15CS 346 Lecture 5
Error Handling
• Stop at the first error and print a message
– Compiler writer friendly
– But not user friendly
• Every reasonable compiler must recover from error and
identify as many errors as possible
• However, multiple error messages (cascaded spurious error
messages) due to a single fault must be avoided
• Error recovery methods
– Panic mode
– Phrase level recovery
– Error productions
– Global correction
28-Jan-15 16CS 346 Lecture 5
Panic Mode
• Simplest and the most popular method
• Most tools provide for specifying panic mode
recovery in the grammar
• When an error is detected• When an error is detected
– Discard tokens until one with a clear role is found
– Continue from there
• Looking for synchronizing tokens
– Typically the statement or expression terminators
28-Jan-15 17CS 346 Lecture 5
• Consider following code
{
a = b + c;
d = m n ;
e = d - f;
Syntax Error
Panic Mode
e = d - f;
}
• The second expression has syntax error
– Skip ahead to next ‘;’ and try to parse the next expression
• It discards one expression and tries to continue parsing
28-Jan-15 18CS 346 Lecture 5
Phrase Level Recovery
• Make local correction to the input
– Eg: Fill in the blank entries in the predictive parsing
table with pointers to error routines
• Works only in limited situations
– A common programming error which is easily detected– A common programming error which is easily detected
– For example insert a ‘;’ after closing ‘}’ of a class
definition
• Does not work very well when the actual error has
occurred before the point of detection
28-Jan-15 19CS 346 Lecture 5
Error Productions
• Add erroneous constructs as productions in the
grammar
• Works only for most common mistakes which can be
easily identified
• Essentially makes common errors as part of the• Essentially makes common errors as part of the
grammar
• Complicates the grammar and does not work very well
• Eg:
– Write 5 x instead of 5 * x
– Add the production E … | E E
28-Jan-15 20CS 346 Lecture 5
Global Corrections
• Idea: find a correct “nearby” program
– Try token insertions and deletions
– Nearness may be measured using certain metric (Edit
Distance)
– Exhaustive search
• PL/C compiler implemented this scheme: anything• PL/C compiler implemented this scheme: anything
could be compiled.
• Disadvantages:
– Complicated - hard to implement
– Slows down parsing of correct programs
– “Nearby” is not necessarily “the intended” program
28-Jan-15 21CS 346 Lecture 5
Error Recovery in LL(1) Parser
• Error occurs when a parse table entry M[A, a] is empty or
terminal on stack-top do not match with input.
• Skip symbols in the input until a token in a selected set (synch)
appears
• Place symbols in follow(A) in synch set. Skip tokens until an
element in follow(A) is seen. Pop(A) and continue parsing
• Add symbol in first(A) in synch set. Then it may be possible to
resume parsing according to A if a symbol in first(A) appears
in input.
28-Jan-15 22CS 346 Lecture 5
Lecture8 syntax analysis_4

More Related Content

What's hot

What's hot (20)

Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
LL(1) parsing
LL(1) parsingLL(1) parsing
LL(1) parsing
 
Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
First and follow set
First and follow setFirst and follow set
First and follow set
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 
Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
 
Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)
 
Lecture11 syntax analysis_7
Lecture11 syntax analysis_7Lecture11 syntax analysis_7
Lecture11 syntax analysis_7
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Ch5a
Ch5aCh5a
Ch5a
 
Nonrecursive predictive parsing
Nonrecursive predictive parsingNonrecursive predictive parsing
Nonrecursive predictive parsing
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Predictive parser
Predictive parserPredictive parser
Predictive parser
 
Optimization of dfa
Optimization of dfaOptimization of dfa
Optimization of dfa
 

Viewers also liked (10)

Syntax
SyntaxSyntax
Syntax
 
Syntax & Stylistics 2
Syntax & Stylistics 2Syntax & Stylistics 2
Syntax & Stylistics 2
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3
 
Parsing
ParsingParsing
Parsing
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Module 11
Module 11Module 11
Module 11
 
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
 
Morphology exercises
Morphology exercisesMorphology exercises
Morphology exercises
 

Similar to Lecture8 syntax analysis_4

Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTCh4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
FutureTechnologies3
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
Kumar
 

Similar to Lecture8 syntax analysis_4 (20)

Left factor put
Left factor putLeft factor put
Left factor put
 
LL(1) Parsers
LL(1) ParsersLL(1) Parsers
LL(1) Parsers
 
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTCh4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
 
3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx
 
Parsing
ParsingParsing
Parsing
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniques
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
 
Stack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADTStack and its Applications : Data Structures ADT
Stack and its Applications : Data Structures ADT
 
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
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
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
 
Ch2 (1).ppt
Ch2 (1).pptCh2 (1).ppt
Ch2 (1).ppt
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"
 
compiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.pptcompiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.ppt
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
DS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptxDS MOD2 (1) (1).pptx
DS MOD2 (1) (1).pptx
 

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 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
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
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
 
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
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
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
 
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...
 
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
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
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
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
(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
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.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
 
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
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
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
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
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 in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 

Lecture8 syntax analysis_4

  • 1. Syntax Analysis IV Lecture 8 Syntax Analysis IV Predictive Parsing
  • 2. Predictive Parsing • Like recursive-descent but parser can “predict” which production to use – By looking at the next few tokens (lookahead) – No backtracking • Predictive parsers restrict CFGs and accept LL(k) grammars – 1st L – The input is scanned from left to right– 1st L – The input is scanned from left to right – 2nd L – Create left-most derivation – k – Number of symbols of look-ahead • Most parsers work with one symbol of look-ahead [LL(1)] • Informally, LL(1) has no left-recursion and has been left-factored. 28-Jan-15 2CS 346 Lecture 5
  • 3. Left Factoring • At each step, only one choice of production • Given the grammar: E T + E | T T int | int * T | ( E ) • Hard to predict which production to use because of common prefixes. • We need to left-factor the grammar • The Left-factored grammar: E T X X + E | ɛ T ( E ) | int Y Y * T | ɛ 28-Jan-15 3CS 346 Lecture 5
  • 4. LL(1) Parsing • Left factored Grammar: E T X T ( E ) | int Y X + E | ɛ Y * T | ɛ • LL(1) parsing table: Next input token int * + ( ) $ E TX TX X +E ɛ ɛ T int Y (E) Y *T ɛ ɛ ɛLeftmost non-terminal RHS of production to use 28-Jan-15 4CS 346 Lecture 5
  • 5. • For the leftmost non-terminal X • We look at the next input token a • Makes use of an explicit parsing table of the form M[X, a] • An explicit external stack records frontier of the parse tree Predictive Parsing Algorithm • An explicit external stack records frontier of the parse tree – Non-terminals that have yet to be expanded – Terminals that have yet to matched against the input – Top of stack = leftmost pending terminal or non-terminal • Reject on reaching error state • Accept on end of input & empty stack 28-Jan-15 5CS 346 Lecture 5
  • 6. • The parse table entry M[X, a] indicates which production to use if the top of the stack is a non-terminal ‘X’ and the current input token is ‘a’. • In that case ‘POP X’ from the stack and ‘PUSH’ all the RHS symbols of the production M[X, a] in reverse order. • Assume that '$' is a special token that is at the bottom of the stack and terminates the input string Predictive Parsing Algorithm terminates the input string if X = a = $ then halt if X = a $ then pop(x) and ip++ if X is a non terminal then if M[X, a] = {X UVW} then begin pop(X); push(W,V,U) end else error 28-Jan-15 6CS 346 Lecture 5
  • 7. LL(1) Parsing STACK INPUT ACTION E$ int * int $ TX TX$ int * int $ intY intYX$ int * int $ Terminal (match) YX$ *int$ *T *TX$ *int$ Terminal int * + ( ) $ E TX TX X +E ɛ ɛ T int Y (E) Y *T ɛ ɛ ɛ E*TX$ *int$ Terminal (match) TX$ int$ intY intYX$ int$ Terminal (match) YX$ $ ɛ X$ $ ɛ $ $ Accept E T X int Y ɛ * T int Y Ɛ28-Jan-15 7CS 346 Lecture 5
  • 8. Construction of Parsing Table • Given the production A α, and a token t • M[A, t] = α occurs in two cases: 1. If α *tβ • α can derive a ‘t’ in the first position in 0 or more moves • We say that t ϵ First(α)• We say that t ϵ First(α) – ‘t’ is one of the terminals that can produce in the first position 2. If A α and α *ɛ and X * β Atδ • Stack has A, input is t, and A cannot derive t • In this case only option is to get rid of A (by deriving ) • Can work only if ‘t’ follows A in at least one derivation • We say t ϵ Follow(A) 28-Jan-15 8CS 346 Lecture 5
  • 9. First Sets • Definition – First(X) = { t | t is a terminal and X *tα} or {ɛ|X *ɛ} • To build FIRST(X): 1. If X ϵ terminal, then FIRST(X) is {X} 2. If ((X ɛ) or ((X Y1…Yk) and (ɛ ϵ [Ɐi: 1 < i ≤ k First (Yi)])) then add ɛ to FIRST(X) 2. If ((X ɛ) or ((X Y1…Yk) and (ɛ ϵ [Ɐi: 1 < i ≤ k First (Yi)])) then add ɛ to FIRST(X) 3. If ((X Y1Y2…Yk α) and (ɛ ϵ [Ɐi: 1 < i ≤ k First (Yi)])) then add FIRST(α) to FIRST(X) • Find the first sets in the grammar: E TX X + E | ɛ T ( E ) | int Y Y * T | ɛ 28-Jan-15 9CS 346 Lecture 5
  • 10. E TX X + E | ɛ T ( E ) | int Y Y * T | ɛ • First (+)={+} • First (E)=First(T) First Sets • First (*)={*} • First (()={(} • First ())={)} • First (int)={int} • First (E)=First(T) • First (T) = {(, int} • First(X) = {+, ɛ} • First (Y) = {*, ɛ} 28-Jan-15 10CS 346 Lecture 5
  • 11. Follow Set • Definition: – Follow(X) = { t | S * αXtβ} – ‘t’ is said to follow of ‘X’ if we can obtain a sentential form where the terminal ‘t’ comes immediately after ‘X’ • Follow set for a given symbol never concerns what the symbol can generate, but depends on where that symbolsymbol can generate, but depends on where that symbol can appear in the derivations. • If (X AB) then – First(B) is in Follow(A) and – Follow(X) is in Follow(B) – If (B *ɛ ) then • Follow(X) is in Follow(A) 28-Jan-15 11CS 346 Lecture 5
  • 12. • To build FOLLOW(X) 1. Add $ to FOLLOW(S) [ If S is the start symbol] 2. If (A αBβ), then Add everything in FIRST(β) except ɛ to FOLLOW(B) 3. If ((A αBβ and β *ɛ) or (A αB)) Add everything in FOLLOW(A) to FOLLOW(B) Follow Set • ɛ never appears in Follow sets, so Follow sets are just sets of terminals • Find the follow sets in the grammar: E T X X + E | ɛ T ( E ) | int Y Y * T | ɛ 28-Jan-15 12CS 346 Lecture 5
  • 13. Parsing Table Construction • for each production A α { • for each terminal ‘t’ in FIRST(α) • M[A, t] = α; • if ɛ is in FIRST(α) , then • for each terminal ‘b’ (including ‘$’) in Follow (A)• for each terminal ‘b’ (including ‘$’) in Follow (A) • M[A, b] = α; } • Find the follow sets in the grammar: E T X X + E | ɛ T ( E ) | int Y Y * T | ɛ 28-Jan-15 13CS 346 Lecture 5
  • 14. Recognizing LL(1) Grammars • Consider the grammar X Xa | b • First (X) = {b} Follow(X) = {$, a} • Parsing Table: Multiply defined entry:a b $ X b Xa Multiply defined entry: This is how we know that the grammar is not LL(1) 28-Jan-15 14CS 346 Lecture 5
  • 15. • Formally, a grammar is LL(1) iff: – It is not left-recursive – Not ambiguous – A grammar G is LL(1) iff whenever A u | v are two distinct productions of G, the following conditions hold: Recognizing LL(1) Grammars • For no terminal ‘a’ do both ‘u’ and ‘v’ derive strings beginning with ‘a’ (i.e., FIRST sets are disjoint) • At most one of ‘u’ and ‘v’ can derive the empty string • if v * ɛ then ‘u’ does not derive any string beginning with a terminal in Follow(A) (i.e., if ɛ ϵ FIRST(v), then FIRST(u) and FOLLOW(A) are disjoint sets) Most programming languages are not LL(1) 28-Jan-15 15CS 346 Lecture 5
  • 16. Error Handling • Stop at the first error and print a message – Compiler writer friendly – But not user friendly • Every reasonable compiler must recover from error and identify as many errors as possible • However, multiple error messages (cascaded spurious error messages) due to a single fault must be avoided • Error recovery methods – Panic mode – Phrase level recovery – Error productions – Global correction 28-Jan-15 16CS 346 Lecture 5
  • 17. Panic Mode • Simplest and the most popular method • Most tools provide for specifying panic mode recovery in the grammar • When an error is detected• When an error is detected – Discard tokens until one with a clear role is found – Continue from there • Looking for synchronizing tokens – Typically the statement or expression terminators 28-Jan-15 17CS 346 Lecture 5
  • 18. • Consider following code { a = b + c; d = m n ; e = d - f; Syntax Error Panic Mode e = d - f; } • The second expression has syntax error – Skip ahead to next ‘;’ and try to parse the next expression • It discards one expression and tries to continue parsing 28-Jan-15 18CS 346 Lecture 5
  • 19. Phrase Level Recovery • Make local correction to the input – Eg: Fill in the blank entries in the predictive parsing table with pointers to error routines • Works only in limited situations – A common programming error which is easily detected– A common programming error which is easily detected – For example insert a ‘;’ after closing ‘}’ of a class definition • Does not work very well when the actual error has occurred before the point of detection 28-Jan-15 19CS 346 Lecture 5
  • 20. Error Productions • Add erroneous constructs as productions in the grammar • Works only for most common mistakes which can be easily identified • Essentially makes common errors as part of the• Essentially makes common errors as part of the grammar • Complicates the grammar and does not work very well • Eg: – Write 5 x instead of 5 * x – Add the production E … | E E 28-Jan-15 20CS 346 Lecture 5
  • 21. Global Corrections • Idea: find a correct “nearby” program – Try token insertions and deletions – Nearness may be measured using certain metric (Edit Distance) – Exhaustive search • PL/C compiler implemented this scheme: anything• PL/C compiler implemented this scheme: anything could be compiled. • Disadvantages: – Complicated - hard to implement – Slows down parsing of correct programs – “Nearby” is not necessarily “the intended” program 28-Jan-15 21CS 346 Lecture 5
  • 22. Error Recovery in LL(1) Parser • Error occurs when a parse table entry M[A, a] is empty or terminal on stack-top do not match with input. • Skip symbols in the input until a token in a selected set (synch) appears • Place symbols in follow(A) in synch set. Skip tokens until an element in follow(A) is seen. Pop(A) and continue parsing • Add symbol in first(A) in synch set. Then it may be possible to resume parsing according to A if a symbol in first(A) appears in input. 28-Jan-15 22CS 346 Lecture 5