I need help parsing!!!

So im trying to make a simple drawing program that reads in (translate (rect 10 10 10 10) 50 50).... what im trying to do is split it so that the 50 50 goes with the translate and the rect keeps all of the 10's. This is a postscript fill and ive heard of hash tables and stacks but i am not sure how to use them. Ive done everything else... like all the calculations for the shapes and what not... i just dont understand how to parse the lines so that i can get the numbers pointing to the right variables. please help.

my program needed to work with (translate 50 50) (rect 10 10 10 10).. but now i need to figure out how to do the same thing but with (translate (rect 10 10 10 10) 50 50)
ive heard of ... stacks but i am not sure how to use them

Then I suggest you research them and learn how to use them:
http://www.cplusplus.com/reference/stl/stack/

Stacks were made for this type of problem.

This example:

(translate (rect 10 10 10 10) 50 50)

would go something like this:

1. See "(translate" -> push "translate" onto the stack
2. See "(rect" -> push "rect" onto the stack
3-6. See "10" -> Look at the top of the stack (find "rect"), and add 10 to the list of parameters for the instruction "rect"
7. See ")" -> pop the stack
8-9. See "50" -> Look at the top of the stack (find "translate"), and add 50 to the list of parameters for the instruction "translate"
10. See ")" -> pop the stack
ok i see where you're going with this and it makes sense... but another problem i come accross is what if its (translate (translate (rect 10 10 10 10) 50 50) 50 50)? with any number of translates? and what if i dont know the input?
what if i dont know the input?

What do you mean by this?

what if its (translate (translate (rect 10 10 10 10) 50 50) 50 50)? with any number of translates?

Technically, that doesn't change the solution.

Now, I'm assuming (translate (rect 10 10 10 10) 50 50) means, in English, "Make a rect at (10,10) with a width of 10 and a height of 10. Then, translate the rect by 50 in x and by 50 in y". With that assumption the parsing would go like:

1. (translate (translate (rect - > push "translate", push "translate", push "rect".
2. 10 10 10 10 -> compute rect with this input.
3. ) -> pop the stack. Now, with my assumption place the computed rect onto the parameter set for the second "translate", which is now on top of the stack.
4. 50 50 -> compute translate with the computed rect and this input.
5. ) -> pop the stack. Now, with my assumption place the translated rect onto the parameter set for the first "translate", which is now on top of the stack.
6. 50 50 -> compute translate with the translated rect and this input.
7. ) -> pop the stack.
Last edited on
Topic archived. No new replies allowed.