Question

I'm trying to write a parser using flex++ and bison. I need the parser to be able to read from a file and write a new file in output.

I have an yyFlexLexer instantiated as follows:

yyFlexLexer lexer;

and I use it:

int main(int argc, char* argv[])
{
    std::istream* in_file = new std::ifstream(argv[1])
    std::ostream* out_file = new std::ofstream(argv[2])
    lexer.switch_streams(in_file, out_file);
    yyparse();
    return 0;
}

If I run:

./executable foo bar

the parser correctly read the file foo (I can see it making some printing in bison rules) but at the end I found only an empty file called "bar" without anything in it.

I have also tried to do this:

int main(int argc, char* argv[])
{
    std::istream* in_file = new std::ifstream(argv[1])
    std::ostream* out_file = new std::ofstream(argv[2])
    lexer.switch_streams(in_file, out_file);
    while(lexer.yylex())
    ;
    return 0;
}

but it does the same thing.

No correct solution

OTHER TIPS

You need the parser's actions (in your .y file) to build some abstract syntax tree of the parsed input, and you need some routine to pretty print that tree to the output.

flex and bison do only the parsing (which is not much). You should do the rest.

I don't understand what you want to achieve.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top