0% found this document useful (0 votes)
11 views30 pages

Error Recovery

The document discusses error-recovery strategies in compiler design, outlining different types of parsers and error handling techniques. It details various error types such as lexical, syntactic, semantic, and logical errors, along with recovery methods like panic mode, phrase level recovery, error production, and global correction. Additionally, it covers predictive parsing and the use of synchronizing tokens for effective error recovery.
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)
11 views30 pages

Error Recovery

The document discusses error-recovery strategies in compiler design, outlining different types of parsers and error handling techniques. It details various error types such as lexical, syntactic, semantic, and logical errors, along with recovery methods like panic mode, phrase level recovery, error production, and global correction. Additionally, it covers predictive parsing and the use of synchronizing tokens for effective error recovery.
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/ 30

Error-Recovery Strategies

Samit Biswas Compiler Design


Types of Parsers
I Universal
Can parse any grammmar
I Cocke-Younger-Kasami parsing method
I Earley’s algorithm
In-efficient to be used in production of compilers.
I Top-down - build purse trees
Starts parsing from top (root) to the bottom (leaves).
I Bottom-up
Starts from leaves and work their way upto the root.
I Both in top-down and bottom up
I scan the input from left to right.
I one symbol at a time.

Samit Biswas Compiler Design


Error Handling Techniques

What should happen when your parser finds an error in the


user input ?

Samit Biswas Compiler Design


Error Handling Techniques

What should happen when your parser finds an error in the


user input ?
I Stop immediately and signal an error.

Samit Biswas Compiler Design


Error Handling Techniques

What should happen when your parser finds an error in the


user input ?
I Stop immediately and signal an error.
I Record the error but try to continue.

Samit Biswas Compiler Design


Error Handling Techniques

What should happen when your parser finds an error in the


user input ?
I Stop immediately and signal an error.
I Record the error but try to continue.
In the first case user must recompile the scratch after a trivial
fix. In the second case, the user might be overwhelmed by a
whole series of error messages, all caused by essentially the
same problem.

Samit Biswas Compiler Design


Error Handling Techniques:

There are mainly four types of error. They are as follows:


I Lexical Error: Such as misspelling an identifier, keyword or
operator.
I Syntactic Error: Such as an arithmetic expression with
unbalanced parentheses.
I Semantic Error: Such as an operator applied to an
incompatible operand.
I Logical Error: Such as infinitely recursive call.

Samit Biswas Compiler Design


Error-Recovery Strategies
I Panic Mode.
I Phrase Level Recovery.
I Error Production.
I Global Correction.

Samit Biswas Compiler Design


Panic Mode Error Recovery
I It is based on the idea of skipping symbols on the input
until in a selected set of synchronizing tokens appears.
I In case of an error like:
a = b + c // no semi-colon
d = e + f;
The compiler will discard all subsequent tokens till a
semicolon is encountered.

Samit Biswas Compiler Design


Phrase Level Recovery.
Perform local correction on the input to repair the error.
I Change input stream by inserting missing tokens
I For example: int id 5; is changed into int id=5;

Samit Biswas Compiler Design


Error Production
Augment grammar with productions for erroneous constructs.

Samit Biswas Compiler Design


Global Correction
Choose a minimal sequence of changes to obtain a global
least-cost correction

Samit Biswas Compiler Design


Error Recovery in Predictive Parsing
I An error detected during predictive parsing when the
terminal on top of the stack does not match the next input
symbol.
I when nonterminal A is on top of the stack, a is the next
input symbol, and the parsing table entry M[A, a] is empty.

Samit Biswas Compiler Design


Some heuristics for the choice of Synchronizing Set are as
follows:
1. As a starting point, place all symbols in FOLLOW (A) into
the synchronizing set for the nonterminal A. Skip the
tokens until an element of FOLLOW (A) is seen and pop A
from the stack, it is likely that parsing can continue.
2. If we add symbols in FIRST(A) to the synchronizing set for
non terminal A, then it may be possible to resume parsing
according to A if a symbol in FiRST(A) appears in the input.
3. If a terminal on top of the stack cannot be matched, a
simple idea is to pop the terminal, issue a message saying
that the terminal was inserted and continue the parsing.

