The yacc environment

When yacc processes a specification, the output is a file of C code, named The function produced by yacc is called yyparse(), and it is an integer-valued function. When yyparse is called, it repeatedly calls yylex(), the lexical analyzer supplied to obtain input tokens. If the lexical analyzer returns the end-marker token and the parser accepts the input, yyparse() returns the value 0. If an error is detected, yyparse() returns the value 1, and no error recovery is possible.

A main() routine that calls yyparse() must be defined. In addition, a routine called yyerror() is needed to print a message when a syntax error is detected.

The user must supply these two routines. A library has been provided with default versions of main() and yyerror() The library is accessed by using the -ly option to cc(CP) or ld(CP). The following source code shows the simplicity of these default programs:

             return (yyparse());
   # include <stdio.h>

yyerror(s) char *s; { (void) fprintf(stderr, "%s\n", s); }

The argument to yyerror() is a string containing an error message, usually the string syntax error. An application may require more sophisticated error reporting. The program should keep track of the input-line number and print it with the message when a syntax error is detected. The external integer variable yychar contains the look-ahead token number at the time the error was detected. This may be useful in giving better diagnostics.

The external integer variable yydebug is normally set to 0. If not, the parser outputs a verbose description of its actions, including the input symbols read and the parser actions. You can set this variable by using a debugger (adb, sdb, or dbxtra).

Next topic: Lexical analysis
Previous topic: Subroutines

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