Domanda

I am making a very basic interpreter (using my own language) for the functions of set theory (union, intersection, etc.). I'm coding with C++ and currently doing my reading and parsing from .txt file. However, I'm trying to make it so code can be executed in a "command-by-command" way, without having the command window close. I'm hoping to be able to have multiple functions be read and performed one after another by using the carriage return.

Is there a way I can change my parser so that it will keep accepting commands/function instead of reading the entire .txt file at one time?

È stato utile?

Soluzione

Generally, when "parsing" something, the idea is to read token at a time, and not really care about lines and other such things.

If your language is structured that way, you could just read your language as a stream, and when you see a call to a function (or whatever you want to call it), execute that function as you go along [assuming you don't compile to machine code that requires the entire thing to be compiled at once, of course - if that's the case, you're in for a bit of work...]

But if you want to read a line at a time, then use the istream& getline(istream&, std::string&) method. It reads a single line. You then parse that line and do whatever makes sense with the result of the parsing.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top