LECTURE NOTES for 26/09/2005 Prepared by K

Total Page:16

File Type:pdf, Size:1020Kb

LECTURE NOTES for 26/09/2005 Prepared by K

LECTURE NOTES FOR 26/09/2005 prepared by K.Venu Gopal, 03CS1007

ERROR RECOVERY IN LR PARSERS

An Error is detected in a LR parser whenever we look at the Shift-Reduce Table and find that there is no action defined for that entry. None of the LR parsers will shift an erroneous symbol into the stack. The SLR and the LALR parsers might make several reductions before announcing an error but since a LR(1) parsers is an improved parser over the SLR and LALR, it will not make even a single reduction before announcing an error.

PANIC MODE ERROR RECOVERY

We scan down the stack until a state s with a goto on a particular nonterminal A is found. Zero or more input symbols are then discarded until a symbol ‘a’ is found that can legitimately follow A. The parser then stacks the state goto[s,A] and resumes normal parsing. By removing states from the stack, skipping over the input, and pushing goto[s,A] on the stack, the parser pretends that it has found an instance of A and resumes normal parsing.

ERROR FUNCTIONS

One more strategy is to use pre-defined functions to be executed when ever a blank entry in the Shift-Reduce Table is found.

Let us look at an example :-

E  E + E | E * E | ( E ) | id

The LR parsing table with error routines is given as below :-

Id + * ( ) $ E 0 s3 e1 e1 s2 e2 e1 1 1 e3 s4 s5 e3 e2 acc 2 s3 e1 e1 s2 e2 e1 6 3 r4 r4 r4 r4 r4 r4 4 s3 e1 e1 s2 e2 e1 7 5 s3 e1 e1 s2 e2 e1 8 6 e3 s4 s5 e3 s9 e4 7 r1 r1 s5 r1 r1 r1 8 r2 r2 r2 r2 r2 r2 9 r3 r3 r3 r3 r3 r3 The error routines are defined as follows :- e1 : /* This routine is called from 0,2,4 and 5 all of which expect the beginning of an operand, either an id or a left parenthesis. Instead an operator, + or *, or the end of the input was found. */ Push an imaginary id onto the stack and cover it with state 3 (the goto of states 0,2,4 and 5 on id) Emit(“ Missing operand”) e2 : /* This routine is called from the states 0,1,2 ,4 and 5 on finding a right parenthesis.*/ Remove the right parenthesis from the input. Emit(“ Unbalanced right parenthesis”) e3 : /* This routine is called from states 1 or 6 when expecting an operator, and an id or right parenthesis is found. */ push an operator say ‘+’ onto the stack and cover t with state 4 . Emit(“ Missing Operator”) e4 : /* This routine is called from state 6 when the end of the input is fond. State 6 expects an operator or a right parenthesis. */

Push a right parenthesis onto the stack and cover it with state 9 . Emit (“ Missing right parenthesis”)

Let us simulate the parsing actions when an erroneous input id + ) is inputted.

STACK INPUT ERROR MSG AND ACTION

0 id + ) $ 0 id 3 + ) $ 0 E 1 + ) $ 0 E 1 + 4 ) $ 0 E 1 + 4 $ “unbalanced right parenthesis “ e2 removes right parenthesis

0 E 1 + 4 id 3 $ “missing operand “ e1 pushes id 3 on stack

0 E 1 + 4 E 7 $

0 E 1 $ SYNTAX – DIRECTED TRANSLATION

When we want to translate a programming language construct , we not only store the generated code until now but we also need to work with many quantities like the type of the construct, where is it’s first appearance in the code, the number of instructions generated etc.,

So to handle these operations we give each construct an abstract set of attributes or properties like it’s type, it’s memory location etc.,

SYNTAX – DIRECTED TRANSLATION is one formalism used to handle the translations for programming language constructs.

We use Post-fix notation to our aid in the translation details of which were dealt with in the next class.

Recommended publications