How to get yacc/bison and lex/flex to pause file scanning?
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, spl开发者_如何学编程itting 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?
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.
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.
精彩评论