Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views68 pages

Parsing

The document provides an overview of syntax analysis in compiler construction, focusing on top-down parsing techniques. It discusses the role of the syntax analyzer, the importance of grammar, and various parsing methods, including predictive parsing and recursive descent. Additionally, it addresses issues such as ambiguous grammar and methods to eliminate ambiguity, along with examples of context-free grammars.

Uploaded by

22bce012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views68 pages

Parsing

The document provides an overview of syntax analysis in compiler construction, focusing on top-down parsing techniques. It discusses the role of the syntax analyzer, the importance of grammar, and various parsing methods, including predictive parsing and recursive descent. Additionally, it addresses issues such as ambiguous grammar and methods to eliminate ambiguity, along with examples of context-free grammars.

Uploaded by

22bce012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

Phase 2 : Syntax Analysis

Top Down Parsing


2CS701 Compiler Construction
Prof Monika Shah
Nirma University

Ref : Ch.4 Compilers Principles, Techniques, and Tools by Alfred Aho, Ravi Sethi, and Jeffrey Ullman

1
Glimpse
• Introduction to Parsing
• Role of the Syntax Analyzer (Parser)
• Components of Syntax Analysis
• Grammar
• Ambiguous Grammar
• Type of Parsing
• Top Down Parsing
• Predictive parsing
• Recursive descent parser
• Parse Table Generation
• Error Recovery
Prof Monika Shah (Nirma University)
2
Position of a Parser in the Compiler Model

Source Token, Parser


Lexical tokenval Intermediate
Program and rest of
Analyzer representation
Get next front-end
token

Lexical error Syntax error


Semantic error
Symbol Table

Prof Monika Shah (Nirma University) 3


Introduction to Parsing
• Syntax analysis for natural languages
• Recognize whether a sentence is grammatically correct
• Identify the function of each word

sentence

subject verb indirect object object Grammar


I gave him noun phrase

article noun
“I gave him the book”
the book
Prof Monika Shah (Nirma University)
Introduction to Parser (validate syntax)
Source Token, SYNTAX
Lexical tokenval Parser Tree
Program Analyzer (Syntax Analyzer)
character stream Get next
token
int main(){ DT ID(){
int a,b; error DT ID, ID; error
total = a + b ID = ID + ID SYNTAX ERROR:
per = total |3.0; ID = ID | FNUM ; ||Semicolon missing
} } at line 3

Prof Monika Shah (Nirma University)


5
5
if (b == 0) a = b;

Where is Syntax Analysis?


Lexical Analysis or Scanner

if ( b == 0 ) a = b ;

IF ( ID == NUM ) ID = ID ;

Syntax Analysis or Parsing

if
abstract syntax tree
== = or parse tree
b 0 a b If no syntax Error
Prof Monika Shah (Nirma University)
Parse Tree vs Abstract Syntax Tree
S Parse tree also called “concrete syntax”

E + S

( S ) E +
E + S 5
+ 5
1 E + S 1 +
2 E 2 +
( S ) 3 4
AST discards (abstracts) unneeded
E + S
information – more compact format
3 E 4 Prof Monika Shah (Nirma University)
Role of Syntax Analyzer

• Verify Syntax as per pre-defined Grammar for selected language


• Report Syntax errors accurately with location
• Error Recovery
• Update Symbol Table
• E.g. Update data type of variables
• Invoke Semantic Actions
• Static checking i.e. Type checking of identifiers, expressions, functions
• Syntax directed translation of source code to intermediate code generation
Prof Monika Shah (Nirma University)
Syntax Analysis Overview
• Goal – determine if the input token stream satisfies the
syntax of the program

• What do we need to do this?


• Grammar : An expressive way to describe the unambiguous
grammar | syntax
• Parsing : A mechanism that determines if the input token
stream satisfies the syntax description

Prof Monika Shah (Nirma University)


Grammar

Prof Monika Shah (Nirma University)


10
Grammar
• Specify valid possible sequence of tokens
Sentence : Subject Verb Indirect_Object Object
Object : Noun Phrase
Non Phrase : Article Noun
Article : the | an | a
Subject : ID | I | He | She| They sentence

subject verb indirect object object Grammar


I gave him noun phrase

