News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Tree Traversal Strategy

Started by cman, January 31, 2007, 05:47:47 PM

Previous topic - Next topic

cman

I'm designing a language that offers nested "repeat statements" like so:




   ( ( ( 1 2  ! ) 3 4 # ) )



which expands the above sequences like so:



       1 2 1 2 3 4 1 2 1 2 3 4 1 2 1 2 3 4 1 2 1 2 3 4 1 2 1 2 3 4 1 2 1 2 3 4



so that if a subsequence is in between a set of '(' and ')' it is doubled and if nested then the result of the sequence when doubled is doubled again, like:


           
              ( ( 1 2 # ) )


is double the sequence 1 2  and then double the result , 1 2 1 2 , again. By using a parse tree like so for the above:



             R
           /  \         
         (      )
             |
         /      \
      (           )
             |
          1   2



I was thinking using an inorder traversal of the tree to find the left most sequence and using the stack to double back at each assent back towards the root of the tree. Any other ideas? Thanks..... :bg

Larry Hammick

It looks like the multiple brackets are acting like an exponential. Maybe you should adapt some existing code that evaluates things like
(a-c)*(b-d)^2
(just an example). Such "EVAL" routines are either reentrent (they call themselves) or else use a stack-like data structure in memory, with a pointer. I hope this helps, but maybe you already know this stuff. :)

cman

Thanks for the reply! Strangely, I just started looking at this program again around the time you made a reply! I think I can achieve the desired result by building a linked list of the number ented of the input list and mark the list according the bracing as to how the list should be traversed ( mark the start of a repeated pattern and use this marker to jump back to the start to a bracketed section the required number of times. ) Thanks for your input!  :U :bg