Programming Utilities Guide

C++ Mangled Symbols

The material for this section is an exact duplication of material found in the "C++ Mangled Symbols" section of Chapter 2, Lexical Analysis. Please substitute yacc when they refer to lex.

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.

Reserved Words

Some programming languages permit you to use words like if, which are normally reserved as label or variable names, provided that such use does not conflict with the legal use of these names in the programming language. This is extremely hard to do in the framework of yacc.

It is difficult to pass information to the lexical analyzer, telling it this instance of if is a keyword and that instance is a variable. Using the information found in the previous section, "Lexical Tie-Ins " might prove useful here.