0

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.

Lesmana
  • 25,663
  • 9
  • 82
  • 87
CJAN.LEE
  • 1,108
  • 1
  • 11
  • 20
  • I retagged this as gnu-flex b/c it has nothing to do w/ Adobe Flex. – JeffryHouser Sep 23 '12 at 17:02
  • @www.Flextras.com: you're right; the `gnu-flex` tag is for GNU Flex. – Jonathan Leffler Sep 23 '12 at 17:12
  • 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 Sep 23 '12 at 17:17
  • See also [How to make YY_INPUT point to a string rather than stdin in Lex and Yacc](http://stackoverflow.com/questions/1920604/). – Jonathan Leffler Sep 23 '12 at 17:18
  • 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 Sep 23 '12 at 17:21
  • @JonathanLeffler: Flex is *not* GNU software. Never has been, never will be (it was funded, at least in part, by the US government, which imposes restrictions that are, among other things, entirely incompatible with all versions of the GPL and anything I can imagine as a possible future version either). – Jerry Coffin Oct 02 '12 at 05:57
  • @JerryCoffin: discuss that on MSO. The tag `gnu-flex` is the one that's been nominated to deal with the difference between Adobe Flex and Flex. Were it up to me, I'd use `adobe-flex` and `flex`, but there'd be a lot of people not paying enough attention who'd use `flex` when they should have used `adobe-flex`. – Jonathan Leffler Oct 02 '12 at 06:02

1 Answers1

1

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.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226