DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

(bison.info) Lexical Tie-ins

Info Catalog (bison.info) Semantic Tokens (bison.info) Context Dependency (bison.info) Tie-in Recovery
 
 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;
      %}
      %%
      ...
      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 C declarations section of
 the parser file is needed to make it accessible to the actions (
 The C Declarations Section C Declarations.).  You must also write the
 code in `yylex' to obey the flag.
 
Info Catalog (bison.info) Semantic Tokens (bison.info) Context Dependency (bison.info) Tie-in Recovery
automatically generated byinfo2html