[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Calling two times yyparse reloading yyin
From: |
Tim Van Holder |
Subject: |
Re: Calling two times yyparse reloading yyin |
Date: |
Tue, 29 Nov 2005 17:12:12 +0100 |
User-agent: |
Mozilla Thunderbird 1.0.6 (Windows/20050716) |
Ismael Moreno wrote:
> void cAssigNode::translate(cVM& vm) throw(std::exception)
> {
> char* Text;
> Text = strdup(RValue.c_str());
>
> FILE* yyin = fmemopen(Text, strlen(Text), "r");
> if(yyin == NULL)
> throw(cError("Can't open yyin stream"));
>
> yyparse();
yyparse() (well, yylex() really) can't (and won't) use your
function-local yyin.
Note that adjusting the code to use the global yyin won't necessarily
work; flex will only cleanly switch to the new yyin if it got an EOF
state on the original one. Using yyrestart(), you to tell flex/bison
that a new "file" should be processed, even if the current/previous
one wasn't fully processed (so that flex can clear its internal cache).
So put a "yyrestart(yyin);" before each yyparse() and things should
work.
Depending on your lexer you may want to add a function of your own
(say, yy_new_file()) to your .l file, that not only calls yyrestart(),
but also sets the lexer state back to INITIAL (so that a parse error
inside a state in the first yyparse() does not break the second
yyparse()).
You typically want to test yyparse()'s return value too, so that
you can detect parse errors.
> [snip]
> delete Text;
Memory allocated by malloc() (even indirectly via strdup()) should be
freed using free(), not delete.
I'm not sure the strdup() is even needed - I would expect that
fmemopen(RValue.c_str(), RValue.size(), "r");
works just as well.