Programming Utilities Guide

Accessing Values in Enclosing Rules

An action can refer to values returned by actions to the left of the current rule. The mechanism is the same as 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 can be 0 or negative. In the action following the word CRONE, a check is made that the preceding token shifted was not YOUNG. Notice, however, that 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.