11.5. Using Only Synthesized Attributes


S-attributed equations

A set of attribute equations is S-attributed if all of its attributes are synthesized.

S-attributed equations are the easiest to use with a bottom-up parser, since they can be handled on a single bottom-to-top pass.


Abstract syntax trees can help you avoid inherited attributes

Normally, you only need synthesized attributes to create abstract syntax trees. Then, once you have the tree, you can traverse it up and down as many times as you have a mind to.


Synthesized attributes of embedded actions in Bison

Recall that Bison allows you to intersperse an action among the tokens and nonterminals on the right-hand side of a production. Those embedded actions can have synthesized attributes.

If the synthesized attribute uses field intval of YYSTYPE, define $<intval>$. To use that in other actions for the same production, use $<intval>k, where k is the position of the action.

For example, suppose that you want an action that sets a context and remembers the current line number.

expr : TOK_FOO
         {insideFoo = 1;
          $<intval>$ = line_number;
         }
       expr  ','  expr  TOK_ENDFOO
         {
           $$ = handleFoo($3, $5, $<intval>2);
         }
     ;