article noun
“I gave him the book”
the book
Prof Monika Shah (Nirma University)
Chomsky Hierarchy: Language
Classification
L(regular)  L(context free)  L(context sensitive)  L(unrestricted)
• A grammar G is said to be
• Regular if it is right linear where each production is of the form
A → w B or A → w
or left linear where each production is of the form
A → B w or A → w
• Context free if each production is of the form
A→
where A  N and   (NT)*
• Context sensitive if each production is of the form
A→
where A  N, ,,  (NT)*, || > 0
• Unrestricted Prof Monika Shah (Nirma University)
12
Grammars (Recap)

• Context-free grammar is a 4-tuple


G = (N, T, P, S) where
• T is a finite set of tokens (terminal symbols)
• N is a finite set of nonterminals
• P is a finite set of productions of the form
→
where   (NT)* N (NT)* and   (NT)*
• S  N is a designated start symbol
Prof Monika Shah (Nirma University)
13
CFG Example 1: Grammar for Binary
Arithmetic Expression
E:E+E ||recursion of E ….ID + E , E+ ID
|E–E
|E*E
|E|E
| ID
| NUM
| FNUM
|(E)
Prof Monika Shah (Nirma University)
14
CFG Example 2: Grammar for IFST

ST : IFST
|E
;
IFST : IF ( E ) ST
| IF ( E ) ST ELSE ST
;

Prof Monika Shah (Nirma University)


15
CFG Example 3: Grammar for functions in C program
PROG : DT ID ‘(‘ ‘)’ BLK
BLK : ‘{‘ STS ‘}’
STS : STS ST
| ST
ST : IFST
| FORST
| WHILEST
| E ‘;’
| BLK
| ‘;’
IFST : IF ‘(‘ E ‘)’ ST
| IF ‘(‘ E ‘)’ ST ELSE ST
Prof Monika Shah (Nirma University)
16
CFG Example 3: Grammar for functions in C program CONTD…
E : E OP E
| ID ‘=‘ E
| TERM
OP : ‘+’ | ‘-’ | ‘*’ | ‘|’
TERM : ID | NUM | FNUM | ( E)
….
….
….

Prof Monika Shah (Nirma University)


17
CFG Example 4: Balanced Parenthesis
• Grammar for balanced-parentheses language
• S→(S)S
• S→
• 1 non-terminal: S
• 2 terminals: “(”, “)”
• Start symbol: S
• 2 productions

Prof Monika Shah (Nirma University)


18
Derivation
• What is Derivation ?
• Sequence of steps
• Every step : choose production to expand a non-terminal
• Final Goal : derive a string of Terminal tokens
• Two standard orders: left and right-most
• Leftmost derivation
• In the string, find the leftmost non-terminal and apply a production to it
• E+S➔1+S
• Rightmost derivation
• Same, but find rightmost non-terminal
• E+S➔E+E+S
Prof Monika Shah (Nirma University)
19
Leftmost|Rightmost Derivation Examples
»S→E+S|E E → number | (S) derive: (1 + 2 + (3 + 4)) + 5
left-most derivation right-most derivation
S S
→ E +S →E +S
→ (S) +S →E +E
→ (E+S) +S →E +5
→ (1+S) +S → (S) +5
→ (1+E+S) +S → (E+S) +5
→ (1+2+S) +S → (E+E+S) +5
→ (1+2+E) +S → (E+E+E) +5
→ (1+2+(S)) + S → (E+E+(S)) +5
→ (1+2+(E+S)) + S → (E+E+(E+S)) + 5
→ (1+2+(3+S)) + S → (E+E+(E+E)) + 5
→ (1+2+(3+E)) + S → (E+E+(E+4)) + 5
→ (1+2+(3+ 4)) + S → (E+E+(3+4)) + 5
→ (1+2+(3+4)) + E → (E+2+(3+4)) + 5
→ (1+2+(3+4)) + 5 → (1+2+(3+4)) + 5
Result: Same parse tree: same productions chosen, but in diff order
Ambiguous Grammar

• Ambiguous grammar = different derivations produce different parse trees


• More specifically, G is ambiguous if there are 2 distinct leftmost (rightmost)
derivations for some sentence

Prof Monika Shah (Nirma University)


