SlideShare a Scribd company logo
SYNTAX ANALYSIS
Outline
• Role of parser
• Context free grammars
• Top down parsing
The role of parser
Lexical
Analyzer
Parser
Source
program
token
getNext
Token
Symbol
table
Parse tree Rest of Front
End
Intermediate
representation
Error handling
• Common programming errors
• Lexical errors
• Syntactic errors
• Semantic errors
• Lexical errors
• Error handler goals
• Report the presence of errors clearly and accurately
• Recover from each error quickly enough to detect subsequent errors
• Add minimal overhead to the processing of correct progrms
Error-recover strategies
• Panic mode recovery
• Discard input symbol one at a time until one of designated set of
synchronization tokens is found
• Phrase level recovery
• Replacing a prefix of remaining input by some string that allows the parser to
continue
• Error productions
• Augment the grammar with productions that generate the erroneous
constructs
• Global correction
• Choosing minimal sequence of changes to obtain a globally least-cost
correction
Context free grammars
• Terminals
• Nonterminals
• Start symbol
• productions
expression -> expression + term
expression -> expression – term
expression -> term
term -> term * factor
term -> term / factor
term -> factor
factor -> (expression)
factor -> id
Derivations
• Productions are treated as rewriting rules to generate a string
• Rightmost and leftmost derivations
• E -> E + E | E * E | -E | (E) | id
• Derivations for –(id+id)
• E => -E => -(E) => -(E+E) => -(id+E)=>-(id+id)
Parse trees
• -(id+id)
• E => -E => -(E) => -(E+E) => -(id+E)=>-(id+id)
Ambiguity
• For some strings there exist more than one parse tree
• Or more than one leftmost derivation
• Or more than one rightmost derivation
• Example: id+id*id
Elimination of ambiguity
Elimination of ambiguity (cont.)
• Idea:
• A statement appearing between a then and an else must be matched
Elimination of left recursion
• A grammar is left recursive if it has a non-terminal A such that
there is a derivation A=> Aα
• Top down parsing methods cant handle left-recursive grammars
• A simple rule for direct left recursion elimination:
• For a rule like:
• A -> A α|β
• We may replace it with
• A -> β A’
• A’ -> α A’ | ɛ
+
Left recursion elimination (cont.)
• There are cases like following
• S -> Aa | b
• A -> Ac | Sd | ɛ
• Left recursion elimination algorithm:
• Arrange the nonterminals in some order A1,A2,…,An.
• For (each i from 1 to n) {
• For (each j from 1 to i-1) {
• Replace each production of the form Ai-> Aj γ by the production Ai -> δ1 γ | δ2 γ | … |δk γ
where Aj-> δ1 | δ2 | … |δk are all current Aj productions
• }
• Eliminate left recursion among the Ai-productions
• }
Left factoring
• Left factoring is a grammar transformation that is useful for
producing a grammar suitable for predictive or top-down parsing.
• Consider following grammar:
• Stmt -> if expr then stmt else stmt
• | if expr then stmt
• On seeing input if it is not clear for the parser which production to
use
• We can easily perform left factoring:
• If we have A->αβ1 | αβ2 then we replace it with
• A -> αA’
• A’ -> β1 | β2
Left factoring (cont.)
• Algorithm
• For each non-terminal A, find the longest prefix α common to two or more of
its alternatives. If α<> ɛ, then replace all of A-productions A->αβ1 |αβ2 |
… | αβn | γ by
• A -> αA’ | γ
• A’ -> β1 |β2 | … | βn
• Example:
• S -> I E t S | i E t S e S | a
• E -> b
Top Down Parsing
Introduction
• A Top-down parser tries to create a parse tree from the root towards
the leafs scanning input from left to right
• It can be also viewed as finding a leftmost derivation for an input string
• Example: id+id*id
E -> TE’
E’ -> +TE’ | Ɛ
T -> FT’
T’ -> *FT’ | Ɛ
F -> (E) | id
E
lm
E
T E’
lm
E
T E’
F T’
lm
E
T E’
F T’
id
lm
E
T E’
F T’
id Ɛ
lm
E
T E’
F T’
id Ɛ
+ T E’
Recursive descent parsing
• Consists of a set of procedures, one for each nonterminal
• Execution begins with the procedure for start symbol
• A typical procedure for a non-terminal
void A() {
choose an A-production, A->X1X2..Xk
for (i=1 to k) {
if (Xi is a nonterminal
call procedure Xi();
else if (Xi equals the current input symbol a)
advance the input to the next symbol;
else /* an error has occurred */
}
}
Recursive descent parsing (cont)
• General recursive descent may require backtracking
• The previous code needs to be modified to allow backtracking
• In general form it cant choose an A-production easily.
• So we need to try all alternatives
• If one failed the input pointer needs to be reset and another
alternative should be tried
• Recursive descent parsers cant be used for left-recursive grammars
Example
S->cAd
A->ab | a Input: cad
S
c A d
S
c A d
a b
S
c A d
a
First and Follow
• First() is set of terminals that begins strings derived from
• If α=>ɛ then is also in First(ɛ)
• In predictive parsing when we have A-> α|β, if First(α)
and First(β) are disjoint sets then we can select
appropriate A-production by looking at the next input
• Follow(A), for any nonterminal A, is set of terminals a that can
appear immediately after A in some sentential form
• If we have S => αAaβ for some αand βthen a is in Follow(A)
• If A can be the rightmost symbol in some sentential form, then $ is in
Follow(A)
*
*
Computing First
• To compute First(X) for all grammar symbols X, apply following rules
until no more terminals or ɛ can be added to any First set:
1. If X is a terminal then First(X) = {X}.
2. If X is a nonterminal and X->Y1Y2…Yk is a production for some k>=1, then
place a in First(X) if for some i a is in First(Yi) and ɛ is in all of First(Y1),
…,First(Yi-1) that is Y1…Yi-1 => ɛ. if ɛ is in First(Yj) for j=1,…,k then add
ɛ to First(X).
3. If X-> ɛ is a production then add ɛ to First(X)
• Example!
*
*
Computing follow
• To compute First(A) for all nonterminals A, apply following rules until
nothing can be added to any follow set:
1. Place $ in Follow(S) where S is the start symbol
2. If there is a production A-> αBβ then everything in First(β) except ɛ is
in Follow(B).
3. If there is a production A->B or a production A->αBβ where
First(β) contains ɛ, then everything in Follow(A) is in Follow(B)
• Example!
LL(1) Grammars
• Predictive parsers are those recursive descent parsers needing no
backtracking
• Grammars for which we can create predictive parsers are called LL(1)
• The first L means scanning input from left to right
• The second L means leftmost derivation
• And 1 stands for using one input symbol for lookahead
• A grammar G is LL(1) if and only if whenever A-> α|βare two distinct
productions of G, the following conditions hold:
• For no terminal a do αandβ both derive strings beginning with a
• At most one of α or βcan derive empty string
• If α=> ɛ then βdoes not derive any string beginning with a terminal in
Follow(A).
*
Construction of predictive parsing table
• For each production A->α in grammar do the following:
• For each terminal a in First(α) add A-> in M[A,a]
• If ɛ is in First(α), then for each terminal b in Follow(A) add A-> ɛ to
M[A,b]. If ɛ is in First(α) and $ is in Follow(A), add A-> ɛ to M[A,$] as
well
• If after performing the above, there is no production in M[A,a] then
set M[A,a] to error
Example
E -> TE’
E’ -> +TE’ | Ɛ
T -> FT’
T’ -> *FT’ | Ɛ
F -> (E) | id
F
T
E
E’
T’
First Follow
{(,id}
{(,id}
{(,id}
{+,ɛ}
{*,ɛ}
{+, *, ), $}
{+, ), $}
{+, ), $}
{), $}
{), $}
E
E’
T
T’
F
Non -
terminal
Input Symbol
id + * ( ) $
E -> TE’ E -> TE’
E’ -> +TE’ E’ -> Ɛ E’ -> Ɛ
T -> FT’ T -> FT’
T’ -> *FT’
T’ -> Ɛ T’ -> Ɛ T’ -> Ɛ
F -> (E)
F -> id
Another example
S -> iEtSS’ | a
S’ -> eS | Ɛ
E -> b
S
S’
E
Non -
terminal
Input Symbol
a b e i t $
S -> a S -> iEtSS’
S’ -> Ɛ
S’ -> eS
S’ -> Ɛ
E -> b
Non-recursive predicting parsing
a + b $
Predictive
parsing
program
output
Parsing
Table
M
stack X
Y
Z
$
Predictive parsing algorithm
Set ip point to the first symbol of w;
Set X to the top stack symbol;
While (X<>$) { /* stack is not empty */
if (X is a) pop the stack and advance ip;
else if (X is a terminal) error();
else if (M[X,a] is an error entry) error();
else if (M[X,a] = X->Y1Y2..Yk) {
output the production X->Y1Y2..Yk;
pop the stack;
push Yk,…,Y2,Y1 on to the stack with Y1 on top;
}
set X to the top stack symbol;
}
Example
• id+id*id$
Matched Stack Input Action
E$ id+id*id$
Error recovery in predictive parsing
• Panic mode
• Place all symbols in Follow(A) into synchronization set for
nonterminal A: skip tokens until an element of Follow(A) is seen and
pop A from stack.
• Add to the synchronization set of lower level construct the symbols
that begin higher level constructs
• Add symbols in First(A) to the synchronization set of nonterminal A
• If a nonterminal can generate the empty string then the production
deriving can be used as a default
• If a terminal on top of the stack cannot be matched, pop the
terminal, issue a message saying that the terminal was insterted
Example E
E’
T
T’
F
Non -
terminal
Input Symbol
id + * ( ) $
E -> TE’ E -> TE’
E’ -> +TE’ E’ -> Ɛ E’ -> Ɛ
T -> FT’ T -> FT’
T’ -> *FT’
T’ -> Ɛ T’ -> Ɛ T’ -> Ɛ
F -> (E)
F -> id
synch synch
synch synch synch
synch synch synch synch
Stack Input Action
E$ )id*+id$ Error, Skip )
E$ id*+id$ id is in First(E)
TE’$ id*+id$
FT’E’$ id*+id$
idT’E’$ id*+id$
T’E’$ *+id$
*FT’E’$ *+id$
+id$
FT’E’$ Error, M[F,+]=synch
+id$
T’E’$ F has been poped
Thank You

