[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [DotGNU]Walking The ILNode_List

From: Rhys Weatherley
Subject: Re: [DotGNU]Walking The ILNode_List
Date: Sat, 23 Mar 2002 10:12:42 +1000

nymia wrote:

> Is CSTypeGather() in cs_gather.c a good base code for walking the
> ILNode_List? Or, in other words, what is the best approach in walking the
> parse tree?

No, it isn't a very good model.  CSTypeGather only
handles a small subset of the nodes, and it has tons
of special cases.  Walking the tree in this way should
only be done if there are no alternatives.  It's pretty
much unmaintainable except by gurus.

> I'm currently working on building a tree view of the parse tree. Any ideas
> are welcome.

A better way is to define a virtual operation called
"ILNode_Dump" or somesuch.  Then add cases for
each major node category.  e.g.

%operation %virtual void ILNode_Dump
     (ILNode *node, FILE *stream, int indent)

    PrintIndent(stream, indent);
    fprintf(stream, "%s\n", yykindname(node));
    ILNode_Dump(node->expr1, stream, indent + 1);
    ILNode_Dump(node->expr2, stream, indent + 1);

where "PrintIndent" is a helper function to indent
the output by a particular amount.

Here I have overridden ILNode_BinaryExpression,
because that will automatically handle every binary
expression in the tree, avoiding the need to separately
handle add, subtract, multiply, divide, etc, etc, etc.

You could of course add extra parameters to specify
"dump as text", "dump as HTML", "dump as XML", etc.
Or even have separate operations: ILNode_DumpText,
ILNode_DumpHTML, ILNode_DumpXML, etc.

However, a better approach may be to always dump
as XML, and then write a separate tool to post-process
the XML to turn it into text or HTML.  I would strongly
suggest doing that.

Create a "" file in "codegen" and put
all of the dump cases there.  Edit "" to
include this file.

The benefit of this approach is that treecc does most
of the hard work for you: it will check that you have
a case for every single node type, and give an error
if you forget something.  If you were to use the manual
walking procedure in CSTypeGather, you wouldn't
get the checks.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]