Open System Services Shell and Utilities Reference Manual (G06.25+, H06.03+)
Table Of Contents
yacc(1) OSS Shell and Utilities Reference Manual
}
int yyerror(s);
char *s;
{
fprintf(stderr,"%s\n",s);
return (0);
}
Comments, in C syntax, can appear anywhere in the user functions or definitions sections. In the
rules section, comments can appear wherever a symbol is allowed. Blank lines or lines consist-
ing of white space can be inserted anywhere in the file, and are ignored.
EXAMPLES
This section describes the example programs for the lex and yacc commands, which together
create a simple desk calculator program that performs addition, subtraction, multiplication, and
division operations. The calculator program also allows you to assign values to variables (each
designated by a single lowercase ASCII letter), and then use the variables in calculations. The
files that contain the program are as follows:
calc.l The lex specification file that defines the lexical analysis rules.
calc.y The yacc grammar file that defines the parsing rules and calls the yylex() function
created by lex to provide input.
The remaining text expects that the current directory is the directory that contains the lex and
yacc example program files.
Compiling the Example Program
Perform the following steps to create the example program using lex and yacc:
1. Process the yacc grammar file using the -d flag. The -d flag tells yacc to create a file that
defines the tokens it uses in addition to the C language source code.
yacc -d calc.y
2. The following files are created:
y.tab.c The C language source file that yacc created for the parser.
y.tab.h A header file containing #define statements for the tokens used by the parser.
3. Process the lex specification file:
lex calc.l
4. The following file is created:
lex.yy.c The C language source file that lex created for the lexical analyzer.
5. Compile and link the two C language source files:
cc -o calc y.tab.c lex.yy.c
6. The following files are created (the *.o files are created temporarily and then removed):
y.tab.o The object file for y.tab.c.
lex.yy.o The object file for lex.yy.c.
10−40 Hewlett-Packard Company 527188-003