yacc(1) — Commands
OSF
NAME
yacc − Generates an LR(1) parsing program from input consisting of a context-free grammar specification
SYNOPSIS
yacc [-vltds] grammar
The yacc command converts a context-free grammar specification into a set of tables for a simple automaton that executes an LR(1) parsing algorithm.
FLAGS
-dProduces the y.tab.h file, which contains the #define statements that associate the yacc-assigned token codes with your token names. This allows source files other than y.tab.c to access the token codes by including this header file.
-lDoes not include any #line constructs in y.tab.c. Use this only after the grammar and associated actions are fully debugged.
-sBreaks the yyparse() function into several smaller functions. Because its size is somewhat proportional to that of the grammar, it is possible for yyparse() to become too large to compile, optimize, or execute efficiently.
-tCompiles run-time debugging code. By default, this code is not included when y.tab.c is compiled. If YYDEBUG has a nonzero value, the C compiler (cc) includes the debugging code, whether or not the -t flag was used. Without compiling this code, yyparse() will run more quickly.
-vProduces the y.output file, which contains a readable description of the parsing tables and a report on conflicts generated by grammar ambiguities.
DESCRIPTION
The yacc grammar can be ambiguous; specified precedence rules are used to break ambiguities.
You must compile the y.tab.c output file with a C language compiler to produce the yyparse() function. This function must be loaded with a yylex lexical analyzer function, as well as main() and yyerror(), an error-handling routine (you must provide these routines). The lex command is useful for creating lexical analyzers usable by yacc.
The yacc program reads its skeleton parser from the file /usr/ccs/lib/yaccpar. Use the environment variable PARSER to specify another location for yacc to read from.
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.lThe lex specification file that defines the lexical analysis rules.
calc.yThe 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 (the ∗.o files are created temporarily and then removed):
y.tab.cThe C language source file that yacc created for the parser.
y.tab.hA 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.cThe 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:
y.tab.oThe object file for y.tab.c.
lex.yy.oThe object file for lex.yy.c.
calcThe executable program file.
You can then run the program directly by entering:
calc
Then enter numbers and operators in calculator fashion. After you press <Return>, the program displays the result of the operation. If you assign a value to a variable as follows, the cursor moves to the next line:
m=4 <Return>
_
You can then use the variable in calculations and it will have the value assigned to it:
m+5 <Return>
9
The Parser Source Code
The text that follows shows the contents of the file calc.y. This file has entries in all three of the sections of a yacc grammar file: declarations, rules, and programs.
%{
#include <stdio.h>
int regs[26];
int base;
%}
%start list
%token DIGIT LETTER
%left ’|’
%left ’&’
%left ’+’ ’-’
%left ’∗’ ’/’ ’%’
%left UMINUS /∗supplies precedence for unary minus ∗/
list:/∗empty ∗/
|list stat ’\n’
|list error ’\n’
{yyerrok;}
;
stat:expr
{printf("%d\n",$1);}
|LETTER ’=’ expr
{regs[$1] = $3; }
;
expr:’(’ expr ’)’
{$$ = $2;}
|expr ’∗’ expr
{$$ = $1 ∗ $3;}
|expr ’/’ expr
{$$ = $1 / $3;}
|expr ’%’ expr
{$$ = $1 % $3;}
|expr ’+’ expr
{$$ = $1 + $3;}
|expr ’-’ expr
{$$ = $1 - $3;}
|expr ’&’ expr
{$$ = $1 & $3;}
|expr ’|’ expr
{$$ = $1 | $3;}
|’-’ expr %prec UMINUS
{$$ = -$2;}
|LETTER
{$$ = regs[$1];}
|number
;
number:DIGIT
{$$ = $1; base = ($1==0) ? 8:10;}
|numberDIGIT
{$$ = base ∗ $1 + $2;}
;
main()
{
return(yyparse());
}
yyerror(s)
char ∗s;
{
fprintf(stderr,"%s\n",s);
}
yywrap()
{
return(1);
}
Declarations Section
This section contains entries that perform the following functions:
•Includes standard I/O header file.
•Defines global variables.
•Defines the list rule as the place to start processing.
•Defines the tokens used by the parser.
•Defines the operators and their precedence.
Rules Section
The rules section defines the rules that parse the input stream.
Programs Section
The programs section contains the following routines. Because these routines are included in this file, you do not need to use the yacc library when processing this file.
main()The required main program that calls yyparse() to start the program.
yyerror(s)This error handling routine only prints a syntax error message.
yywrap()The wrap-up routine that returns a value of 1 when the end of input occurs.
The Lexical Analyzer Source Code
This shows the contents of the file calc.lex. This file contains include statements for standard intput and output, as well as for the y.tab.h file. The yacc program generates that file from the yacc grammar file information, if you use the -d flag with the yacc command. The file y.tab.h contains definitions for the tokens that the parser program uses. In addition, calc.lex contains the rules used to generate the tokens from the input stream.
%{
#include <stdio.h>
#include "y.tab.h"
int c;
extern int yylval;
%}
nerate the tokens
" ";
[a-z]{
c = yytext[0];
yylval = c - ’a’;
return(LETTER);
}
[0-9]{
c = yytext[0];
yylval = c - ’0’;
return(DIGIT);
}
[^a-z 0-9]{
c = yytext[0];
return(c);
}
FILES
y.outputA readable description of parsing tables and a report on conflicts generated by grammar ambiguities.
y.tab.cOutput file.
y.tab.hDefinitions for token names.
yacc.tmpTemporary file.
yacc.debugTemporary file.
yacc.actsTemporary file.
/usr/ccs/lib/yaccparDefault skeleton parser for C programs.
/usr/ccs/lib/liby.ayacc library.
RELATED INFORMATION
Commands: lex(1).
Guide to Programming Support Tools