By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,490 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,490 IT Pros & Developers. It's quick & easy.

Flex, Bison and ifstream

P: n/a
Hello,
is there a way to feed an ifstream to a parser generated with flex and
bison? I could see that flex wants a FILE* for reading, but I only
have an ifstream (actually and ifstream*) for input.
It seems that flex++ can use an ifstream; but how does the generated
lexer then work with bison?
Or is there a (portable) way to get a FILE* from an ifstream?

Thanks in advance,
Oliver Gerlich

PS: can you recommend an open news server for reading and posting to
this newsgroup (as a replacement for Google Groups)?

Sep 7 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

ol***@gmx.de wrote:
Hello,
is there a way to feed an ifstream to a parser generated with flex and
bison? I could see that flex wants a FILE* for reading, but I only
have an ifstream (actually and ifstream*) for input.
It seems that flex++ can use an ifstream; but how does the generated
lexer then work with bison?


man flex

You can also use flex to generate a C++ scanner class, using
the -+
option (or, equivalently, %option c++), which is automatically
speci-
fied if the name of the flex executable ends in a $-1+,
such as flex++.
When using this option, flex defaults to generating the scanner
to the
file lex.yy.cc instead of lex.yy.c. The generated scanner
includes the
header file FlexLexer.h, which defines the interface to
two C++
classes.

....

yyFlexLexer( istream* arg_yyin = 0, ostream* arg_yyout = 0 )
constructs a yyFlexLexer object using the given
streams for
input and output. If not specified, the streams default
to cin
and cout, respectively.

Sep 7 '05 #2

P: n/a
ol***@gmx.de writes:
is there a way to feed an ifstream to a parser generated with flex and
bison? I could see that flex wants a FILE* for reading, but I only
have an ifstream (actually and ifstream*) for input.


For Flex, you can provide a replacement YY_INPUT macro that reads from a
stream, instead of from the FILE* yyin.

%{
#undef YY_INPUT
#define YY_INPUT(buf,result,max) (result = a_stream.readsome(buf,max))
%}

This only works with Flex - it's not portable to AT&T Lex, MKS Lex, etc.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
Sep 7 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.