|
|
With each grammar rule, you can associate actions to be performed when the rule is recognized. Actions may return values and may obtain the values returned by previous actions. Moreover, the lexical analyzer can return values for tokens if desired.
An action is an arbitrary C language statement and as such can do input and output, call subroutines, and alter arrays and variables. An action is specified by one or more statements enclosed in { and }. For example,
A : '(' B ')' { hello( 1, "abc" ); }and
XXX : YYY ZZZ { (void) printf("a message\n"); flag = 25; }are grammar rules with actions.
The $ symbol is used to facilitate communication between the actions and the parser, The pseudo-variable $$ represents the value returned by the complete action. For example, the action
{ $$ = 1; }returns the value of one; in fact, that is all it does.
To obtain the values returned by previous actions and the lexical analyzer, the action can use the pseudo-variables $1, $2, . . . $n. These refer to the values returned by components 1 through n of the right side of a rule, with the components being numbered from left to right. If the rule is
A : B C D ;then $2 has the value returned by C, and $3 the value returned by D. The rule
expr : '(' expr ')' ;provides a common example. One would expect the value returned by this rule to be the value of the expr within the parentheses. Since the first component of the action is the literal left parenthesis, the desired logical result can be indicated by
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
A : B ;frequently need not have an explicit action. In previous examples, all the actions came at the end of 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 as at the end. This action is assumed to return a value accessible through the usual $ mechanism by the actions to the right of it. In turn, it may access the values returned by the symbols to its left. Thus, in the rule below the effect is to set x to 1 and y to the value returned by C:
A : B { $$ = 1; } C { x = $2; y = $3; } ;
Actions that do not terminate a rule are 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 by recognizing this added rule. yacc treats the above example as if it had been written
$ACT : / empty / { $$ = 1; } ;where $ACT is an empty action.A : B $ACT C { x = $2; y = $3; } ;
In many applications, output is not done directly by the actions. 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 structure desired. For example, suppose there is a C function node written so that the call
node( L, n1, n2 )creates a node with label L and descendants n1 and n2 and returns the index of the newly created node. Then a parse tree can be built by supplying actions such as
expr : expr '+' expr { $$ = node( '+', $1, $3 ); }in the specification.
You may define other variables to be used by the actions. Declarations and definitions can appear in the declarations section enclosed in %{ and %}. These declarations and definitions have global scope, so they are known to the action statements and can be made known to the lexical analyzer. For example:
%{ int variable = 0; %}could be placed in the declarations section making variable accessible to all of the actions. You should avoid names beginning with yy because the yacc parser uses only such names. Note, too, that in the examples shown thus far all the values are integers. A discussion of values of other types may be found in ``Advanced topics''. Finally, note that in the following case
%{ int i; printf("%}"); %}yacc will start copying after %{ and stop copying when it encounters the first %}, the one in printf(). In contrast, it would copy %{ in printf() if it encountered it there.