Interaction of Yacc / Bison Parser with a program in C ++

This is not a duplicate of this question , because the solution should not use a parser!

I have a Bison parser that I can execute by executing ./parser < file_to_parse. I want to call the Bison parser from my C ++ program. I do not want to do system(./parser < file_to_parse)it because it assumes that the parser is precompiled, and then my whole program will not be platform independent.

If I have a function:

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

then how can this be done? Any ideas? I think this is a case of calling yyparse or something else, but I can't handle it anywhere!

Thank:).

+5
source share
3 answers

, ++?

.

flex ++, , .

flex ++, . , :

// 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();
+2

, - yyparse(). yylex() , . , yylex() getchar() -, ; , -, , .

+1

C, , ++.
, YY_DECL .l :

#define YY_DECL int alpha_yylex(yylval_) void *yylval_;

int alpha_yylex(yylval_) - , void *yylval - .
, , , , .

(extern int alpha_yylex(void *yylval_);) .y.
Flex/Bison .c.

, .l - , .y - . , -, union .y ():

%union {

   union {
      int Integer;
      double Double;
      char Character;
      char* String;
   } value;

}

, , yylval.value.String = strdup(yytext) .l file , , $$.Double = $1.Double + $3.Double .y.

, , , , :)

P.S: ( FAQ -, , โ†’ , xD: D xD)

+1

All Articles