ERROR HANDLING Lecture on 27/08/2013 PPT: 11CS10037 SAHIL ARORA Syntax Error Handling ● Next few lectures will focus on the nature of syntactic errors and general strategies for error-recovery. ● If a compiler had to process only correct programs, its design would be extremely simple. However it is expected to Assist the programmer in locating and tracking errors. ● One should note that a programming language does not specify how a compiler should respond to errors. It is actually left entirely to the compiler designer. Review: Common Programming Errors ■ Semantic Errors: Type mismatches between operators and operands. Like, a return statements with return type void. ■ Syntactic Errors: Misplaced semicolons, extra or missing braces. ■ Lexical errors: Misspellings of keywords, identifiers, and operators. ■ Logical Errors : Incorrect reasoning or plain carelessness might result in errors like interchangeably using = and == operators. Challenges of Error Handling Viable-Fixable property: detecting an error as soon as a prefix of the input cannot be completed to form a string of the language. Goals of an Error Handler: ● Reporting presence of errors, clearly and accurately. ● Recovering from errors quickly enough to detect subsequent errors. ● Add minimal overhead to the processing of correct programs. Error recovery ● There is no universally acceptable method. ● The simplest method is for the parser to quit with the appropriate error message, at the first instance of an error. ● Problem? Subsequent errors will not be detected. ● Solution? If the parser can restore itself to a state where processing can continue, future errors can be detected. ● In some cases compiler stops if errors pileup. Error Recovery Strategies ● Panic Mode Recovery: The parser discovers an error. It then discards input symbols till a designated set of synchronizing token is found. ● Synchronizing tokens selected are such that their role in the program is unambiguous, like Delimiters ; } etc. Advantage? Simple and never goes into an infinite loop. ● Drawback: Skips considerable amount of input when checking for additional errors Phrase-level Recovery ● Local Correction by parser on remaining input, by some string which allows parser to continue. ● Replacing comma by semicolon, inserting extra semicolon etc. ● Drawbacks: Improper replacement might lead to infinite loops. More importantly, if the error has occurred before the point of detection. ● Advantage: It can correct any input string. Error Productions ● A method of anticipating common errors that might be encountered. ● Augmenting the grammar for the language at hand, with productions that generate erroneous constructs. ● Such a parser will detect anticipated errors when an error production is used. ● Advantage: Error diagnostics will be readily available for such anticipated errors. Global Corrections ● Ideally minimum changes should be made to the input string. ● Given an input string x, algorithm finds parse tree for a related string y, such that number of insertions, deletions, and token changes required for converting x to y is minimum. ● Drawback: Too costly to implement in terms of both time and space, and only theoretical. ● It is however used as a yardstick for evaluating error-recovery techniques.