Next: Tie-in Recovery, Previous: Semantic Tokens, Up: Context Dependency [Contents][Index]
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 *); %} %% …
expr: IDENTIFIER | constant | HEX '(' { hexflag = 1; } expr ')' { hexflag = 0; $$ = $4; } | expr '+' expr { $$ = make_sum ($1, $3); } … ;
constant: INTEGER | STRING ;
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.