help-bison
[Top][All Lists]
Advanced

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

C++ and Bison


From: Christoph B.
Subject: C++ and Bison
Date: Wed, 22 Mar 2006 12:45:28 +0100
User-agent: Debian Thunderbird 1.0.7 (X11/20051017)

Hi everybody!

Although I'm completely new to building parsers I successfully created a
lexer file and a bison grammar file. Finally my parser worked as
expected but now I'd like to put the parsed content into internal data
structures.
Unfortunately I soon discovered that bison and lex cannot handle C++
class types in the YYSTYPE union defnition and I did not find a way to
get this working.

Does anyone have a clue what to do in this case?

My first idea was to replace bison with bison++, but apart from much
more warnings the error message that said that my classes could't be
used in the union still remains.

g++ -c  lex.yy.c -o lex.yy.o
mof.y:71: error: 'Entry' does not name a type

I also read something about flex++, I am using Debian sid and I only
found packages flex and flex-old, so I am not quite sure if one of these
packages really is flex++ but as I am in doubt about that I will soon
install flex++ from source.

Unfortunately I did not find any good tutorials based on flex++/bison++,
so what I'd basically like to know is what changes to my lex/yacc files
are necessary to get them working with bison++ (and maybe flex++).
Can I use the exactly same files (I fear that won't work...)? And how do

This is what my lexer file looks like:
================================
%{
#define YY_SKIP_YYWRAP
#include <stdio.h>
#include <string.h>
#include "y.tab.h"
#include "entry.h" //class that I would like to use in the union
int yywrap()
{
        return 1;
}
;
%}
%%
token                yylval.string=strdup(yytext);return TOKEN;
[...]
%%
================================


My bison grammar file:
================================
%{
#define YY_SKIP_YYWRAP
#include <iostream>
#include <stdio.h>
#include "entry.h"
[...]
int yylex(void);
int yyparse(void);
void yyerror(const char *str) {fprintf(stderr,"error: %s\n",str);}
int main(void) { yyparse();}
%}
%union
{
        int number;
        char *string;
        Entry foobar;   //does not work for some bison reason.
}
%token <string> TOKEN
[...]
%type <string> nonterminals
%%
//description of grammar
================================

I'm a complete newbie to lex and yacc and started a few days ago with
this [1] tutorial. Unfortunately it is designed for C developers and
there is only a very short section for C++ that does not fit my needs.

Any help is gladly appreciated, thanks very much in advance!
Yours,
Christoph

[1] http://ds9a.nl/lex-yacc/cvs/lex-yacc-howto.html




reply via email to

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