Lexical Tie-ins
Previous: <Semantic Tokens=>SemanticTo> * Next: <Tie-in Recovery=>TieinRecov> * Up: <Context Dependency=>ContextDep>

#Wrap on
{fH3}Lexical Tie-ins{f}

One way to handle context-dependency is the {fUnderline}lexical tie-in{f}: 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 {fEmphasis}hex ({fStrong}hex-expr{f}){f}.  After the keyword {fCode}hex{f} comes
an expression in parentheses in which all integers are hexadecimal.  In
particular, the token {fEmphasis}a1b{f} must be treated as an integer rather than
as an identifier if it appears in that context.  Here is how you can do it:

#Wrap off
#fCode
%\{
int hexflag;
%\}
%%

expr:   IDENTIFIER
        | constant
        | HEX '('
                \{ hexflag = 1; \}
          expr ')'
                \{ hexflag = 0;
                   $$ = $4; \}
        | expr '+' expr
                \{ $$ = make\_sum ($1, $3); \}
        
        ;

constant:
          INTEGER
        | STRING
        ;
#f
#Wrap on

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

The declaration of {fCode}hexflag{f} shown in the C declarations section of
the parser file is needed to make it accessible to the actions 
(\*Note <C Declarations=>CDeclarati>: The C Declarations Section).  You must also write the code in {fCode}yylex{f}
to obey the flag.