21
Ambiguous Grammar - Example
E → E + E | E * E | number
Consider the expression: 1 + 2 * 3

Derivation 1: E → E+E → Derivation 2: E → E*E →


1+E → 1+E*E → 1+2*E → E+E*E → 1+E*E → 1+2*E →
1+2*3 1+2*3

*
+
+ 3
1 *
2 3 1 2

Obviously not equal!


Prof Monika Shah (Nirma University)
Eliminating Ambiguity from Grammar
• Rewrite Grammar with following guidelines
1. Introduce distinct Non-terminal for each precedence level
e.g. E for +, - T for *, | F for ID, NUM , (
2. Higher precedence non-terminal
e.g. E: T+ E | T- E | T E:E+T|E–T|T
T : F * T | F|T | F OR T : T * F | T|F | F
F : ID | NUM | (E )
3. Use Left recursion for Left associative operator , right recursion for Right Associative
E:E+T|E–T|T G : ID = G | E
T : T * F | T|F | F
F : ID | NUM | (G )
Prof Monika Shah (Nirma University) 23
S → S + S | S – S | S * S | S | S | (S) | -S | S ^ S | number

Class Problem (Tough)


Enforce the standard arithmetic precedence rules and remove
all ambiguity from the above grammar
Precedence (high to low)
(), unary –
^
*, |
+, -
Associativity
^ = right
rest are left

Prof Monika Shah (Nirma University)


Ambiguity Review: If-Then-Else Grammar
S → if (E) S
S → if (E) S else S Anything wrong with this grammar?
S → other Dangling else

Consider parsing: “if (E1) if (E2) S1 else S2


S → if (E) S S → if (E) S else S
→ if (E) if (E) S else S → if (E) if (E) S else S
if if
Which “if”
E1 if is the “else” E1 if S2
attached to?
E1 S1 S2 E1 S1
Prof Monika Shah (Nirma University)
Grammar for Closest-if Rule

• Want to rule out: if (E) if (E) S else S


• Impose that unmatched “if ” statements occur only on the
“else” clauses
• statement → matched | unmatched
• matched → if (E) matched else matched |
other
• unmatched → if (E) statement |
if (E) matched else unmatched

Prof Monika Shah (Nirma University)


Parsing

• Techniques to derive input token string for given


grammar

Prof Monika Shah (Nirma University) 27


Parsing

• Universal (any C-F grammar)


• Cocke-Younger-Kasimi
• Earley
• Top-down (C-F grammar with restrictions)
• Recursive descent (predictive parsing)
• LL (Left-to-right, Leftmost derivation) methods
• Bottom-up (C-F grammar with restrictions)
• Operator precedence parsing
• LR (Left-to-right, Rightmost derivation) methods
• SLR, canonical LR, LALR

Prof Monika Shah (Nirma University) 28


Parsers
1. Top-Down Parser
• the parse tree is created top to bottom, starting from the root.
• Beginning with the start symbol, try to guess the productions to apply
to end up at the user's program
2. Bottom-Up Parser
• the parse is created bottom to top; starting from the leaves
• Beginning with the user's program, try to apply productions in reverse
to convert the program back into the start symbol

Prof Monika Shah (Nirma University)


29
Top-Down Parsing
• LL methods (Left-to-right, Leftmost derivation) and recursive-descent
parsing
Grammar: Leftmost derivation:
E→T+T E lm T + T
T→(E) lm id + T
T→-E lm id + id
T → id
E E E E

T T T T T T

+ id + id + id
Prof Monika Shah (Nirma University) 30
Problem with Top-Down Parsing
Want to decide which production to apply based
on next symbol
S→E+S|E
E → num | (S)

Ex1: “(1)” S → E → (S) → (E) → (1)


Ex2: “(1)+2” S → E+S → (S)+S → (E)+S
→ (1)+E → (1)+2

How did you know to pick E+S in Ex2, if you picked


E followed by (S), you couldn’t parse it?

Prof Monika Shah (Nirma University)


Issue with Top-down parsing
Number of backtracking
• Backtracking algorithms are not –efficient
• Steps of Top-down parsing of input string : cad for Grammar: S→ cAd , A→ab |a

• Solution : Predictive Parser


Prof Monika Shah (Nirma University)
32
Predictive Parsing Program (Driver)
push($)
push(S)
a := lookahead
repeat
X := pop()
if X is a terminal or X = $ then
match(X) || moves to next token and a := lookahead
else if M[X,a] = X → Y1Y2…Yk then
push(Yk, Yk-1, …, Y2, Y1) || such that Y1 is on top
… invoke actions and|or produce IR output …
else error()
endif
until X = $ Prof Monika Shah (Nirma University)
33
Non-Recursive Predictive Parsing: Table-Driven Parsing

• Given an LL(1) grammar G = (N, T, P, S) construct a table M[A,a] for A 


N, a  T and use a driver program with a stack

input a + b $

stack
Predictive parsing
X output
program (driver)
Y
Z Parsing table
Prof Monika Shah (Nirma University) $ M 34
Table-Driven Predictive Parsing Stack Input Que Production applied
$E id+id*id$ E → T ER
$ERT id+id*id$ T → F TR
id + * ( ) $ id+id*id$ F → id
$ERTRF
$ERTRid id+id*id$
E TER T ER
$ERTR +id*id$ TR → 
$ER +id*id$ ER → + T ER
$ERT+ +id*id$
ER  
+TER $ERT id*id$ T → F TR
$ERTRF id*id$ F → id
T FTR FTR $ERTRid id*id$
$ERTR *id$ TR → * F TR
TR  *FTR  $ERTRF* *id$
 id$ F → id
$ERTRF
$ERTRid id$
F Id (E) $ TR → 
$ERTR
$ER $ ER → 
Prof Monika Shah (Nirma University) $ $ 35
Table-Driven Stack Input Que Production applied
Predictive Parsing $E Id Id $
$
id + * ( ) $
E TER T ER

ER +TE  
R

T FTR FTR

TR  *FTR 

F Id (E)

Prof Monika Shah (Nirma University) 36


Self Evaluation

• Trace top-down parsing for following strings


1. Id Id $
2. Id * (Id + Id) $
3. ) id ( id + * id $
4. ( id + ) id $

Prof Monika Shah (Nirma University)


37
Example 2: Parsing with Table
S → ES’ S’ →  | +S E → num | (S)
Partly-derived String Lookahead parsed part unparsed part
→ES’ ( (1+2+(3+4))+5
→(S)S’ 1 (1+2+(3+4))+5
→(ES’)S’ 1 (1+2+(3+4))+5
→(1S’)S’ + (1+2+(3+4))+5
→(1+ES’)S’ 2 (1+2+(3+4))+5
→(1+2S’)S’ + (1+2+(3+4))+5

num + ( ) $
S → ES’ → ES’
S’ → +S → →
E → num → (S)
Prof Monika Shah (Nirma University)
How to Implement This?
• Table can be converted easily into a recursive descent parser
• 3 procedures: parse_S(), parse_S’(), and parse_E()
• start parsing with start non-terminal
int main()
{ token = input.read();
parse_S( ) ; }

num + ( ) $
S → ES’ → ES’
S’ → +S → →
E → num → (S)
Prof Monika Shah (Nirma University)
Recursive-Descent Parser
lookahead token
void parse_S() {
switch (token) {
case num: parse_E(); parse_S’(); return;
case ‘(‘: parse_E(); parse_S’(); return;
default: ParseError();
}
}
num + ( ) $
S → ES’ → ES’
S’ → +S → →
E → num → (S)
Prof Monika Shah (Nirma University)
Recursive-Descent Parser cont…
void parse_S’() {
switch (token) {
case ‘+’: token = input.read(); parse_S(); return;
case ‘)‘: return;
case EOF: return;
default: ParseError();
}
}
num + ( ) $
S → ES’ → ES’
S’ → +S → →
E → num → (S)
Prof Monika Shah (Nirma University)
Recursive-Descent Parser cont…
void parse_E() {
switch (token) {
case number: token = input.read(); return;
case ‘(‘: token = input.read(); parse_S();
if (token != ‘)’) ParseError();
token = input.read(); return;
default: ParseError();
}
}
num + ( ) $
S → ES’ → ES’
S’ → +S → →
E → num → (S)
Prof Monika Shah (Nirma University)
Predictive Parser

• Non leftmost recursive


• Eg. LL(1) Grammar
• LL(1) Grammar
• Should not have leftmost recursion in production rules
• E.g. E : E + T is leftmost recursive
• Should not have left factoring present

Prof Monika Shah (Nirma University)


43
Left Recursion (Recap)

• Productions of the form


A→A
|
|
are left recursive
• When one of the productions in a grammar is left recursive then a predictive
parser loops forever on certain inputs

Prof Monika Shah (Nirma University)


44
A General Systematic Left Recursion Elimination Method
Input: Grammar G with no cycles or -productions
Arrange the nonterminals in some order A1, A2, …, An
for i = 1, …, n do
for j = 1, …, i-1 do ||substitution
replace each
Ai → Aj 
with
Ai → 1  | 2  | … | k 
where
Aj → 1 | 2 | … | k
enddo
eliminate the immediate left recursion in Ai
enddo
Prof Monika Shah (Nirma University)
45
Immediate Left-Recursion Elimination
Rewrite every left-recursive production
A→A
|
|
|A
into a right-recursive production:
A →  AR
|  AR
AR →  AR
|  AR
|
Prof Monika Shah (Nirma University)
46
Example Left Recursion Elimination completely
A→BC|a
B→CA|Ab Choose arrangement: A, B, C
C→AB|CC|a
i = 1: nothing to do
i = 2, j = 1: B → C A | A b For every non-terminal repeat step 1 and 2
 B→CA|BCb|ab Step 1. Substitute visited leftmost
(imm) B → C A BR | a b BR Non-terminal
BR → C b B R |  Step 2. Apply Immediate Left recursion
i = 3, j = 1: C → A B | C C | a elimination
 C→BCB|aB|CC|a
i = 3, j = 2: C → B C B | a B | C C | a
 C → C A BR C B | a b B R C B | a B | C C | a
(imm) C → a b BR C B CR | a B CR | a CR
CR → A BR C B CR | C CR | 
Prof Monika Shah (Nirma University)
47
Self Evaluation

• Eliminate Left Recursion from following Grammars


1. E → ID = E | E + E | E – E | E * E | E/ E | ID | NUM
2. A → ABd | Aa | a ; B → Be | b
3. S → Aa | b ; A → Ac | Sd | ∈
4. S → A ; A → Ad | Ae | aB | ac ; B → bBc | f

Prof Monika Shah (Nirma University)


48
Left Factoring

• When a nonterminal has two or more productions whose right-


hand sides start with the same grammar symbols, the grammar
is not LL(1) and cannot be used for predictive parsing
• Replace productions
A →   1 |  2 | … |   n | 
with
A →  AR | 
A R → 1 | 2 | … | n
Prof Monika Shah (Nirma University)
49
Left Factoring Example

• S : iStS | iStSeS
• After Left factoring
• S : iStSS’
• S’ : null | eS

Prof Monika Shah (Nirma University)


50
Predictive Parsing

• Eliminate left recursion from grammar


• Left factor the grammar
• Compute FIRST and FOLLOW
• Two variants:
• Recursive (recursive-descent parsing)
• Non-recursive (table-driven parsing)

Prof Monika Shah (Nirma University)


51
E → T ER
FIRST (Revisited) ER → + T E R | 
T → F TR
TR → * F TR | 
• FIRST() = { the set of terminals that begin all strings derived from  } F → ( E ) | id
FIRST(a) = {a} if a  T A→ FIRST()
FIRST() = {} E → T ER ( id
FIRST(A) = A→ FIRST() for A→  P
FIRST(X1X2…Xk) = ER → + T ER +
if for all j = 1, …, i-1 :   FIRST(Xj) then ER →  
add non- in FIRST(Xi) to FIRST(X1X2…Xk)
if for all j = 1, …, k :   FIRST(Xj) then T → F TR ( id
add  to FIRST(X1X2…Xk) T R → * F TR *
TR →  
F→(E) (
F → id id
Prof Monika Shah (Nirma University)
52
E → T ER
ER → + T ER | 
T → F TR
FOLLOW TR → * F TR | 
F → ( E ) | id
• FOLLOW(A) = { the set of terminals that can
immediately follow nonterminal A } A→ FOLLOW(A)
E → T ER $)
FOLLOW(A) =
for all (B →  A )  P do E R → + T ER $)
add FIRST()\{} to FOLLOW(A) ER →  $)
for all (B →  A )  P and   FIRST() do
add FOLLOW(B) to FOLLOW(A) T → F TR +$)
for all (B →  A)  P do TR → * F TR +$)
add FOLLOW(B) to FOLLOW(A)
if A is the start symbol S then TR →  +$)
add $ to FOLLOW(A) F→(E) *+$)
F → id *+$)
Prof Monika Shah (Nirma University)
53
LL(1) Grammar

