Turn Desktop View Off
by Dinesh Thakur

• An efficient program should not terminate on an parse error.

• It must recover to parse the rest of the input and check for subsequent errors.

• For one line input, the routine yyparse() can be made to return 1 on error and then calls yyparse() again.

YACC program error handling

• Parser must be capable of detecting the error as soon as it encounters, i.e., when an input stream does not match the rules in grammar.

• If there is an error-handling subroutine in the grammar file, the parser can allow for entering the data again, ignoring the bad data or initiating a cleanup and recovery action.

• When the parser finds an error, it may need to reclaim parse tree storage, delete or alter symbol table entries and set switches to avoid generating further output.

• Error handling routines are used to restart the parser to continue its process even after the occurrence of error.

• Tokens following the error get discarded to restart the parser.

• The YACC command uses a special token name error, for error handling. The token is placed at places where error might occur so that it provides a recovery subroutine.

• To prevent subsequent occurrence of errors, the parser remains in error state until it processes three tokens following an error.

• The input is discarded and no message is produced, if an error occurred while the

parser remains in error state.

(eg.) stat : error ‘;’

o The above rule tells the parser that when there is an error, it should ignore the token and all following tokens until it finds the next semicolon.

o It discards all the tokens after the error and before the next semicolon.

o Once semicolon is found, the rule is reduced by parser and cleanup action associated with that rule will be performed.

Providing for error correction

The input errors can be corrected by entering a line in the data stream again.

input : error '\n'

{

printf ("Reenter last line:");

}

input

{

$$ = $4;

} ;

The YACC statement, yyerrok is used to indicate that error recovery is complete.

This statement leaves the error state and begins processing normally.

input : error '\n'

yyerrok;

printf ("Reenter last line:");

}

input

{

$$ = $4;

};

Clearing the Lookahead token

 

• When an error occurs, the lookahead token becomes the token at which the error was detected.

• The lookahead token must be changed if the error recovery action includes code to find the correct place to start processing again.

• To clear the lookahead token, the error-recovery action issues the following statement:

yyclearin;

To assist in error handling, macros can be placed in YACC actions.

                                            Macros for error handling

 

YYERROR

Causes the parser to initiate error handling.

YYABORT

Causes the parser to return with a value of 1.

YYACCEPT

Causes the parser to return with a value of 0.

YYRECOVERING()

Returns a value of 1 if a syntax error has

been detected and the parser has not yet fully

recovered.