Search code examples
c++cbisonyacclex

Put the code generated by flex to a normal C++ program


I create a simple file, using flex, it generate a file lex.yy.c, for now, I want to put it to C++ program.

%{

 #include < stdio.h>

%}

%%

stop    printf("Stop command received\n");

start   printf("Start command received\n");

%%

When I type start or stop in command line, there is a output. What I want to do is to give the input by my C++ program, and the output of it should be sent to a variable in my program, is it possible? Thanks a lot!

I know the code I post is quite simple, but the result I imagine is:

create c file by flex and bison, and I use it as a header, so in the c++ program, I just need to call a function lex_yacc() to use it. ex. lex_yacc() is a calculator, so I sent an expression with evariables to this function, and it will return the result. I want to use this function in a C++ program, I am confused...Thanks a lot!


Solution

  • See the section about multiple input buffers in the manual. Especially the section about yy_scan_string and yy_scan_bytes.

    For the "output", of course the is "output" when you give "stop" or "start" as input, you explicitly do that (i.e. the printf calls). You can put any code you want there.