With each grammar rule, the user may associate actions to be performed each time
the rule is recognized in the input process.
These actions may return values, and may obtain the values returned by previous
Moreover, the lexical analyzer can return values
An action is an arbitrary C statement, and as such can do
input and output, call subprograms, and alter
external vectors and variables.
An action is specified by
one or more statements, enclosed in curly braces ``{'' and ``}''.
{ printf("a message\en");
are grammar rules with actions.
To facilitate easy communication between the actions and the parser, the action statements are altered
The symbol ``dollar sign'' ``$'' is used as a signal to Yacc in this context.
To return a value, the action normally sets the
pseudo-variable ``$$'' to some value.
For example, an action that does nothing but return the value 1 is
To obtain the values returned by previous actions and the lexical analyzer, the
action may use the pseudo-variables $1, $2, . . .,
which refer to the values returned by the
components of the right side of a rule, reading from left to right.
for example, then $2 has the value returned by C, and $3 the value returned by D.
As a more concrete example, consider the rule
expr : \'(\' expr \')\' ;
The value returned by this rule is usually the value of the
expr : \'(\' expr \')\' { $$ = $2 ; }
By default, the value of a rule is the value of the first element in it ($1).
Thus, grammar rules of the form
frequently need not have an explicit action.
In the examples above, all the actions came at the end of their rules.
Sometimes, it is desirable to get control before a rule is fully parsed.
Yacc permits an action to be written in the middle of a rule as well
This rule is assumed to return a value, accessible
through the usual \$ mechanism by the actions to
In turn, it may access the values
returned by the symbols to its left.
to the value returned by C.
Actions that do not terminate a rule are actually
handled by Yacc by manufacturing a new nonterminal
symbol name, and a new rule matching this
name to the empty string.
The interior action is the action triggered off by recognizing
Yacc actually treats the above example as if
In many applications, output is not done directly by the actions;
rather, a data structure, such as a parse tree, is constructed in memory,
and transformations are applied to it before output is generated.
Parse trees are particularly easy to
construct, given routines to build and maintain the tree
For example, suppose there is a C function
creates a node with label L, and descendants n1 and n2, and returns the index of
Then parse tree can be built by supplying actions such as:
{ $$ = node( \'+\', $1, $3 ); }
The user may define other variables to be used by the actions.
Declarations and definitions can appear in
the declarations section,
enclosed in the marks ``%{'' and ``%}''.
These declarations and definitions have global scope,
so they are known to the action statements and the lexical analyzer.
could be placed in the declarations section,
accessible to all of the actions.
The Yacc parser uses only names beginning in ``yy'';
the user should avoid such names.
In these examples, all the values are integers: a discussion of
values of other types will be found in Section 10.