Parsing with yacc

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 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 that consists 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 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.

Next topic: Reserved words
Previous topic: Left recursion

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004