DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Parsing with yacc

Accessing values in enclosing rules

An action may refer to values returned by actions to the left of the current rule. The mechanism is simply the same as with ordinary actions, $ followed by a digit.

   sent   :   adj  noun  verb  adj  noun
          {
              look at the sentence ...
          }
          ;
   adj    :   THE
          {
                   $$ = THE;
          }
          |   YOUNG
          {
                   $$ = YOUNG;
          }
          ...
          ;
   noun   :   DOG
          {
              $$ = DOG;
          }
          |   CRONE
          {
              if( $0 == YOUNG )
              {
                  (void) printf( "what?\n" );
              }
              $$ = CRONE;
          }
          ;
          ...

In this case, the digit may be 0 or negative. In the action following the word CRONE, a check is made that the preceding token shifted was not YOUNG. Obviously, this is only possible when a great deal is known about what might precede the symbol noun in the input. Nevertheless, at times this mechanism prevents a great deal of trouble especially when a few combinations are to be excluded from an otherwise regular structure.


Next topic: Support for arbitrary value types
Previous topic: Simulating error and accept in actions

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004