This question already has an answer here:
I'm trying to write a simple compiler in C++ for a language that I am making up for my lab. The compiler will have to be able to turn a string like this:
"do command_one 3 times"
into a data structure that represents this:
<command_one>, <command_one>, <command_one>
it should also be able to turn something more complicated like this:
"do do c_one do c_two 2 times 2 times c_thr 2 times"
into a data structure that represents this:
<c_one> <c_two> <c_two> <c_one> <c_two> <c_two> <c_thr> <c_one>
<c_two> <c_two> <c_one> <c_two> <c_two> <c_thr>
Another function will then read the commands out one by one.
These examples show all of the functionality that I would want from the compiler.
I'm having design issues for the compiler that I want to create. Initially, I just wanted to do some string manipulation and turn the input string into an array of integers representing commands. However, since the compiler will be outputting an arbitrarily large number of commands, and since arrays in C++ need to be initialized with a length, this could be a problem.
I am not fluent in C++ so I am hoping that a C++ whiz can show me a quick solution. I have already come up with the suggested solution on my own in Python, however, my solution in Python has to do with the ability to change the length of lists on the fly.
EDIT: I am also intending to use this code for an Arduino, which supports C++ code but not the standard libraries. I found the vector solution to work in my C++ code, but it will not work in the Arduino interface. Is there a way to do the compiling WITHOUT using vectors?
std::vector
? – cdhowie Oct 10 '14 at 16:07