Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interfacing a Yacc/Bison Parser with a C++ program

Tags:

c++

yacc

bison

This is not a duplicate of this question because the solution was not to use a parser!

I have a Bison parser I can run by doing ./parser < file_to_parse. I want to call the Bison parser from within my C++ program. What I don't want is to do system(./parser < file_to_parse) because that would assume the parser is pre-compiled and then my whole program would not be platform independant.

If I have a function:

void foo(file_name) {
   // call parser on file_name
}

then how can this be done? Any ideas? I think it's the case of calling yyparse or something but I'm not getting anywhere with it!

Thank you :).

like image 638
ale Avatar asked Oct 11 '22 13:10

ale


1 Answers

Are you using a version that is adapted to C++?

I use the one by Alain Coetmeur.

At the moment, I only have code handy for employing flex++, but it should show you how this goes.

When you run flex++ it will output a header file and an implementation file for a new class to do the parsing. Add these to your project, then write:

// Construct new instance of parser
::lex * plex = new lex();

// tell parser what file to parse
plex->yyin = _wfopen(L"C:/Documents and Settings/All Users/Application Data/QuietWake/eCrew/new_rules_to_be_parsed.txt",L"r");

// run the parser
int lex_return = plex->yylex();
like image 196
ravenspoint Avatar answered Nov 22 '22 09:11

ravenspoint