YACC(2)

Table of Contents

Name

yacc declaration section for the yacc file

Synopsis

%yacc
yacc-declarations

Description

The yacc section of a source description file contains yacc text that is copied into the declaration section of the generated yacc file, yacc.y. This section is used to specify token precedence levels and associativities. Each line beginning with a percent sign must be preceded by an additional percent sign.

The yacc declarations must use the lex-yacc interface names rather than the token names specified in the grammar section. The lex-yacc interface name is obtained by translating the lower-case letters in a token name to upper-case, and then prepending T_ (e.g., the lex name defined in ``num:<[0-9]+>'' has the lex-yacc interface name T_NUM).

In certain circumstances unnamed literal tokens that are single characters can be used in yacc declarations by enclosing them in single quotes. However, if a literal token appears more than once in a grammar section, and not all occurrences of the token are merged, then the token should not be declared in the yacc section. If such a token must be declared, it should first be named, and its lex-yacc interface name used in the yacc section.

Example

In the following example the lex-yacc interface name for minus is used in the yacc section. This is because the minus token is used twice in the grammar. The prec clause is needed in the sixth alternative to avoid a grammar ambiguity.
%grammar
expr: ( asg:expr «=» expr
| add:expr «+» expr
| sub:expr minus expr
| mul:expr «*» expr
| div:expr «/» expr
| minus expr %prec `*'
| id
)
minus: «-"

id:
<[a-zA-Z]+>

%yacc
%%right `=' %%left `+' T_MINUS
%%left `*' `/'

See Also

GRAMMAR(2)
LEX(2)

Documentation

Yacc Yet Another Compiler-Compiler, by S.C. Johnson


© 1990 Lucent Technologies, Inc
© 1998 Harmony Software, Inc