More Related Content

Similar to 51114.-Compiler-Design-Syntax-Analysis-Top-down.ppt (20)

PPTX
Top down parsing
LakshmiSamivel
 
PDF
Assignment10
Sunita Milind Dol
 
PPTX
Top Down Parsing, Predictive Parsing
Tanzeela_Hussain
 
PPT
PARSING.ppt
ayyankhanna6480086
 
PPT
Lecture 05 syntax analysis 2
Iffat Anjum
 
PPTX
Syntactic Analysis in Compiler Construction
voyoc79528
 
PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
FutureTechnologies3
 
PPTX
Unitiv 111206005201-phpapp01
riddhi viradiya
 
PPTX
11CS10033.pptx
ssuser0be977
 
PPTX
LL(1) parsing
KHYATI PATEL
 
PPTX
5-Top-Down Parsing natural language .pptx
ratnababum
 
PDF
ACD-U2-TopDown..pdf it hhepls inall the the
akshay04282004
 
PPT
Chapter 3 -Syntax Analyzer.ppt
FamiDan
 
PDF
LL(1) and the LR family of parsers used in compilers
MandarMitra1
 
PPT
lect08.ppt
ssuser0be977
 
PDF
Left factor put
siet_pradeep18
 
PDF
Lecture8 syntax analysis_4
Mahesh Kumar Chelimilla
 
