Lecture 17: Error Handling - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Lecture 17: Error Handling

Description:

'Compilers: Principles, Techniques and Tools', Aho, Sethi and Ullman ... normally replace or delete delimiters. bad choice of replacement may cause compiler to loop ... – PowerPoint PPT presentation

Number of Views:293
Avg rating:3.0/5.0
Slides: 15
Provided by: xxx3108
Category:

less

Transcript and Presenter's Notes

Title: Lecture 17: Error Handling


1
Lecture 17 Error Handling
  • lexical errors
  • syntax errors
  • error recovery
  • error handling in LR parsing
  • error handling in Yacc

2
Books in the library
  • "Introduction to computer theory", Cohen,D.
  • gentle walk through the formal langauge parts
  • "Compilers Principles, Techniques and Tools",
    Aho, Sethi and Ullman
  • authoritative (but getting old), concise, but
    with many examples
  • "Compiler construction principles and practice",
    Louden,K.
  • a simpler book than Aho et al, following the same
    route
  • "Modern compiler implementation in Java", Appel
  • "Lex and Yacc", Levine, Mason, Brown
  • everything you ever wanted to know about Lex and
    Yacc, and then some more ...

3
Error Handling
  • A compiler should
  • detect errors
  • locate errors
  • recover from errors
  • Errors may occur in any of the three analysis
    phases
  • lexical analysis
  • syntax analysis
  • semantic analysis
  • There are others
  • run-time errors
  • logical errors
  • but they are not trapped
  • by the compiler ...

4
Lexical Errors
  • few real possibilities
  • unexpected character
  • illegal sequence of characters
  • recovery strategy
  • deleting the offending characters from input and
    continue

5
Syntax Errors
  • most compile-time errors can be detected in this
    phase
  • the parser should
  • detect all syntactic errors
  • report errors cleanly and accurately
  • recover quickly and detect subsequent errors
  • not slow down the compiler
  • poor error recovery can cause an avalanche of
    error messages, for errors that don't occur in
    the program

6
Error Recovery Panic Mode
  • ignore all symbols until a synchronising token is
    found
  • e.g "end" or "" or "" etc
  • simple to implement
  • guaranteed to halt
  • ignores a lot of code

7
Error Recovery Phrase level
  • replace a prefix of the current input string by
    something else to allow the parser to continue
  • normally replace or delete delimiters
  • bad choice of replacement may cause compiler to
    loop
  • unable to recover if the error has already been
    shifted and is on the stack

8
Error recovery Error Productions
  • include extra productions in the grammar which
    recognise common errors
  • report them with standard rule actions
  • blend them into the parse tree and continue
  • require some knowledge of typical language use
  • ensures messages and recovery procedures are
    specific to the individual errors

9
Error recovery Global Correction
  • aim the compiler carries out the minimum number
    of changes to the program to get a syntactically
    correct version
  • algorithms exist to compute minimum change
  • requires a complete traversal of the program
    before errors can be identified
  • extremely expensive in space and time

10
Error handling in LR parsing
  • Detection detects errors at earliest possible
    point by reaching an error entry in the table
  • Error recovery phrase level
  • for each error entry in table, insert a pointer
    to a particular error procedure, which assumes
    the most likely cause, and takes the appropriate
    action.

11
0 1 2 3 4 5 6 7 8 9 10 11
S T F a ( ) 1 2 3 S5 e1 e1 S4 e
2 e1 e3 S6 e4 e3 e2 A e3
R2 S7 e3 R2 R2 e3 R4 R4 e3 R4 R4 8 2
3 S5 e1 e1 S4 e2 e1 e3 R6 R6 e3
R6 R6 9 3 S5 e1 e1 S4 e2
e1 10 S5 e1 e1 S4 e2 e1 e3
S6 e4 e3 S11 e5 e3 R1 S7 e3
R1 R1 e3 R3 R3 e3 R3 R3 e3
R5 R5 e3 R5 R5
12
Error Procedures
e1 / expecting an a or an "(", but finding an
"", "" or a "" / put 5 on top of stack /
assume a / issue "missing operand"
message e2 / finding an unexpected ")"
/ remove ")" from input / ignore it / issue
"unmatched right parenthesis" message e3 /
expecting "", finding a or "(" / put 6 on top
of stack / assume "" / issue "missing "
message e4 / expecting "", finding ""
/ put 6 on top of stack / assume ""
/ remove "" from input issue " instead of "
message e5 / expecting ")", finding ""
/ put 11 on stack / assume ")" / issue
"missing right parenthesis" message
13
Yacc Error Recovery
  • Add productions A -gt error a
  • When error is found, scan down stack until a
    state with a member A -gt error a is found.
  • Scan through input until a string matching a is
    found, and remove all input until the end of that
    string.
  • Parser then reduces to A, and continues.
  • Example
  • Statement -gt error

14
Next lecture ...
  • type checking
Write a Comment
User Comments (0)
About PowerShow.com