Compiler Design Error detection and Recovery in Compiler This process of locating errors and reporting it to user is called Error Handling process. Functions of Error handler ● Detection ● Reporting ● Recovery Classification of errors:1. Compile time 2. Run time Compile time error is of 3 types 1. Lexical Phase error 2. Syntactic Phase error 3. Semantics Error Or Classification of Compile-time error – 1. Lexical : This includes misspellings of identifiers, keywords or operators 2. Syntactical : missing semicolon or unbalanced parenthesis 3. Semantical : incompatible value assignment or type mismatches between operator and operand 4. Logical : code not reachable, infinite loop. Finding error or reporting an error – Viable-prefix is the property of a parser which allows early detection of syntax errors. ● Goal: detection of an error as soon as possible without further consuming unnecessary input
● How: detect an error as soon as the prefix of the input does not match a prefix of any string in the language. Lexical phase errors These errors are detected during the lexical analysis phase. Typical lexical errors are ● Exceeding length of identifier or numeric constants. ● Appearance of illegal characters ● Unmatched string Syntactic phase errors These errors are detected during syntax analysis phase. Typical syntax errors are ● Errors in structure ● Missing operator ● Misspelled keywords ● Unbalanced parenthesis Semantic errors These errors are detected during semantic analysis phase. Typical semantic errors are ● Incompatible type of operands ● Undeclared variables ● Not matching of actual arguments with formal one It generates a semantic error because of an incompatible type of a and b.(e.g. Int a=b). Error recovery: 1. Panic Mode Recovery ○ In this method, successive characters from input are removed one at a time until a designated set of synchronizing tokens is found. Synchronizing tokens are deli-meters such as ; or }
○ Advantage is that it's easy to implement and guarantees not to go to infinite loop ○ Disadvantage is that a considerable amount of input is skipped without checking it for additional errors 2. Statement Mode recovery ● In this method, when a parser encounters an error, it performs necessary correction on remaining input so that the rest of input statement allow the parser to parse ahead. ● The correction can be deletion of extra semicolons, replacing comma by semicolon or inserting missing semicolon. ● While performing correction, at most care should be taken for not going in infinite loop. ● Disadvantage is that it finds difficult to handle situations where actual error occured before point of detection. 3. Error production ● If user has knowledge of common errors that can be encountered then, these errors can be incorporated by augmenting the grammar with error productions that generate erroneous constructs. ● If this is used then, during parsing appropriate error messages can be generated and parsing can be continued. ● Disadvantage is that its difficult to maintain. 4. Global Correction ● The parser examines the whole program and tries to find out the closest match for it which is error free. ● The closest match program has less number of insertions, deletions and changes of tokens to recover from erroneous input.
● Due to high time and space complexity, this method is not implemented practically. Error recovery ● If error “Undeclared Identifier” is encountered then, to recover from this a symbol table entry for corresponding identifier is made. ● If data types of two operands are incompatible then, automatic type conversion is done by the compiler. Abstract Syntax Trees Parse tree representations are not easy to be parsed by the compiler, as they contain more details than actually needed. Take the following parse tree as an example: