Programming Utilities Guide

Lexical Tie-Ins

Some lexical decisions depend on context. For example, the lexical analyzer might normally delete blanks, but not within quoted strings, or names might be entered into a symbol table in declarations but not in expressions. One way of handling these situations is to create a global flag that is examined by the lexical analyzer and set by actions. For example,

%{ 
       	int dflag; 
%} 
...  other declarations ...  
%% 
prog 	   : decls stats 
       		; 
decls    :	    	/* empty */ 
        	{ 
          			dflag = 1; 
        	} 
        	| decls declaration 
        	; 
stats	   :	   	/* empty */ 
         { 
          			dflag = 0; 
         } 
        	| stats statement 
         ; 

.  .  .  other rules .  .  .

specifies a program consisting of zero or more declarations followed by zero or more statements. The flag dflag is now 0 when reading statements and 1 when reading declarations, except for the first token in the first statement.

This token must be seen by the parser before it can tell that the declaration section has ended and the statements have begun. In many cases, this single token exception does not affect the lexical scan. This approach represents a way of doing some things that are difficult, if not impossible, to do otherwise.