Writing a Command-Parser in Arduino (IPO)

Hello everyone,

I am currently working on a project where I put data in, process it and then put it out. That means I put in some bytes which can represent a command or an argument and these should be processed.

An example, the following task: (7 * 15) + 4 + 8
The structure of the input would look like this:
Add(Multiply(7; 15)Close; 4; 8)Close
All in all we have a sum, so the top command must be "add", then we have a product, so the second command must be Multiply for the values 7 and 15. "Add" adds the result of the Multiplication (in this case 105) and the other values/arguments, in this case 4 and 8.

I now want to write an algorithm, which solves the task. My first thought was, to solve it with recursion: I first search for the deepest command in the construction and then solve it. Afterwards the result of this is written as a value in the upper command, so the upper command can be solved and so on until there is no command left but a value which is the final result. I wrote an algorithm for that in Arduino and the following happened: As I tried to give the arduino a slightly more complicated task, it wont work. It just stops working. I first tried it with like 2 levels of recursion. Then when I tried a more complicated task with like 4 levels of recursion the Arduino seems not to be able to handle this. It justs stops working. I tried for example the inner commands which means less levels of recursion and this worked fine. So it looks like the Arduino just cant handle it.

I now want to try to implement it iterative but the problem would be that I would have to use dynamic arrays and I read that the Arduino is probably not very suitable for dynamic arrays either.

Here is my algorithm explained: I first read in the commands and values as bytes, a 1 (command) or 0 (value) marks the byte as command or value. Then I put all the bytes I got in form of String in an dynamic array. After that I go through the array, search for commands and put them on a stack until I find the closing command (I have one universal command that marks the end of a command). Then I remove the top command on the stack and execute it with the values in between the command and the closing command. Next I overwrite the executed command and the values in the dynamic array and fill the gap with the result of the task.

That would be the iterative algorithm, but I fear that this also wont work because of memory.

Can anyone think of another algorithm or does anyone have an idea what the problem is with my algorithms or how to fix them? I would be very grateful if anyone could help me.

I attached my code for the recursive implementation and a short explanation of the syntax of the input-bytes.
I also used the Stack-library which you can find and download here: Arduino Playground - StackArray Library

runThrough_recursive.ino (6.35 KB)

structure.txt (682 Bytes)

I forgot to mention:
Another idea of mine was to write the data to a static array and when I want to resize the array I just write the data to a new array and to somehow delete the old one. Would that be possible?

Arduino does not have much memory so recursion causes an issue. It is especially bad if the function you are calling recursively has parameters.

Is there some reason you are required to do this on an Arduino? You are not really using any of the Arduinos inputs/outputs except for the serial interface.

I suspect you would get some useful ideas by Googling "Arduino calculator"

...R

You also might want to google reverse polish notation, that is commonly used for parsing mathematical equations.

ToddL1962:
Is there some reason you are required to do this on an Arduino? You are not really using any of the Arduinos inputs/outputs except for the serial interface.

Yes there is a reason to use the Arduino. Actually I am using the inputs of the Arduino and I also later want to use its output but I only talked about the relevant part for the problem to save you some time.