2
votes

Im trying to parse a file using Bison/Yacc but i want to pause the parsing for a while. The reason i want to do this is that i want to process a huge file sequentially and simulate a java iterator using hasNext() and next() methods.

A trivial example would be, splitting a file by line using yacc so i could call:

while(myYaccObj.hasNext())
{
    std::string line = myYaccObj.next()
}

I cant find how to "pause" the file scanning. Is there a way to do that?

2
Due to a decision taken by over-eager meta users the [flex] tag means the Adobe product and for the lexer generator you need to use the mis-named [gnu-flex].dmckee --- ex-moderator kitten
What do yo mean "PAUSE" the scanning! Flex generates a function (or method: yylex() ) when you call this function it reads ONE token (lexeme) then returns.Martin York
I suspect the problem is that Tlol isn't using Flex directly, @Martin. Instead, he's using Bison, and Bison implicitly calls yylex whenever it needs to. And a common Bison usage is to call yyparse once to generate one big AST. I think Tlol wants to know how to parse a file incrementally instead of in a single yyparse call.Rob Kennedy
Basically the code you want to write is already implemented inside of bison. So the question really becomes what are you really trying to do? Why do you need to 'PAUSE' (whatever that means) after each line? Would a call back at the end of each line help?Martin York

2 Answers

2
votes

The easiest way is just to do the pause directly in you action code. For example, you could have a rule:

rule:  whatever  { Pause(); }
;

This would call your Pause function which could pause and/or do whatever you want. When you want to continue parsing, simply have the Pause function return.

0
votes

In fact pausing for me means "keep the state and finish the yyparse" call. For example in my gramar I would do:

rule:
    SaveLine;
    Pause;

And then the control is returned to my code. I do what i have to do and then I call:

yyparse_resume();

and the parsing continues until another pause or EOF.