CD Chapter 3
CD Chapter 3
Error-Recovery Strategies
There are many different general strategies that a parser can employ to recover from a syntactic
error. Although no one strategy has proven itself to be universally acceptable, a few methods have
broad applicability. Here we introduce the following strategies;
a)Panic mode:- This is the simplest method to implement and can be used by most parsing
methods.
On discovering an error, the parser discards input symbols one at a time until one of a designated set
of synchronizing tokens is found.
The synchronizing tokens are usually delimiters, such as semicolon or end, whose role in the source
program is clear.
b) Phrase level:- On discovering an error, a parser may perform local correction on the remaining
input; that is, it may replace a prefix of the remaining input by some string that allows the parser
to continue.
A typical local correction would be to replace a comma by a semicolon. Delete an extraneous
semicolon, or insert a missing semicolon.
The choice of the local correction is left to the compiler designer. Of course, we must be careful to
choose replacements that do not lead to infinite loops,
3.2. Syntax Error Handling …cont
c) Error productions:- If we have a good Idea of the common errors that might be
encountered, we can augment the grammar for the language at hand with
productions that generate the erroneous constructs, We then use the grammar
augmented by these error productions to construct a parser, if an error production
is used by the parser, we can generate appropriate error diagnostic to indicate the
erroneous construct that has been recognized in the input.
.
Bottom-up Parsing
• We introduce a general style of bottom-up syntax analysis, known as shift-reduce
parsing. A much more general method of shift-reduce parsing, called LR parsing, LR
parsing is used in a number of automatic parser generators.
• Shift-reduce parsing attempts to construct a parse tree for an input string beginning at
the leaves (the bottom) and working up towards the r o d (the top). We can think of this
process as one of "reducing" a string w to the start symbol of a grammar.
• At each reduction step a particular substring matching the right side of a production is
replaced by the symbol on the left of that production, and if the substring is chosen
correctly at each step, a rightmost derivation is traced out in reverse
.
• 1. Handle Pruning
A rightmost derivation in reverse can be obtained by "handle
pruning," That is, we start with a string of terminals w that we wish to
parse. If w is a sentence of the grammar at hand. Then w = yn where
yn is the nth right-sentential form of some as yet known rightmost
derivation
Eg. Consider the following arithmetic grammar and reduces id1 + id2*id3 to the start
symbol E, The reader should observe that the sequence of right-sentential forms in
this example is just the reverse of the sequence in the first rightmost derivation
2. Stack Implementation of Shift-Reducing Parsing
• There are two problems that must be solved if we are to parse by
handle pruning. The first is to locate the substring to be reduced in a
right-sentential form, and the second is to determine what
production to choose in case there is more than one production with
that substring on the right side.
• Before we get to these questions, let us first consider the type of data
structures to use in a shift-reduce parser, A convenient way to
implement a shift-reduce parser is to use a stack to hold grammar
symbols and an input buffer to hold the string w to be parsed. We use
$ to mark the bottom of the stack and also the right end of the input.
• Initially, the stack is empty, and the string w is on the input, as
follows:
2. Stack Implementation of Shift-Reducing Parsing ...
After entering this configuration, the parser halts and announces
successful completion of parsing. Example:
2. Stack Implementation of Shift-Reducing Parsing ...
Thank You!