Open System Services Shell and Utilities Reference Manual (G06.28+, H06.05+)

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 denitions 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 le, 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
les that contain the program are as follows:
calc.l The lex specication le that denes the lexical analysis rules.
calc.y The yacc grammar le that denes 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 les.
Compiling the Example Program
Perform the following steps to create the example program using lex and yacc:
1. Process the yacc grammar le using the -d ag. The -d ag tells yacc to create a le that
denes the tokens it uses in addition to the C language source code.
yacc -d calc.y
2. The following les are created:
y.tab.c The C language source le that yacc created for the parser.
y.tab.h A header le containing #dene statements for the tokens used by the parser.
3. Process the lex specication le:
lex calc.l
4. The following le is created:
lex.yy.c The C language source le that lex created for the lexical analyzer.
5. Compile and link the two C language source les:
cc -o calc y.tab.c lex.yy.c
6. The following les are created (the *.o les are created temporarily and then removed):
y.tab.o The object le for y.tab.c.
lex.yy.o The object le for lex.yy.c.
1040 Hewlett-Packard Company 527188-007