I insist on using lex and not the flex.
am developing an API in lex like the one existing in the flex util ( yy_switch_buffer, yy_create_buffer …) offering the possibility to switch between multiple buffers .
This is the main difficulty for me until now :
- for example when I encounter a
#includetoken i should switch the buffer to the included file. So first i should interrupt the current parsing action( I triedfclose(yyin)FAILED) the parser complete the whole current yyin. NOT good because I should parse the included file to store structures (for example ) used in the main file.
Question is : How can I interrupt immediately a parser ? Is it enough for me to define a new buffer using yyin = fopen(somefile, "r"); ??
It is going to be tough to handle it, if it is doable at all. AFAIK, Lex only allows you to switch input streams on EOF (real or simulated) when it calls
yywrap().Maybe you can fake things so that when you find the ‘include’ directive, you fake an EOF on the current stream and then have
yywrap()fix things so that the new input comes from the included file, and then when you reach EOF on the included file, you haveyywrap()restore input from the original input stream at the original position. Clearly, this works for nested includes (if it works at all) unless you arbitrarily restrict the number of levels of inclusion.