Search code examples
parsingbisonflex-lexer

Bison: yywrap() to parse same file exactly one more time?


I have some data consisting of numbers that I would like to parse with Bison after lexing with Flex. To do so I need to know the minimum and maximum of all my numbers - if I cheat and explicitly define these, I can do it.

I know I must eventually automatically find the maximum of all lines before I run the parser function for the first.

I thought I would use yywrap() but it does not return to the start.

Here is my yywrap(): it returns 0 once and then 1, but does not seem to return to the start in between. It remains at the end, and my test printf(s) appear side-by-side there.-

int wrap;
int yywrap()
{
if (wrap == 0) {wrap++;return 0;}
else {return 1;}
}

Currently
INPUT:

--
G0
G0
G0
e0
--
--
--
--
F0
F0
F0
D0
--
--
--

OUTPUT:

------------
67----------
67----------
67----------
--------63--
------------
------------
------------
------------
----65------
----65------
----65------
----------62
------------
------------
------------

Solution

  • The flex manual has this to say about using yywrap (emphasis added):

    When the scanner receives an end-of-file indication from YY_INPUT, it then checks the yywrap() function. If yywrap() returns false (zero), then it is assumed that the function has gone ahead and set up yyin to point to another input file, and scanning continues. If it returns true (non-zero), then the scanner terminates, returning 0 to its caller. Note that in either case, the start condition remains unchanged; it does not revert to INITIAL.

    So if you want to rewind(yyin), you need to do so yourself, in the yywrap function.