• A grammar G is LL(1) if it is not left recursive and for each


collection of productions
A →  1 | 2 | … | n
for nonterminal A the following holds:
1. FIRST(i)  FIRST(j) =  for all i  j
2. if i   then
*
2.a. j   for all i  j
*
2.b. FIRST(j)  FOLLOW(A) = 
for all i  j
Prof Monika Shah (Nirma University)
54
Non-LL(1) Examples
Grammar Not LL(1) because:
S→Sa|a Left recursive
S→aS|a FIRST(a S)  FIRST(a)  
S→aR|
R→S| For R: S *  and   * 
S→aRa For R:
R→S| FIRST(S)  FOLLOW(R)  
Exercise :Rewrite above non-LL(1) grammar by Left factoring, and
Left recursion elimination, and check is it still LL(1) ?
Prof Monika Shah (Nirma University) 55
Constructing an LL(1) Predictive Parsing Table
for each production A →  do
for each a  FIRST() do
add A →  to M[A,a]
enddo
if   FIRST() then
for each b  FOLLOW(A) do
add A →  to M[A,b]
enddo
endif
enddo
Mark each undefined entry in M error

Prof Monika Shah (Nirma University)


56
A→ FIRST() FOLLOW(A)
Example Table E → T ER ( id $)
ER → + T ER + $)
E → T ER
ER →   $)
ER → + T ER | 
T → F TR ( id +$)
T → F TR
TR → * F TR * +$)
TR → * F TR | 
TR →   +$)
F → ( E ) | id
F→(E) ( *+$)
F → id id *+$)
id + * ( ) $
E E → T ER E → T ER
ER ER → + T ER ER →  ER → 
T T → F TR T → F TR
TR TR →  TR → * F TR TR →  TR → 
F F → id F→(E) 57
LL(1) Grammars are Unambiguous
Ambiguous grammar A→ FIRST() FOLLOW(A)
S → i E t S SR | a S → i E t S SR i e$
SR → e S |  S→a a e$
E→b SR → e S e e$
SR →   e$
E→b b t
Error: duplicate table entry
a b e i t $
S S→a S → i E t S SR
SR → 
SR SR → 
SR → e S
E E→b 58
Error Recovery Strategies
• Panic Mode Error Recovery
• Phrase Level Error Recovery
• Production rule based Error recovery
• Global Error Recovery

