antlrjavaccantlrworks

Is there any tool to visualize parsed tree for jtb & javacc like antlrworks


Antlrworks provides a visualized parsed tree for the input source code file. I was wondering if there is any equavalent tool for javacc & jtb thanks


Solution

  • Walk the tree. Spit out nodes and arcs in graphviz ("dot") format. Invoke graphviz on the result.

    This won't be very useful for more than a hundred nodes, because it isn't very dense.

    Remarkably, a more scalable way to is print a nested S-expression, in the following format:

    <depth_from_root*spaces> ( <nodetype>  <newline>
            <childnode1_as_S-expression>
            <childnode2_as_S-expression>
            ...
            <childnodeN_as_S-expression>
                   )<nodetype>
    

    This in effect prints the tree sideways, e.g., with the root at the left and children to the right. You can print out very big trees this way, and still be able to read them (if you can scroll up and down through the text).

    As an example: for a*(b+c)-d:

       (-
            (* 
               (+ 
                  (variable b)
                  (variable c)
                )+
             )*
             (constant 1)
        )-
    

    This is also easily done with a tree walk. You can easily make the printed version more dense, or add more information.

    See examples of both of these, here.