SlideShare a Scribd company logo
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

Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
Radhakrishnan Chinnusamy
 
LL(1) parsing
LL(1) parsingLL(1) parsing
LL(1) parsing
KHYATI PATEL
 
Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4
Iffat Anjum
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
ASHOK KUMAR REDDY
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
Mahbubur Rahman
 
First and follow set
First and follow setFirst and follow set
First and follow set
Dawood Faheem Abbasi
 
Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)
Nitin Mohan Sharma
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
kunj desai
 
Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)
Siddhesh Pange
 
Lecture11 syntax analysis_7
Lecture11 syntax analysis_7Lecture11 syntax analysis_7
Lecture11 syntax analysis_7
Mahesh Kumar Chelimilla
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
Antony Alex
 
Nonrecursive predictive parsing
Nonrecursive predictive parsingNonrecursive predictive parsing
Nonrecursive predictive parsing
alldesign
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
Tanzeela_Hussain
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
Dattatray Gandhmal
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
Prankit Mishra
 
Predictive parser
Predictive parserPredictive parser
Predictive parser
Jothi Lakshmi
 
Optimization of dfa
Optimization of dfaOptimization of dfa
Optimization of dfa
Kiran Acharya
 

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

Syntax
SyntaxSyntax
Syntax & Stylistics 2
Syntax & Stylistics 2Syntax & Stylistics 2
Syntax & Stylistics 2Rick McKinnon
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
Akshaya Arunan
 
Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3
Iffat Anjum
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
Akshaya Arunan
 
Module 11
Module 11Module 11
Module 11
bittudavis
 
Assigment # 1 telecommunication system
Assigment # 1 telecommunication systemAssigment # 1 telecommunication system
Assigment # 1 telecommunication system
Nadeem Khan
 
Comiler construction Notes
Comiler construction NotesComiler construction Notes
Comiler construction NotesNadeem Khan
 
Morphology exercises
Morphology exercisesMorphology exercises
Morphology exercises
Jahanzeb Jahan
 

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

LL(1) Parsers
LL(1) ParsersLL(1) Parsers
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTCh4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
FutureTechnologies3
 
3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx
Mattupallipardhu
 
Parsing
ParsingParsing
Parsing
khush_boo31
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniques
d72994185
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
ayyankhanna6480086
 
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
Soumen Santra
 
7-parsing-error and Jake were centered at Gondar began
7-parsing-error and Jake were centered at Gondar began7-parsing-error and Jake were centered at Gondar began
7-parsing-error and Jake were centered at Gondar began
KiyaMama
 
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
Nitesh Dubey
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
BBDITM LUCKNOW
 
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 Design
Akhil 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 way
Kir Chou
 
Ch2 (1).ppt
Ch2 (1).pptCh2 (1).ppt
Ch2 (1).ppt
daniloalbay1
 
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
sumitbardhan
 
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.ppt
SheikhMuhammadSaad3
 

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
 
7-parsing-error and Jake were centered at Gondar began
7-parsing-error and Jake were centered at Gondar began7-parsing-error and Jake were centered at Gondar began
7-parsing-error and Jake were centered at Gondar began
 
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]
 

More from Mahesh Kumar Chelimilla

Lecture3 lexical analysis
Lecture3 lexical analysisLecture3 lexical analysis
Lecture3 lexical analysis
Mahesh Kumar Chelimilla
 
Lecture2 general structure of a compiler
Lecture2 general structure of a compilerLecture2 general structure of a compiler
Lecture2 general structure of a compiler
Mahesh Kumar Chelimilla
 
Lecture1 introduction compilers
Lecture1 introduction compilersLecture1 introduction compilers
Lecture1 introduction compilers
Mahesh Kumar Chelimilla
 
Transportlayer tanenbaum
Transportlayer tanenbaumTransportlayer tanenbaum
Transportlayer tanenbaum
Mahesh Kumar Chelimilla
 
Network layer tanenbaum
Network layer tanenbaumNetwork layer tanenbaum
Network layer tanenbaum
Mahesh Kumar Chelimilla
 
Forouzan frame relay
Forouzan frame relayForouzan frame relay
Forouzan frame relay
Mahesh Kumar Chelimilla
 
Forouzan data link_2
Forouzan data link_2Forouzan data link_2
Forouzan data link_2
Mahesh Kumar Chelimilla
 
Forouzan data link_1
Forouzan data link_1Forouzan data link_1
Forouzan data link_1
Mahesh Kumar Chelimilla
 
Datalinklayer tanenbaum
Datalinklayer tanenbaumDatalinklayer tanenbaum
Datalinklayer tanenbaum
Mahesh Kumar Chelimilla
 

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

road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
Kamal Acharya
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
DuvanRamosGarzon1
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 

Recently uploaded (20)

road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 

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