Prune/collapse AST Nodes

18 views
Skip to first unread message

Florian Leitner

unread,
Feb 2, 2011, 11:17:37 AM2/2/11
to le...@googlegroups.com
Hi again,

I have a problem I'm not quite able to solve on myself, pruning Nodes that only contain other, single Nodes in the AST, so that the innermost single Node gets "promoted" to the top. Let's take the Nodes example on the webpage (bottom):

http://www.acooke.org/lepl/nodes.html

There we see the following AST from the expression "1 + 2 * (3 + 4 - 5)", modified with dropped spaces and parenthesis:

Expression
+- Factor
|   +- Term
|   |   `- number '1'
+- operator '+'
`- Factor
    +- Term
    |   `- number '2'
    +- operator '*'
    `- Term
        +- Expression
            +- Factor
            |   +- Term
            |   |   `- number '3'
            +- operator '+'
            +- Factor
            |   +- Term
            |   |   `- number '4'
            +- operator '-'
            `- Factor
                +- Term
                |   `- number '5'

Now, this is rather verbose... I'd like to get rid of all Nodes that contain only single child nodes, to arrive at the following tree:

Expression
+- Term
|   `- number '1'
+- operator '+'
`- Factor
    +- Term
    |   `- number '2'
    +- operator '*'
    `- Term
        +- Expression
            +- Term
            |   `- number '3'
            +- operator '+'
            +- Term
            |   `- number '4'
            +- operator '-'
            `- Term
                |   `- number '5'

This doesn't seem like something very sophisticated, but I don't see a way this is simply done with LEPL, or at least am not able to deduce it from the API and manual. Any hints? Maybe I need to walk the tree and prune it manually?I'm a bit lost ATM, so any help would be much appreciated! Thanks!

--Florian

Florian Leitner

unread,
Feb 2, 2011, 2:09:30 PM2/2/11
to le...@googlegroups.com
Just to follow up - forget my question, I realized the problem is trivial by using the postorder walk function of support.graph...

--Florian

andrew cooke

unread,
Feb 3, 2011, 4:23:32 AM2/3/11
to le...@googlegroups.com

OK, cool.

Sorry for the slow reply, but I am only able to read email a few times
a day - I have just returned to Chile and don't have an internet
connection (I requested it Monday, but they quoted two weeks for
installation). Sometimes I can access an open Wifi, but it is very
unreliable (yesterday was not visible at all).

Andrew

Reply all
Reply to author
Forward
0 new messages