Samit Biswas Compiler Design


FIRST(E) = FIRST(T) = FIRST(F) = {(, id}
FIRST(E’) ={ +, }
FIRST(T’) ={*,}
FOLLOW(E) = FOLLOW(E’) ={),$}
FOLLOW(T)=FOLLOW(T’)={+,),$ }
FOLLOW(F) = {*, +, ), $}

Table: Synchronizing Tokens added to Predictive Parsing Table


Non Terminal id + ∗ ( ) $
E E → TE 0 E → TE 0 Synch Synch
0 0 0 0
E’ E → +TE E → E →
T T → FT 0 Synch T 0 → FT 0 Synch Synch
T’ T0 →  T 0 → ∗FT 0 T0 →  T0 → 
F F → id Synch Synch F → (E) Synch Synch

Samit Biswas Compiler Design


I Synch the driver pops current nonterminal A and skips
input till synch token or skips input until one of FIRST(A) is
found
I If the parser looks up entry M[A,a] and finds that it is blank,
the input symbol a is skipped.
I If the entry is synch, the the nonterminal on top of the
stack is popped.
I If a token on top of the stack does not match the input
symbol, then we pop the token from the stack.

Samit Biswas Compiler Design


Table: Parsing and error recovery moves made by predictive parser.
Stack Input Output
(1) $E ) id * + id $ error, Skip )

Samit Biswas Compiler Design


Table: Parsing and error recovery moves made by predictive parser.
Stack Input Output
(1) $E ) id * + id $ error, Skip )
(2) $E id * + id $ id is in FIRST(E)

Samit Biswas Compiler Design


Table: Parsing and error recovery moves made by predictive parser.
Stack Input Output
(1) $E ) id * + id $ error, Skip )
(2) $E id * + id $ id is in FIRST(E)
(3) $E’T id * + id $

Samit Biswas Compiler Design


Table: Parsing and error recovery moves made by predictive parser.
Stack Input Output
(1) $E ) id * + id $ error, Skip )
(2) $E id * + id $ id is in FIRST(E)
(3) $E’T id * + id $
(4) $E’T’F id * + id $
(5) $E’T’id id * + id $
(6) $E’T’ * + id $
(7) $E’T’F* * + id $

Samit Biswas Compiler Design


Table: Parsing and error recovery moves made by predictive parser.
Stack Input Output
(1) $E ) id * + id $ error, Skip )
(2) $E id * + id $ id is in FIRST(E)
(3) $E’T id * + id $
(4) $E’T’F id * + id $
(5) $E’T’id id * + id $
(6) $E’T’ * + id $
(7) $E’T’F* * + id $
(8) $E’T’F + id $ error, M[F, +] = Synch

Samit Biswas Compiler Design


Table: Parsing and error recovery moves made by predictive parser.
Stack Input Output
(1) $E ) id * + id $ error, Skip )
(2) $E id * + id $ id is in FIRST(E)
(3) $E’T id * + id $
(4) $E’T’F id * + id $
(5) $E’T’id id * + id $
(6) $E’T’ * + id $
(7) $E’T’F* * + id $
(8) $E’T’F + id $ error, M[F, +] = Synch
(9) $E’T’ + id $ F has been popped

Samit Biswas Compiler Design


Table: Parsing and error recovery moves made by predictive parser.
Stack Input Output
(1) $E ) id * + id $ error, Skip )
(2) $E id * + id $ id is in FIRST(E)
(3) $E’T id * + id $
(4) $E’T’F id * + id $
(5) $E’T’id id * + id $
(6) $E’T’ * + id $
(7) $E’T’F* * + id $
(8) $E’T’F + id $ error, M[F, +] = Synch
(9) $E’T’ + id $ F has been popped
(10) $E’ + id $
(11) $E’T+ + id $
(12) $E’T id $
(13) $E’T’F id $
(14) $E’T’id id $
(15) $E’T’ $
(16) $E’ $
(17) $ $

