80 likes | 266 Views
Error Recovery. CS 671 February 5, 2008. Previously. Top-Down Parsing, e.g. LL(1) Bottom-Up Parsing, e.g. LR(1) Next Question : What if the compiler finds a syntactic error?. Error Recovery. What if the compiler finds a syntactic error? Ideally, report all errors, not just first
E N D
Error Recovery CS 671 February 5, 2008
Previously • Top-Down Parsing, e.g. LL(1) • Bottom-Up Parsing, e.g. LR(1) • Next Question: • What if the compiler finds a syntactic error?
Error Recovery • What if the compiler finds a syntactic error? • Ideally, report all errors, not just first • Approaches: • Local error recovery – Find synch point, resume • Global error recovery – Change input, resume
Local Error Recovery • Informally, skip to next synchronizing token • Example exp ID exp exp + exp exp ( exps ) exps exp exps exps ; exp • Skip to next SEMICOLON or RPAREN • Resume parsing How?Introduce new terminal symbols and rules: exp ( error ) exps error ; exp
Caution… • Unfortunately, error states can cause problems when semantic actions are attached (unless we’re careful). Consider: • statements : statements exp SEMICOLON • | statements error SEMICOLON • | /* empty */ • exp : increment exp decrement • | ID • increment: LPAREN {nest=nest+1;} • increment: RPAREN {nest=nest-1;}
Global Error Repair • Another way to recover: insert/delete tokens before the point of the error • let type a := intArray [ 10 ] of 0 in … • ^ • (real error: type should be var) • Global repair: Find smallest set of insertions or deletions that would convert string to a syntactically correct string (not always at error point) • Outcome: type var (1 replacement)
Burke-Fisher Error Repair • Tries every possible single-token insertion, deletion, replacement up to K tokens before error. • Chooses: Whichever replacement, etc. allows it to progress farthest after error • Realistic limit: 4 tokens beyond error is “good enough” error K
Yacc Support for Error Correction • The %value directive (used during insertions) • %value ID (“bogus”) • %value INT (1) • %value STRING (“”) • Programmer specified substitutions • %change EQ -> ASSIGN | • ASSIGN -> EQ | • SEMICOLON ELSE -> ELSE | • -> RBRACE Scope closer