You are not logged in.

#1 2007-11-06 11:58:13

LE_Shatai
Member
Registered: 2007-01-04
Posts: 26

flex/bison interpreter

Does someone know how to reset yyin from flex ?
I am trying to build an interpreter, and I have a >load< command, which loads a file.

So this is the code for loading a file:

void StackInterpreter::loadfile(char *name)
{
        string filename(name);
        filename = filename.substr(1,strlen(name)-2);

        if(freopen(filename.c_str(), "r", yyin) == NULL)
        {
                cout << "=> Error: File "<< filename << " could not be opened..." << endl;
                return;
        }

        cout << "=> File " << filename << " opened..." << endl;
}

Mention that I open the file directing to yyin.

As far as I know, I have to overwrite yywrap, as this function is called when EOF is read.
So I this is my yywrap

int yywrap()
{
        yyin = stdin;
        return 0;
}

Ok, I reset yyin to stdin and >return 0< to say, that yylex can go on.
And here is where the trouble starts.
I can type in commands as I want, it seems that they do not reach yylex, nor yyparse.

So what is wrong here? Did I miss(-understood) something ?

Hmm, maybe an example to make things clearer:

$ ./wip                                 #starting the interpreter
=> Interpreter ready... ( type >help< for command overview )                                  #interpreter says it's ready
load 'ex.wip'                                #telling the interpreter to load the file
=> File ex.wip opened...             #interpreter says file is loaded (which is true, as I checked this)
help                                             # from this point on no more commands are executed; means the help is not displayed etc..
debug
;
''
load''

CU
Martin

Offline

Board footer

Powered by FluxBB