The end of the input to the parser is signaled by a special token, called the end-marker. If the tokens up to, but not including, the end-marker form a structure which matches the start symbol, the parser function returns to its caller after the end-marker is seen; it accepts the input. If the end-marker is seen in any other context, it is an error. Actions 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 actions. Moreover, the lexical analyzer can return values for tokens, if desired. 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 ``}''. For example, A : '(' B ')' { hello( 1, "abc" ); } and XXX : YYY ZZZ { printf("a message\n"); flag = 25; } are grammar rules with actions. To facilitate easy communication between the actions and the parser, the action statements are altered slightly. 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 { $$ = 1; } 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. Thus, if the rule is A : B C D ; 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 in parentheses. This can be indicated by expr : '(' expr ')' { $$ = $2 ; }
The end of the input to the parser is signaled by a special token, called the end-marker. If the tokens up to, but not including, the end-marker form a structure which matches the start symbol, the parser function returns to its caller after the end-marker is seen; it accepts the input. If the end-marker is seen in any other context, it is an error. Actions 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 actions. Moreover, the lexical analyzer can return values for tokens, if desired. 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 ``}''. For example, A : '(' B ')' { hello( 1, "abc" ); } and XXX : YYY ZZZ { printf("a message\n"); flag = 25; } are grammar rules with actions. To facilitate easy communication between the actions and the parser, the action statements are altered slightly. 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 { $$ = 1; } 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. Thus, if the rule is A : B C D ; 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 in parentheses. This can be indicated by expr : '(' expr ')' { $$ = $2 ; }