delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/04/17/11:08:51

Sender: crough45 AT amc DOT de
Date: Thu, 17 Apr 1997 15:42:15 +0100
From: Chris Croughton <crough45 AT amc DOT de>
Mime-Version: 1.0
To: eliz AT is DOT elta DOT co DOT il
Cc: djgpp AT delorie DOT com
Subject: Re: HELP: Bison, Flex & C++ class support...
Message-Id: <97Apr17.164001gmt+0100.21892@internet01.amc.de>

Eli Zaretskii wrote:

> What version of flex did you use?  The latest port of flex 2.5.4
> (v2gnu/flx254b.zip) comes with a program flexpp.exe that should be used
> for creating C++ lexers.  A header file FlexLexer.h should also be used 
> in C++ lexers.  I believe this is all explained in the flex docs.

I haven't managed to get the C++ lexer class to work (or even compile).
I think that what the original message was asking about was compiling
the non-class lexer as C++ rather than creating a lexer class.  If
anyone's got the C++ lexer class stuff to work, and used it with bison
generated code, I'd be interested in hearing from them.

The answer is that it can be done, but you have to add some
declarations to make sure that everything is in the right form.
Basically, this means making sure that functions are declared 
before they're used, which is essential in C++ although it often
doesn't matter in C (and therefore bison in particular is rather
lax about declaring functions, often using the old K&R declarations
to be compatible with old compilers).

In this case, I added the following:

At the top of cplus.y:

  int yylex(void);

At the top of cplus.l:

  int yyerror(char *x);

(both times within the %{ %} block).  That solved the problems (except
that my Unix system here doesn't have libfl.a, apparently, so I had
to provide my own yywrap function).

Note that if you do provide your own yywrap function you have to put
an extern "C" before it for C++, because that's how it's declared in
the lexer file (presumably so you can compile the flex output as C++
in an otherwise C program!).

(This answer was made a lot easier by the fact that I had to do exactly 
the same thing a couple of months ago, and I have the code lying around
to compare.  The man/info pages don't say much about this sort of
thing.)

Chris

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019