UP PREVIOUS NEXT

Lexical Tie-ins

Some lexical decisions depend on context. For example, the lexical analyzer might want to delete blanks normally, but not within quoted strings. Or names might be entered into a symbol table in declarations, but not in expressions.

One way of handling this situation is to create a global flag that is examined by the lexical analyzer, and set by actions. For example, suppose a program consists of 0 or more declarations, followed by 0 or more statements. Consider:

%{
	int dflag;
%}
  ...  other declarations ...

%%

prog	:	decls  stats
	;

decls	:	/* empty */
			{	dflag = 1;  }
	|	decls  declaration
	;

stats	:	/* empty */
			{	dflag = 0;  }
	|	stats  statement
	;

    ...  other rules ...
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 kind of ``backdoor'' approach can be elaborated to a noxious degree. Nevertheless, it represents a way of doing some things that are difficult, if not impossible, to do otherwise.


UP PREVIOUS NEXT