help-flex
[Top][All Lists]
Advanced

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

flex++: Using yylval


From: Christoph B.
Subject: flex++: Using yylval
Date: Thu, 30 Mar 2006 15:38:57 +0200
User-agent: Mail/News 1.5 (X11/20060228)

Hi everybody!

I am working on my very first project including a parser, I decided to
use flex and bison. I finally got everything working fine, but I need
more than just one parser in that project and because I am coding in C++
and therefore prefer class structures, I decided to port my parser to
flex++ and bison++.

The only thing that still bothers me is that I cannot assign values to
the tokens in the lexer file. Previously, in the plain flex file, i had
something like that:

[a-zA-Z][a-zA-Z0-9_]*   yylval.text=strdup(yytext);  return V_NAME;

Just to check if my c++ parser and scanner classes are working, I
changed this in my flex++ file to

[a-zA-Z][a-zA-Z0-9_]*   { return Parser::V_NAME; }

The only problem that obviously remains is that I cannot pass the
content of the tokens to the 'text' element defined in the %union
section in the bison++ parser class. So how can I pass the content of
the tokens to my parser? Is there some replacement for yylval when using
flex++/bison++?

Maybe the problem is caused by some (bad?) hack I did. I could not
compile my scanner.cpp file because the compiler said:

/usr/include/FlexLexer.h:113: error: redefinition of 'class yyFlexLexer'
/usr/include/FlexLexer.h:113: error: previous definition of 'class
yyFlexLexer'

So what I did was to write a short script that removed line 319 of my
scanner.cpp file where it said "#include <FlexLexer.h>". When this line
was removed, the compiler succeeded. Passing the tokens to the bison++
parser also works, but as mentioned before, assigning values to those
tokens does not work.

When using yylval.text... inside the flex++ scanner file, g++ fails with:

scanner.l: In member function 'virtual int yyFlexLexer::yylex()':
scanner.l:116: error: 'yylval' was not declared in this scope

Do I have to include something else than the .h file generated by
bison++? I would be very glad if someone could tell me what lines of
code are missing in my flex++ and bison++ files.

Thanks very much in advance!
Yours,
Christoph

PS.: Another simple question: How can I tell my scanner to read from
specified files instead from stdin?

PPS.: I am also quite new to coding C++, so it would be very kind if you
could explain your solution as if I was just 5 years old... :-)





reply via email to

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