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

Lexical tie-ins

Some lexical decisions depend on context. For example, the lexical analyzer might normally want to delete blanks, but not within quoted strings; 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 set by actions in the parser but which can be accessed by the lexical analyzer. Then the lexical analyzer can make decisions on the basis of this variable's value. The following specification specifies a program that consists of zero or more declarations followed by zero or more statements:

   %{
      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 set to 0 when reading statements and 1 when reading declarations. The first token in the first statement must be seen by the parser before it can tell that the declarations section has ended and the statements have begun. In many cases, this single token exception does not affect the lexical scan.

Next topic: Reserved words
Previous topic: Left recursion

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003