Next: , Previous: , Up: Handling Context Dependencies   [Contents][Index]

7.2 Lexical Tie-ins

One way to handle context-dependency is the lexical tie-in: a flag which is set by Bison actions, whose purpose is to alter the way tokens are parsed.

For example, suppose we have a language vaguely like C, but with a special construct ‘hex (hex-expr)’. After the keyword hex comes an expression in parentheses in which all integers are hexadecimal. In particular, the token ‘a1b’ must be treated as an integer rather than as an identifier if it appears in that context. Here is how you can do it:

  int hexflag;
  int yylex (void);
  void yyerror (char const *);
| constant
| HEX '('        { hexflag = 1; }
    expr ')'     { hexflag = 0; $$ = $4; }
| expr '+' expr  { $$ = make_sum ($1, $3); }


Here we assume that yylex looks at the value of hexflag; when it is nonzero, all integers are parsed in hexadecimal, and tokens starting with letters are parsed as integers if possible.

The declaration of hexflag shown in the prologue of the grammar file is needed to make it accessible to the actions (see The prologue). You must also write the code in yylex to obey the flag.