Question

I have a C library that I'd like to interface to from C++ code without modifying the library. It has a yacc-generated front end that reads from yyin, which is a FILE *. I'd like to set yyin to some kind of emulation of a FILE * which reads from memory. Is there any hope of doing this in a portable (Linux, Mac, Windows) manner -- or is there another trick for making such a parser read from memory rather than a FILE *?

Was it helpful?

Solution

You can use fmemopen() on Linux. Unfortunately, not only is there no portable way to do this, but then again, fopen() isn't even really portable (it's been broken for a long time on Windows).

However, if your tokenizer is Flex, you can use yy_scan_buffer(). See String input to flex lexer.

OTHER TIPS

A yacc scanner will normally get tokens via a lexer, calling a function named yylex.

The lexer is what normally reads characters from an input file (or buffer, in your case). Assuming you're using Flex to generate the lexer, the usual "hook" for modifying how input is read is to re-define the YY_INPUT macro.

As @dietrich Epp mentioned, however, there are also yy_scan_string, yy_scan_buffer, and yy_scan_bytes. Whether these are more suitable for your purposes than defining your own YY_INPUT may be open to some question. Although I can't remember any of the details, my recollection is of having avoided them at times due to (at least perceived) lack of efficiency (or maybe it was just that it seemed to me that defining YY_INPUT was easier -- can't remember for sure).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top