Error token

The token name error is provided by yacc to allow the user some control over this process. This name can be used in grammar rules. In effect, it suggests places where errors are expected and recovery might take place. If an error occurs, the parser pops its stack until it enters a state where the token error is legal. It then behaves as if the token error were the current look-ahead token, and performs the action encountered. The look-ahead token is then reset to the token that caused the error.

In order to prevent a cascade of error messages, after detecting an error, the parser remains in an error state until three tokens have been successfully read and shifted. If an error is detected when the parser is already in error state, no message is given, and the input token is ignored.

As an example, the following rule means that on a syntax error the parser will pop its state stack until the symbol stat is valid. It will then act as if stat has been seen and perform the code associated with the token error:

   stat    :   error
                   ... action code ...
           | ...
Error rules such as this are very general but difficult to control. Rules like the following are somewhat easier:
   stat   :   error  '.'
Here, when there is an error, the parser attempts to skip over the statement to the next period. Tokens following the error and preceding the next period cannot be shifted and are discarded. When the period is seen, this rule will be reduced and any action associated with it performed.

Next topic: Interactive error recovery
Previous topic: Error handling

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003