This document summarizes error recovery strategies used by parsers. It discusses the role of parsers in validating syntax based on grammars and producing parse trees. It then describes several error recovery strategies like panic-mode recovery, phrase-level recovery using local corrections, adding error productions to the grammar, and global correction aiming to make minimal changes to parse invalid inputs.
5. Role of the Parser
• The parser obtains a string of tokens from the lexical
analyzer, and verifies that the string can be generated by the
grammar for the source language.
• The parser returns any syntax error for the source language
6. Types of parsers
The task of the parser is essentially to determine if and how
the input can be derived from the start symbol of the
grammar.
This can be done in essentially two ways
a. Top-down parser
b. Bottom-up parser
7. Top-down parser:
Top-down parsers build parse trees from the top (root) to
the bottom (leaves).
Input to the parser is scanned from left to right.
One symbol at a time.
Always expand the leftmostnon-terminal
8. Bottom-up parser:
Bottom-up parsers build parse trees from the leaves and work up to
the root.
Input to the parser is scanned from left to right.
One symbol at a time.
And just as efficient
9. The role of parser
interm
repres
errors
lexical
analyzer
parser
rest of
front end
symbol
table
source
program
parse
tree
token
get next
token
regular
expressions
•also technically part or
parsing
•includes augmenting info on
tokens in source, type
checking, semantic analysis
•uses a grammar to check
structure of tokens
•produces a parse tree
•syntactic errors and recovery
•recognize correct syntax
•report errors
10. Parsing Responsibilities
Syntax Error Handling:
a. Lexical: Misspellings
b. Syntactic: Omission, wrong order of tokens
c. Semantic: Incompatible types
d. Logical: Infinite loop / recursive call
12. Error-Recovery Strategies
The simplest approach is for the parser to quit with an informative
error message when it detects the first error.
Panic-mode recovery.
Phrase-level recovery.
Error-productions.
Global-correction.
13. Panic-Mode Recovery
This is used by most parsing methods.
The synchronizing tokens are usually delimiters, such as ; or }.
Skips a considerable amount of input without checking for additional
errors
It has the advantage of simplicity, and is guaranteed not to go
into an infinite loop.
14. Phrase-Level Recovery
Perform local correction on the remaining input;
A typical local correction is to replace a comma by asemicolon.
Delete an extraneous semicolon.
Insert a missing semicolon.
Disadvantage in coping with situations in which the actual error has
occurred before the point of detection.
15. Error Productions
Expand the grammar for the language at hand with productions that
generatethe
erroneous constructs.
16. Global Correction
Compiler to make as few changes as possible in processing an
incorrect input string.
Given an incorrect input string x and grammar G, algorithms will find a
parse tree for a related string y, such that the number of insertions,
deletions, and changes of tokens required to transform x into y is as
small as possible.
Not implemented.
Top-down parser:
Top-down parsing expands a parse tree from the start symbol to the leaves
Always expand the leftmost non-terminal
Bottom-up parser:
Start at the leaves and grow toward root
And just as efficient
Builds on ideas in top-down parsing
Preferred method in practice
Also called LR parsing
L means that tokens are read left to right
R means that it constructs a rightmost derivation -->
Majority of error processing occurs during syntax analysis NOTE: Not all errors are identifiable !! Which ones?