0% found this document useful (0 votes)
74 views9 pages

01 06-01 - Error Handling 13m03s

The document discusses different approaches to error handling in compilers. It describes four main types of errors that compilers detect: lexical, syntax, semantic, and correctness errors. It also outlines three approaches to error handling: panic mode, error productions, and automatic local or global correction. Panic mode is the simplest and most popular method, where the compiler discards tokens until it finds a synchronizing token to continue parsing.

Uploaded by

suraj kumar
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)
74 views9 pages

01 06-01 - Error Handling 13m03s

The document discusses different approaches to error handling in compilers. It describes four main types of errors that compilers detect: lexical, syntax, semantic, and correctness errors. It also outlines three approaches to error handling: panic mode, error productions, and automatic local or global correction. Panic mode is the simplest and most popular method, where the compiler discards tokens until it finds a synchronizing token to continue parsing.

Uploaded by

suraj kumar
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/ 9

Compilers

Error Handling

Alex Aiken
Error Handling
• Purpose of the compiler is
– To detect non-valid programs
– To translate the valid ones

• Many kinds of possible errors (e.g. in C)

Error kind Example Detected by …


Lexical …$… Lexer
Syntax … x *% … Parser
Semantic … int x; y = x(3); … Type checker
Correctness your favorite program Tester/User

Alex Aiken
Error Handling

• Error handler should


– Report errors accurately and clearly
– Recover from an error quickly
– Not slow down compilation of valid code

Alex Aiken
Error Handling

• Panic mode

• Error productions

• Automatic local or global correction

Alex Aiken
Error Handling
• Panic mode is simplest, most popular method

• When an error is detected:


– Discard tokens until one with a clear role is found
– Continue from there

• Looking for synchronizing tokens


– Typically the statement or expression terminators

Alex Aiken
Error Handling

• Consider the erroneous expression


(1 + + 2) + 3
• Panic-mode recovery:
– Skip ahead to next integer and then continue

• Bison: use the special terminal error to describe how


much input to skip
E  int | E + E | ( E ) | error int | ( error )

Alex Aiken
Error Handling
• Error productions
– specify known common mistakes in the grammar

• Example:
– Write 5 x instead of 5 * x
– Add the production E  … | E E

• Disadvantage
– Complicates the grammar

Alex Aiken
Error Handling

• Idea: find a correct “nearby” program


– Try token insertions and deletions
– Exhaustive search

• Disadvantages:
– Hard to implement
– Slows down parsing of correct programs
– “Nearby” is not necessarily “the intended” program
Alex Aiken
Error Handling
• Past
– Slow recompilation cycle (even once a day)
– Find as many errors in one cycle as possible

• Present
– Quick recompilation cycle
– Users tend to correct one error/cycle
– Complex error recovery is less compelling

Alex Aiken

You might also like