PDF
07 top-down-parsing
Harish Khodke
 
PPTX
Top down parsing
Prankit Mishra
 
PPTX
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
venkatapranaykumarGa
 
Top down parsing
LakshmiSamivel
 
Assignment10
Sunita Milind Dol
 
Top Down Parsing, Predictive Parsing
Tanzeela_Hussain
 
PARSING.ppt
ayyankhanna6480086
 
Lecture 05 syntax analysis 2
Iffat Anjum
 
Syntactic Analysis in Compiler Construction
voyoc79528
 
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
FutureTechnologies3
 
Unitiv 111206005201-phpapp01
riddhi viradiya
 
11CS10033.pptx
ssuser0be977
 
LL(1) parsing
KHYATI PATEL
 
5-Top-Down Parsing natural language .pptx
ratnababum
 
ACD-U2-TopDown..pdf it hhepls inall the the
akshay04282004
 
Chapter 3 -Syntax Analyzer.ppt
FamiDan
 
LL(1) and the LR family of parsers used in compilers
MandarMitra1
 
lect08.ppt
ssuser0be977
 
Left factor put
siet_pradeep18
 
Lecture8 syntax analysis_4
Mahesh Kumar Chelimilla
 
07 top-down-parsing
Harish Khodke
 
Top down parsing
Prankit Mishra
 
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
venkatapranaykumarGa
 

