Bell 32V development
[unix-history] / usr / doc / yacc / ss2
CommitLineData
2240a03d
TL
1.SH
22: Actions
3.PP
4With each grammar rule, the user may associate actions to be performed each time
5the rule is recognized in the input process.
6These actions may return values, and may obtain the values returned by previous
7actions.
8Moreover, the lexical analyzer can return values
9for tokens, if desired.
10.PP
11An action is an arbitrary C statement, and as such can do
12input and output, call subprograms, and alter
13external vectors and variables.
14An action is specified by
15one or more statements, enclosed in curly braces ``{'' and ``}''.
16For example,
17.DS
18A : \'(\' B \')\'
19 { hello( 1, "abc" ); }
20.DE
21and
22.DS
23XXX : YYY ZZZ
24 { printf("a message\en");
25 flag = 25; }
26.DE
27are grammar rules with actions.
28.PP
29To facilitate easy communication between the actions and the parser, the action statements are altered
30slightly.
31The symbol ``dollar sign'' ``$'' is used as a signal to Yacc in this context.
32.PP
33To return a value, the action normally sets the
34pseudo-variable ``$$'' to some value.
35For example, an action that does nothing but return the value 1 is
36.DS
37 { $$ = 1; }
38.DE
39.PP
40To obtain the values returned by previous actions and the lexical analyzer, the
41action may use the pseudo-variables $1, $2, . . .,
42which refer to the values returned by the
43components of the right side of a rule, reading from left to right.
44Thus, if the rule is
45.DS
46A : B C D ;
47.DE
48for example, then $2 has the value returned by C, and $3 the value returned by D.
49.PP
50As a more concrete example, consider the rule
51.DS
52expr : \'(\' expr \')\' ;
53.DE
54The value returned by this rule is usually the value of the
55.I expr
56in parentheses.
57This can be indicated by
58.DS
59expr : \'(\' expr \')\' { $$ = $2 ; }
60.DE
61.PP
62By default, the value of a rule is the value of the first element in it ($1).
63Thus, grammar rules of the form
64.DS
65A : B ;
66.DE
67frequently need not have an explicit action.
68.PP
69In the examples above, all the actions came at the end of their rules.
70Sometimes, it is desirable to get control before a rule is fully parsed.
71Yacc permits an action to be written in the middle of a rule as well
72as at the end.
73This rule is assumed to return a value, accessible
74through the usual \$ mechanism by the actions to
75the right of it.
76In turn, it may access the values
77returned by the symbols to its left.
78Thus, in the rule
79.DS
80A : B
81 { $$ = 1; }
82 C
83 { x = $2; y = $3; }
84 ;
85.DE
86the effect is to set
87.I x
88to 1, and
89.I y
90to the value returned by C.
91.PP
92Actions that do not terminate a rule are actually
93handled by Yacc by manufacturing a new nonterminal
94symbol name, and a new rule matching this
95name to the empty string.
96The interior action is the action triggered off by recognizing
97this added rule.
98Yacc actually treats the above example as if
99it had been written:
100.DS
101$ACT : /* empty */
102 { $$ = 1; }
103 ;
104
105A : B $ACT C
106 { x = $2; y = $3; }
107 ;
108.DE
109.PP
110In many applications, output is not done directly by the actions;
111rather, a data structure, such as a parse tree, is constructed in memory,
112and transformations are applied to it before output is generated.
113Parse trees are particularly easy to
114construct, given routines to build and maintain the tree
115structure desired.
116For example, suppose there is a C function
117.I node ,
118written so that the call
119.DS
120node( L, n1, n2 )
121.DE
122creates a node with label L, and descendants n1 and n2, and returns the index of
123the newly created node.
124Then parse tree can be built by supplying actions such as:
125.DS
126expr : expr \'+\' expr
127 { $$ = node( \'+\', $1, $3 ); }
128.DE
129in the specification.
130.PP
131The user may define other variables to be used by the actions.
132Declarations and definitions can appear in
133the declarations section,
134enclosed in the marks ``%{'' and ``%}''.
135These declarations and definitions have global scope,
136so they are known to the action statements and the lexical analyzer.
137For example,
138.DS
139%{ int variable = 0; %}
140.DE
141could be placed in the declarations section,
142making
143.I variable
144accessible to all of the actions.
145The Yacc parser uses only names beginning in ``yy'';
146the user should avoid such names.
147.PP
148In these examples, all the values are integers: a discussion of
149values of other types will be found in Section 10.