0
votes

I create an .exe FILE, which can parser an expression, which is generated by lex and yacc. But I do it just get the input from screen, and just return the parser result from screen. I saw some suggestions about using YY_BUFFER_STATE yy_scan_buffer(char *base, yy_size_t size), but I still could not find a good way to do it.

Is it possible that I put some headers (which is compiled by lex yacc) to my main program c++, and then I can use yylex() to call it, giving a string as input, and get the return value in the main program? Thanks for your help, I am confused about how to realize it. Thanks.

1
I retagged this as gnu-flex b/c it has nothing to do w/ Adobe Flex.JeffryHouser
@www.Flextras.com: you're right; the gnu-flex tag is for GNU Flex.Jonathan Leffler
If you've created an executable (.exe) from the Lex and Yacc code, then you aren't going to be able to use it as a subroutine in a C++ program, so I'm confused about that part of your question. If you're asking whether you can organize a lexical scanner so that instead of reading from standard input, it will read from a string, the answer is 'yes, but how depends on whether you're using AT&T Lex or GNU Flex or some other variant of Lex'. Please clarify.Jonathan Leffler
For example I got lex.yy.c, frame.tab.c frame.tab.h (frame.l,frame.y has already generated), so I want to use them in my main program. I want to include them as headers, and I can use some functions to call the parser. Ex. function1(&string1), then it will be parser.CJAN.LEE

1 Answers

1
votes

yy_scan_string is how you give flex a string as input. You call that first, and then call yylex and it will use that string as the input to get tokens from rather than stdin. When you get an EOF from yylex, it has scanned the entire string. You can then call yy_delete_buffer on the YY_BUFFER_STATE returned by yy_scan_string (to free up memory) and call yy_scan_string again if you want to scan a new string.

You can use yy_scan_buffer instead to save a bit of copying, but then you have to set up the buffer properly yourself (basically, it needs to end with two NUL bytes instead of just one).

Unfortunately, there's no standard header file from flex declaring these. So you need to either declare them yourself somewhere (copy the declarations from the flex documentation), or call them in the 3rd section of the .l file, which is copied verbatim to the end of the lex.y.c file.