More from Padamata Rameshbabu (9)

PDF
CD NOTErvvtvvevbvtgv4tgtgtgtgtvefeveS.pdf
Padamata Rameshbabu
 
PDF
Ch03-LexicalAnalysis in compiler design subject.pdf
Padamata Rameshbabu
 
PDF
05SyntaxAnalysis in compiler design notespdf
Padamata Rameshbabu
 
PPTX
Ch03-LexicalAnalysis chapter2 in compiler design.pptx
Padamata Rameshbabu
 
PPT
51114.-Compiler-Design-Syntax-Analysis-Top-down.ppt
Padamata Rameshbabu
 
PPTX
LexicalAnalysis chapter2 i n compiler design.pptx
Padamata Rameshbabu
 
PDF
CNS2 unit 2.pdf
Padamata Rameshbabu
 
DOCX
CNS unit -1.docx
Padamata Rameshbabu
 
PDF
Http tutorial
Padamata Rameshbabu
 
CD NOTErvvtvvevbvtgv4tgtgtgtgtvefeveS.pdf
Padamata Rameshbabu
 
Ch03-LexicalAnalysis in compiler design subject.pdf
Padamata Rameshbabu
 
05SyntaxAnalysis in compiler design notespdf
Padamata Rameshbabu
 
Ch03-LexicalAnalysis chapter2 in compiler design.pptx
Padamata Rameshbabu
 
51114.-Compiler-Design-Syntax-Analysis-Top-down.ppt
Padamata Rameshbabu
 
LexicalAnalysis chapter2 i n compiler design.pptx
Padamata Rameshbabu
 
CNS2 unit 2.pdf
Padamata Rameshbabu
 
CNS unit -1.docx
Padamata Rameshbabu
 
Http tutorial
Padamata Rameshbabu
 
Ad

Recently uploaded (20)

PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Horarios de distribución de agua en julio
pegazohn1978
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
Ad

