Flex, Bison and ifstream

O

olig9

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)?
 
M

Maxim Yegorushkin

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.
 
S

Sherm Pendley

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--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top