59
Panic Mode Recovery
Add synchronizing actions to FOLLOW(E) = { ) $ }
undefined entries based on FOLLOW FOLLOW(ER) = { ) $ }
FOLLOW(T) = { + ) $ }
Pro: Can be automated FOLLOW(TR) = { + ) $ }
Cons: Error messages are needed FOLLOW(F) = { + * ) $ }

id + * ( ) $
E E → T ER E → T ER synch synch
ER ER → + T ER ER →  ER → 
T T → F TR synch T → F TR synch synch
TR TR →  TR → * F TR TR →  TR → 
F F → id synch synch F→(E) synch synch
60
synch: the driver pops current nonterminal A and skips input till
synch token or skips input until one of FIRST(A) is found
Panic mode Error Recovery
1. If the parser looks up entry M[A,a] and finds that it
is blank, the input symbol a is skipped.
2. If the entry is synch, the nonterminal on top of the
stack is popped., skip token until sync or First(A)
3. If a token on top of the stack does not match the
input symbol, then we pop the token from the stack.

Prof Monika Shah (Nirma University)


Example: Panic mode error recovery
1. If M[A,a]=blank, skip the input symbol
2. If M[A,a]= synch, Pop the Non-terminal
3. If top(stack) =terminal and
input!=top(stack), pop the token