51114.-Compiler-Design-Syntax-Analysis-Top-down.ppt

  • 2. Outline • Role of parser • Context free grammars • Top down parsing
  • 3. The role of parser Lexical Analyzer Parser Source program token getNext Token Symbol table Parse tree Rest of Front End Intermediate representation
  • 4. Error handling • Common programming errors • Lexical errors • Syntactic errors • Semantic errors • Lexical errors • Error handler goals • Report the presence of errors clearly and accurately • Recover from each error quickly enough to detect subsequent errors • Add minimal overhead to the processing of correct progrms
  • 5. Error-recover strategies • Panic mode recovery • Discard input symbol one at a time until one of designated set of synchronization tokens is found • Phrase level recovery • Replacing a prefix of remaining input by some string that allows the parser to continue • Error productions • Augment the grammar with productions that generate the erroneous constructs • Global correction • Choosing minimal sequence of changes to obtain a globally least-cost correction
  • 6. Context free grammars • Terminals • Nonterminals • Start symbol • productions expression -> expression + term expression -> expression – term expression -> term term -> term * factor term -> term / factor term -> factor factor -> (expression) factor -> id
  • 7. Derivations • Productions are treated as rewriting rules to generate a string • Rightmost and leftmost derivations • E -> E + E | E * E | -E | (E) | id • Derivations for –(id+id) • E => -E => -(E) => -(E+E) => -(id+E)=>-(id+id)
  • 8. Parse trees • -(id+id) • E => -E => -(E) => -(E+E) => -(id+E)=>-(id+id)
  • 9. Ambiguity • For some strings there exist more than one parse tree • Or more than one leftmost derivation • Or more than one rightmost derivation • Example: id+id*id
  • 11. Elimination of ambiguity (cont.) • Idea: • A statement appearing between a then and an else must be matched
  • 12. Elimination of left recursion • A grammar is left recursive if it has a non-terminal A such that there is a derivation A=> Aα • Top down parsing methods cant handle left-recursive grammars • A simple rule for direct left recursion elimination: • For a rule like: • A -> A α|β • We may replace it with • A -> β A’ • A’ -> α A’ | ɛ +
  • 13. Left recursion elimination (cont.) • There are cases like following • S -> Aa | b • A -> Ac | Sd | ɛ • Left recursion elimination algorithm: • Arrange the nonterminals in some order A1,A2,…,An. • For (each i from 1 to n) { • For (each j from 1 to i-1) { • Replace each production of the form Ai-> Aj γ by the production Ai -> δ1 γ | δ2 γ | … |δk γ where Aj-> δ1 | δ2 | … |δk are all current Aj productions • } • Eliminate left recursion among the Ai-productions • }
  • 14. Left factoring • Left factoring is a grammar transformation that is useful for producing a grammar suitable for predictive or top-down parsing. • Consider following grammar: • Stmt -> if expr then stmt else stmt • | if expr then stmt • On seeing input if it is not clear for the parser which production to use • We can easily perform left factoring: • If we have A->αβ1 | αβ2 then we replace it with • A -> αA’ • A’ -> β1 | β2
  • 15. Left factoring (cont.) • Algorithm • For each non-terminal A, find the longest prefix α common to two or more of its alternatives. If α<> ɛ, then replace all of A-productions A->αβ1 |αβ2 | … | αβn | γ by • A -> αA’ | γ • A’ -> β1 |β2 | … | βn • Example: • S -> I E t S | i E t S e S | a • E -> b
  • 17. Introduction • A Top-down parser tries to create a parse tree from the root towards the leafs scanning input from left to right • It can be also viewed as finding a leftmost derivation for an input string • Example: id+id*id E -> TE’ E’ -> +TE’ | Ɛ T -> FT’ T’ -> *FT’ | Ɛ F -> (E) | id E lm E T E’ lm E T E’ F T’ lm E T E’ F T’ id lm E T E’ F T’ id Ɛ lm E T E’ F T’ id Ɛ + T E’
  • 18. Recursive descent parsing • Consists of a set of procedures, one for each nonterminal • Execution begins with the procedure for start symbol • A typical procedure for a non-terminal void A() { choose an A-production, A->X1X2..Xk for (i=1 to k) { if (Xi is a nonterminal call procedure Xi(); else if (Xi equals the current input symbol a) advance the input to the next symbol; else /* an error has occurred */ } }
  • 19. Recursive descent parsing (cont) • General recursive descent may require backtracking • The previous code needs to be modified to allow backtracking • In general form it cant choose an A-production easily. • So we need to try all alternatives • If one failed the input pointer needs to be reset and another alternative should be tried • Recursive descent parsers cant be used for left-recursive grammars
  • 20. Example S->cAd A->ab | a Input: cad S c A d S c A d a b S c A d a
  • 21. First and Follow • First() is set of terminals that begins strings derived from • If α=>ɛ then is also in First(ɛ) • In predictive parsing when we have A-> α|β, if First(α) and First(β) are disjoint sets then we can select appropriate A-production by looking at the next input • Follow(A), for any nonterminal A, is set of terminals a that can appear immediately after A in some sentential form • If we have S => αAaβ for some αand βthen a is in Follow(A) • If A can be the rightmost symbol in some sentential form, then $ is in Follow(A) * *
  • 22. Computing First • To compute First(X) for all grammar symbols X, apply following rules until no more terminals or ɛ can be added to any First set: 1. If X is a terminal then First(X) = {X}. 2. If X is a nonterminal and X->Y1Y2…Yk is a production for some k>=1, then place a in First(X) if for some i a is in First(Yi) and ɛ is in all of First(Y1), …,First(Yi-1) that is Y1…Yi-1 => ɛ. if ɛ is in First(Yj) for j=1,…,k then add ɛ to First(X). 3. If X-> ɛ is a production then add ɛ to First(X) • Example! * *
  • 23. Computing follow • To compute First(A) for all nonterminals A, apply following rules until nothing can be added to any follow set: 1. Place $ in Follow(S) where S is the start symbol 2. If there is a production A-> αBβ then everything in First(β) except ɛ is in Follow(B). 3. If there is a production A->B or a production A->αBβ where First(β) contains ɛ, then everything in Follow(A) is in Follow(B) • Example!
  • 24. LL(1) Grammars • Predictive parsers are those recursive descent parsers needing no backtracking • Grammars for which we can create predictive parsers are called LL(1) • The first L means scanning input from left to right • The second L means leftmost derivation • And 1 stands for using one input symbol for lookahead • A grammar G is LL(1) if and only if whenever A-> α|βare two distinct productions of G, the following conditions hold: • For no terminal a do αandβ both derive strings beginning with a • At most one of α or βcan derive empty string • If α=> ɛ then βdoes not derive any string beginning with a terminal in Follow(A). *
  • 25. Construction of predictive parsing table • For each production A->α in grammar do the following: • For each terminal a in First(α) add A-> in M[A,a] • If ɛ is in First(α), then for each terminal b in Follow(A) add A-> ɛ to M[A,b]. If ɛ is in First(α) and $ is in Follow(A), add A-> ɛ to M[A,$] as well • If after performing the above, there is no production in M[A,a] then set M[A,a] to error
  • 26. Example E -> TE’ E’ -> +TE’ | Ɛ T -> FT’ T’ -> *FT’ | Ɛ F -> (E) | id F T E E’ T’ First Follow {(,id} {(,id} {(,id} {+,ɛ} {*,ɛ} {+, *, ), $} {+, ), $} {+, ), $} {), $} {), $} E E’ T T’ F Non - terminal Input Symbol id + * ( ) $ E -> TE’ E -> TE’ E’ -> +TE’ E’ -> Ɛ E’ -> Ɛ T -> FT’ T -> FT’ T’ -> *FT’ T’ -> Ɛ T’ -> Ɛ T’ -> Ɛ F -> (E) F -> id
  • 27. Another example S -> iEtSS’ | a S’ -> eS | Ɛ E -> b S S’ E Non - terminal Input Symbol a b e i t $ S -> a S -> iEtSS’ S’ -> Ɛ S’ -> eS S’ -> Ɛ E -> b
  • 28. Non-recursive predicting parsing a + b $ Predictive parsing program output Parsing Table M stack X Y Z $
  • 29. Predictive parsing algorithm Set ip point to the first symbol of w; Set X to the top stack symbol; While (X<>$) { /* stack is not empty */ if (X is a) pop the stack and advance ip; else if (X is a terminal) error(); else if (M[X,a] is an error entry) error(); else if (M[X,a] = X->Y1Y2..Yk) { output the production X->Y1Y2..Yk; pop the stack; push Yk,…,Y2,Y1 on to the stack with Y1 on top; } set X to the top stack symbol; }
  • 30. Example • id+id*id$ Matched Stack Input Action E$ id+id*id$
  • 31. Error recovery in predictive parsing • Panic mode • Place all symbols in Follow(A) into synchronization set for nonterminal A: skip tokens until an element of Follow(A) is seen and pop A from stack. • Add to the synchronization set of lower level construct the symbols that begin higher level constructs • Add symbols in First(A) to the synchronization set of nonterminal A • If a nonterminal can generate the empty string then the production deriving can be used as a default • If a terminal on top of the stack cannot be matched, pop the terminal, issue a message saying that the terminal was insterted
  • 32. Example E E’ T T’ F Non - terminal Input Symbol id + * ( ) $ E -> TE’ E -> TE’ E’ -> +TE’ E’ -> Ɛ E’ -> Ɛ T -> FT’ T -> FT’ T’ -> *FT’ T’ -> Ɛ T’ -> Ɛ T’ -> Ɛ F -> (E) F -> id synch synch synch synch synch synch synch synch synch Stack Input Action E$ )id*+id$ Error, Skip ) E$ id*+id$ id is in First(E) TE’$ id*+id$ FT’E’$ id*+id$ idT’E’$ id*+id$ T’E’$ *+id$ *FT’E’$ *+id$ +id$ FT’E’$ Error, M[F,+]=synch +id$ T’E’$ F has been poped