[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

C++ pure lexer/parser

From: Hans Aberg
Subject: C++ pure lexer/parser
Date: Sun, 13 Jan 2002 13:24:16 +0100

I made a simple pure C++ Flex lexer/Bison parser combination. The tweaks
are relatively simple, so I think I should report them. I would be great if
there is support for a fullely pure C++ parser.

First, I use a compile style where the Flex lex.my.cc and the Bison
my.tab.cc sources are compiled indpendently, by using the my.tab.h header.
I write a special my.h header, defining types, etc. for use in the

A problem with using Bison's %pure_parser and Flex's C++ class is that the
latter is only accepting yylex() empty arguments. So in order to get around
that, I altered FlexLexer.h to
  class FlexLexer {
    virtual int yylex(YYLEX_ARG) = 0;
and similarly for class yyFlexLexer, so that one can alter the arguments.

Then, in my.h, I put
  #include "pg_parser.tab.h"

  #define YYLEX_ARG YYSTYPE& yylval, YYLTYPE& yylloc

  #define YY_DECL int yyFlexLexer::yylex(YYLEX_ARG)
  #define yyFlexLexer pg_parserFlexLexer

  #include "FlexLexer.h"

If the idea is to use also say Bison's %locations. I also changed the Bison
skeleton file, os that under C++, one uses references:
#  ifdef YYLEX_PARAM
#   define YYLEX                yylex (yylval, yylloc, YYLEX_PARAM)
#  else
#   define YYLEX                yylex (yylval, yylloc)
#  endif
# else /* !YYLSP_NEEDED */
#  ifdef YYLEX_PARAM
#   define YYLEX                yylex (yylval, YYLEX_PARAM)
#  else
#   define YYLEX                yylex (yylval)
#  endif
# endif /* !YYLSP_NEEDED */
#else /* !YYPURE */
# define YYLEX                  yylex ()
#endif /* !YYPURE */

I also zip out #include <FlexLexer.h> from the Folex skelton file, as it
now is included from my.h.

Then that is pretty much it: my.l and my.y need to #include "pg_parser.h".
Also, my.y currenlty has global:
  FlexLexer* mylexer;
  inline int yylex(YYSTYPE& x, YYLTYPE& y) { return mylexer->yylex(x, y); }
with mylexer created dynamically, clearly an impurity.

  Hans Aberg

reply via email to

[Prev in Thread] Current Thread [Next in Thread]