5.3 Action Symbols

The action terms specify the semantic rules by which value of LHS of a grammar rule is computed in terms of the values on the RHS. The augmented grammar simultaneously specifies two aspects – syntax and semantics – bare grammar rules specify the syntax and the action symbols specify the semantic rules.

For example, for the partial grammar for expr, the action symbols for generating RPN as the Intermediate code can be shown as below:

E : T { + <pop> T <out>}
T : F { * <pop> F <out>} 
F : a <pop> <out> |( E )

with a proviso that Terminals ‘+’, ‘*’ and ‘a’ push themselves. Compare this against the example code given in Section 5.2. Note that the nature of interpretation and position of the action symbols depend upon the output we want.

5.3.1 Action Symbols in yacc

Symbol Values

Every symbol in a yacc-based parser has a value. The value gives additional information about a particular instance of a symbol. For example, if a symbol represents a number, the value would be the particular number. If it represents a literal text string, the value would normally be a pointer to a copy of the string. If it represents a variable in a program, the value would be a pointer to a symbol table entry for the variable. Some tokens do not have a useful value, e.g. a token representing a closed parenthesis, it is a delimiter – it may trigger some action, but has no value. Non-terminal symbols can have any values you want, as decided by the semantics of the language and created by the action code in the parser. Usually, the action code builds a semantic tree corresponding to the input, so that later phases can process a whole statement or even a whole program at a time.

In real parsers, the values of different symbols use different data types, e.g. int and double for numeric symbols, char * for strings. We have seen previously that yacc handles multiple value types by creating a C union typedef called YYSTYPE to contain them.

Whenever the parser reduces by a rule, it executes user-specified C code associated with the rule, known as the rules action. The action appears in braces after the end of the rule, before the semicolon or a vertical bar. The action code can refer to the values of the RHS symbols in the rule as $1, $2, etc. and can set the value of the LHS by setting $$.

Embedded Actions in yacc

Sometimes we have to get some action be executed during the recognition of a grammar rule. Embedded actions in yacc provide this facility. Rules in yacc may contain embedded actions:

list: item1 { do_item1($1); } item2 { do_item2($3); } item3

Note that the actions take up a cell in the Semantic stack, so do_item2 must use $3 to reference item2. Actually, this grammar is transformed by yacc into the following:

list: item1 _rule01 item2 _rule02 item3 
_rule01: { do_item1($0); }
_rule02: { do_item2($0); }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.138.178