|
|
A yacc specification consists of a mandatory rules section, and optional sections for definitions and user subroutines.
The declarations section for definitions, if present, must be the first section in the yacc program. The mandatory rules section follows the definitions; if there are no definitions, then the rules section is first. In both cases, the rules section must start with the delimiter %%. If there is a subroutines section, it follows the rules section and is separated from the rules by another %% delimiter. If there is no second %% delimiter, the rules section continues to the end of the file.
When all sections are present, a specification file has the format:
declarations %% rules %% subroutinesThe example that follows is a complete yacc specification. The sample program generates a parser which takes input in the form:
month day , yearThis input is converted to output in the form:
day month yearIn the example, the declarations section defines a data structure used to hold the values associated with tokens, and declares all the token names used in the rules section. The rules section contains one rule and an action associated with it. The subroutines section defines a function that is called in the action.
%union { char *text; int ival; } %token t_DAY %token t_MONTH %token t_YEAR %% date : t_MONTH t_DAY ',' t_YEAR { print_date ($2,$1,$4); }; %% void print_date(d,m,y) char *m; int d,y; { printf("%d %s %d\n",d,m,y); }
The parser uses a lexical analyzer that can return the tokens t_DAY, t_MONTH, and t_YEAR, and also can associate each token with some value. The mechanisms by which this may be implemented are discussed later in this chapter, and also in ``lex''.
Blanks, tabs, and newlines are ignored in a yacc specification, but they cannot appear in names or in multicharacter reserved symbols. Comments can appear wherever a symbol is legal. They are enclosed in / ... /, as in C.