s = synch

id + * ( ) $
E TER T ER Synch Synch

ER +TER  
T FTR Synch FTR Synch Synch

TR  *FTR  
F Id Synch Synch (E) Synch Synch
Stack Input Remark
Example 2 : Panic mode recovery
$E ( Id ( + $
$ERT ( Id ( + $
1. If M[A,a]=blank, skip the input $ERTRF ( Id ( + $
symbol $ERTR)E( ( Id ( + $ Match (
2. If M[A,a]= synch, Pop the Non- $ERTR)E Id ( + $
terminal $ERTR)ERT Id ( + $
$ERTR)ERTRF Id ( + $
3. If top(stack) =terminal and
$ERTR)ERTRId Id ( +$ Match Id
input!=top(stack), pop the token
$ERTR)ERTR (+$ M[TR,(]=blank.➔ Recovery :Skip (.
Then s = synch Error: Unexpected (
$ERTR)ERTR +$
id + * ( ) $
$ERTR)ER +$
E TER skip skip T ER Synch Synch
$ERTR)ERT+ +$ Match +
ER skip +TER skip skip  
$ERTR)ERT $ M[TR,$]=sync➔Recovery :pop T.
T FTR Synch skip FTR Synch Synch
Error: missing Id
TR skip  *FTR skip   $ERTR)ER $
F Id Synch Synch (E) Synch Synch
$ERTR) $ Top(stack)=‘)’ != $➔Recovery :pop ).
Error: missing )
$ERTR $
Phrase-Level Recovery
Change input stream by inserting missing tokens
For example: id id is changed into id * id
Pro: Can be automated
Cons: Recovery not always intuitive
Can then continue here

id + * ( ) $
E E → T ER E → T ER synch synch
ER ER → + T ER ER →  ER → 
T T → F TR synch T → F TR synch synch
TR insert * TR →  TR → * F TR TR →  TR → 
F F → id synch synch F→(E) synch synch
64
insert *: driver inserts missing * and retries the production
Example : Phrase level error recovery
Stack Input Remark
$E ( Id Id $
$ERT ( Id Id $
$ERTRF ( Id Id $
$ERTR)E( ( Id Id $ Match (
$ERTR)E Id Id $
$ERTR)ERT Id Id $
$ERTR)ERTRF Id Id $
$ERTR) ERTRId Id Id $ Match Id
id + * ( ) $
$ERTR) ERTR Id $ M[TR,Id]=Insert *.
E TER T ER Synch Synch Recovery Insert *.
Error: Missing *
ER +TER  
$ERTR) ERTR * Id $
T FTR Synch FTR Synch Synch
$ERTR) ERTR F * * Id $ Match *
Insert
TR *  *FTR   $ERTR) TR F Id $
$ERTR) TR Id Id $ Match Id
F Id Synch Synch (E) Synch Synch

