SlideShare uma empresa Scribd logo
1 de 23
Baixar para ler 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

Mais conteúdo relacionado

Mais procurados (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
 

Destaque

Destaque (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
 

Semelhante a Lecture8 syntax analysis_4

CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniquesd72994185
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxchandankumar364348
 
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 ADTSoumen Santra
 
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 manualNitesh Dubey
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7Kumar
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler DesignAkhil Kaushik
 
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 wayKir Chou
 
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-9sumitbardhan
 
Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Algorithms - "Chapter 2 getting started"
Algorithms - "Chapter 2 getting started"Ra'Fat Al-Msie'deen
 
compiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.pptcompiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.pptSheikhMuhammadSaad3
 
New compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfNew compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfeliasabdi2024
 

Semelhante a 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
 
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
 
New compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfNew compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdf
 

Mais de 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
 

Último

The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGSIVASHANKAR N
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
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
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
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
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
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
 
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
 
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.pdfankushspencer015
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 

Último (20)

The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.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
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
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...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
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...
 
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
 
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
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 

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