Sign up ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Oct 13 '14 at 12:46

This question came from our site for professional and enthusiast programmers.

marked as duplicate by Chris Stratton, jfpoilpret, sachleen Oct 13 '14 at 21:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2  
Use std::vector? – cdhowie Oct 10 '14 at 16:07
2  
look into C++ vectors. – Trevor Hickey Oct 10 '14 at 16:07
7  
Compilers are difficult. Not knowing the language you are writing the compiler in sounds crazy. – crashmstr Oct 10 '14 at 16:07
    
It is, both were asking different questions originally (answers for C++ and answers in Arduino) but as I got an answer in C++ the mods moved the C++ question to the Arduino forums – Paul Terwilliger Oct 13 '14 at 17:26
    
To clarify, I did need answers for both C++ and Arduino since I needed the compiler on both systems. – Paul Terwilliger Oct 13 '14 at 17:26

2 Answers 2

up vote 3 down vote accepted

In C++ there is a std library container called vector you can use to create variable length arrays. You can use it like this:

#include <vector>
// ...
std::vector<int> commands;
// ...
// parse string and generate commands, add your commands with push_back
commands.push_back(new_command);
// and you can access elements in the array like normal
commands[0];
// and also see how many elements are in the array
commands.size();

I also want to mention the std stack, which I think would be a helpful class for managing the states of your nested loops. Each time your interpreter enters a new loop for example, push to the top of the stack how many times to run, and each iteration count down till 0 and pop (just as an example).

Edit: Because you can't use vectors C++ also supplies the new and delete keywords to dynamically allocate and delete memory. However as arduino has really low memory I don't recommend trying to implement an expansible array on this. This post here mentions that program memory and global memory is shared on 2K: new operator on arduino

You may just want to go with a reasonably sized static array on arduino considering the memory limitations. I would suggest using a pool or the new + delete stuff but any implementation you add to deal with memory management will take away space you need for your interpreter and its instructions.

Also I don't know what data type you've chosen to represent your instructions but char is probably best on the embedded implementation if your instruction set is small as it only occupies one byte each.

share|improve this answer
    
My instructions can simply be represented by numbers. Ex: 1 can mean cmd_one and 2 can mean cmd_two, etc... – Paul Terwilliger Oct 11 '14 at 21:20

After Edit:

Since you cant use vectors ...

... or use variable length arrays, if the compiler you are using it, supports it.

... the other viable solution is to get your own list or vector library, either from an existing open source library or implementing your own.

Cheers.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.