Error Productions
E → T ER Add “error production”:
ER → + T E R |  TR → F T R
T → F TR to ignore missing *, e.g.: id id
TR → * F TR |  Pro: Powerful recovery method
F → ( E ) | id Cons: Cannot be automated
id + * ( ) $
E E → T ER E → T ER synch synch
ER ER → + T ER ER →  ER → 
T T → F TR synch T → F TR synch synch
TR T R → F TR TR →  TR → * F TR TR →  TR → 
66
F F → id synch synch F→(E) synch synch
Example : error recovery using Error productions
Stack Input Remark
$E ( Id Id $
$ERT ( Id Id $
$ERTRF ( Id Id $
$ERTR)E( ( Id Id $ Match (
$ERTR)E Id Id $
$ERTR)ERTRF Id Id $
$ERTR) ERTRId Id Id $ Match Id
$ERTR) ERTR Id $ Error: Missing *
id + * ( ) $ $ERTR) ERTR F Id $
E TER T ER Synch Synch $ERTR) ERTR Id Id $ Match Id

ER +TER  
T FTR Synch FTR Synch Synch

TR FTR  *FTR  
F Id Synch Synch (E) Synch Synch
Self Evaluation

• Trace top-down parsing with different error recovery for following strings
1. ) id ( id + * id $
2. ( id + ) id $

Prof Monika Shah (Nirma University)


68

You might also like