Re: Reentrant Flex and Bison

Burkhard Perkens-Golomb <perkens@sdm.de>
7 Dec 1999 00:37:12 -0500

          From comp.compilers

Related articles
Reentrant Flex and Bison etetz@my-deja.com (2000-06-03)
Re: Reentrant Flex and Bison rhyde@shoe-size.com (Randall Hyde) (2000-06-06)
Reentrant Flex and Bison richard.lanyon@wadham.oxford.ac.uk (1999-11-16)
Re: Reentrant Flex and Bison perkens@sdm.de (Burkhard Perkens-Golomb) (1999-12-07)
| List of all articles for this month |
From: Burkhard Perkens-Golomb <perkens@sdm.de>
Newsgroups: comp.compilers
Date: 7 Dec 1999 00:37:12 -0500
Organization: sd&m AG, Muenchen, Germany
References: 99-11-087
Keywords: lex, yacc, practice

Our workaround (don't want to call it a solution):


File Lexer.h:
      // Declaration of the lexer class
      class LexerClass : public yyFlexLexer {


      [...]


          private:


              // A pointer to the union that holds the associated value to
              // the returned token
              char** lexerlval;


              // The method flex generates
              yylex ();


              // The method the bison parser expects: 1. variante
              yylex (void* lvalp) {
                  lexerlval = &((static_cast<YYSTYPE*>(lvalp)));
                  return yylex();
              }


              // The method the bison parser expects: 2. variante
              yylex (YYSTYPE* lvalp) {
                  lexerlval = &lvalp;
                  return yylex();
              }


      }


File lexer.l:
    // a rule:
    {match_anything} { lexerlval* =3D strdup(YYText()); return ANYTOKEN; }




Of course you can have more than "char*" for the associated
values. And you can let bison generate two kinds of yylex:
yylex(void*) or yylex(YYSTYPE*). In the latter case you don't need the
static_cast.


Burkhard




richard.lanyon@wadham.oxford.ac.uk (Richard Lanyon) writes:


> I'm trying to get a lexer + parser combination going using flex and bison.
> The problem is that I seem to have to declare yylex (as int yylex()) in
> the C declarations section of the bison grammar file.
> This wouldn't necessarily be a problem (except that I'm sure I shouldn't
> /have/ to) except that if I then want to make a re-entrant (aka "pure")
> parser, yylex needs to be declared as
> int yylex(YYSTYPE *lvalp)
> and YYSTYPE isn't defined in the C declarations section of the Bison
> grammar file - it only comes into being in the Bison declaration section.


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.