DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
yacc

yacc specifications

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
   %%
   subroutines
The example that follows is a complete yacc specification. The sample program generates a parser which takes input in the form:
   month day , year
This input is converted to output in the form:
   day month year
In 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''.


NOTE: While the lexical analyzer may be included as a routine defined in the specification file, it is more usual to define it elsewhere.

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.


Next topic: Terminal and non-terminal symbols
Previous topic: yacc

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