Samit Biswas Compiler Design


Phrase Level Recovery

Phrase Level Recovery in Predictive Parsing


I Each cell can be filled with a special-purpose error routine.
I Such routines typically remove tokens from the input,
and/or pop an item from the stack.
I It is ill-advised to modify the input stream or the stack
without removing items, because it is then hard to
guarantee that error recovery will always terminate

Samit Biswas Compiler Design


Phrase Level Recovery in Predictive Parsing
Change input stream by inserting missing tokens
For example: id id is changed into id * id

Table: Phrase Level entry added to Predictive Parsing Table


Non Terminal id + ∗ ( ) $
E E → TE 0 E → TE 0 Synch Synch
E’ E 0 → +TE 0 E0 →  E0 → 
T T → FT 0 Synch T → FT 0 Synch Synch
T’ Insert * T0 →  T 0 → ∗FT 0 T0 →  T0 → 
F F → id synch Synch F → (E) Synch Synch

Note: insert *: driver inserts missing * and retries the production

Samit Biswas Compiler Design


Error Production

Recovery using Error Production in Predictive Parsing


I Include productions for common errors.
I As for example, to ignore missing * in id id
I Add Error production : T 0 → FT 0

Table: Error Production added to Predictive Parsing Table

Non Terminal id + ∗ ( ) $
E E → TE 0 E → TE 0 Synch Synch
0 0 0 0
E’ E → TE E → E →
T T → FT 0 Synch T 0 → FT 0 Synch Synch
T’ T 0 → FT 0 T0 →  T 0 → ∗FT 0 T0 →  T0 → 
F F → id synch Synch F → (E) Synch Synch

Note: Powerful recovery method but there is a need of


manual addition of productions.

Samit Biswas Compiler Design


Error Handling in LR Parsing

Phrase level recovery - for each error entry in the table insert
a pointer to a particular error procedure, which assumes the
most likely cause and takes the appropriate action.
Consider the following grammar production rules
1. E → E + T
2. E → T
3. T → T ∗ F
4. T → F
5. F → (E)
6. F → id

Samit Biswas Compiler Design


Table: SLR Parsing Table For the given grammar
State Action Goto
id + * ( ) $ E T F
0 S5 e1 e1 S4 e2 e1 1 2 3
1 e3 S6 e4 e3 e2 Accept
2 e3 r2 S7 e3 r2 r2
3 e3 r4 r4 e3 r4 r4
4 S5 e1 e1 S4 e2 e1 8 2 3
5 e3 r6 r6 e3 r6 r6
6 S5 e1 e1 S4 e2 e2 9 3
7 S5 e1 e1 S4 e2 e2 10
8 e3 S6 e4 e3 S11 e5
9 e3 r1 S7 e3 r1 r1
10 e3 r3 r3 e3 r3 r3
11 e3 r5 r5 e3 r5 r5

Samit Biswas Compiler Design


Error Procedures:
e1 /* Expecting an id or an “(” , but finding an ’+’ , ’*’ or ’$’ */
put 5 on top of the stack
issue “missing operand” message.
e2 /* Finding an Unexpected ’)’ */
remove ’)’ from input. /* ignore it */
issue “Unmatched right parentheses ” message.
e3 /* Expecting ’+’ , finding id or ’(’ */
put 6 on top of the stack, /* assume ’+’ */
issue “missing + ” message.
e4 /* Expecting ‘+’ , finding ‘*’ */
put 6 on top of the stack, /* assume ’+’ */
remove ’*’ from input.
issue “ ‘*’ instead of ‘+’ ”message.
e5 /* Expecting ’)’, finding ‘$’ */
put 11 on stack /* assume ‘)’ */
issue “missing right parenthesis” message.

Samit Biswas Compiler Design


Parser Generators — Yacc

Yacc
Yacc
Specification y.yab.c
Compiler
translate.y

C
y.yab.c a.out
Compiler

input a.out output

Samit Biswas Compiler Design

You might also like