Set yyin to "emulated" FILE *?

I have a C library with which I would like to interact with C ++ code without changing the library. It has a formed yacc front that reads from yyin , which is FILE * . I would like to set yyin to some kind of FILE * emulation that is read from memory. Is there any hope for this in a portable (Linux, Mac, Windows) manner - or is there another trick to make such a parser read from memory, rather than FILE * ?

+4
source share
2 answers

You can use fmemopen() on Linux. Unfortunately, not only is there no portable way to do this, but again, fopen() is not even very portable (it was corrupted for a long time on Windows).

However, if your tokenizer is Flex, you can use yy_scan_buffer() . See Entering a string in flex lexer .

+4
source

The yacc scanner usually receives tokens through the lexer, calling a function called yylex .

A lexer is what usually reads characters from an input file (or buffer, in your case). Assuming you are using Flex to generate lexer, the usual “hook” to change the way you read is to override the YY_INPUT macro.

However, as @dietrich Epp noted, there are also yy_scan_string, yy_scan_buffer and yy_scan_bytes. Whether they are more suitable for your purposes than defining your own YY_INPUT may be open to some kind of question. Although I cannot recall a single detail, my recollection is that from time to time they avoided them because of (at least the sensation) lack of effectiveness (or maybe it seemed to me that the definition of YY_INPUT was simpler - t remember for sure).

+2
source

Source: https://habr.com/ru/post/1485353/


All Articles