[antlr-interest] AST utility class for C# users

Ney, Richard richard.ney at aspect.com
Tue Nov 5 16:18:47 PST 2002


For those out there how are using C# and want an easy way to display your
AST trees in a standard TreeView class here is a little utility class. I
felt this was easier than using the ASTFrame classes that came with the C#
codegen library.

using System;
using System.Windows.Forms;
using antlr;
using antlr.collections;

namespace GenericNamespace
{
	/// <summary>
	/// Summary description for AstTreeCollection.
	/// </summary>
	public class AstTreeNode : TreeNode
	{
		public AstTreeNode(AST parentNode)
		{
			string text = (parentNode.getText() == "" ? "Blank"
: parentNode.getText());
			if (text != "")
			{
				this.Text = text;
			}

			// Get the first child of the node.
			AST childNode = parentNode.getFirstChild();

			// While we have elements to work on.
			while (childNode != null)
			{		
				// Walk deeper into the tree
				this.Nodes.Add(new AstTreeNode(childNode));

				// Go to the next sibling in the tree.
				childNode = childNode.getNextSibling();
			}
		}
	}
}


And a piece of sample code using it.


parser.expression();
antlr.CommonAST exprAST = (antlr.CommonAST)parser.getAST();

AstTreeNode treeNode = new AstTreeNode(exprAST);
expressionAST.Nodes.Clear();
expressionAST.Nodes.Add(treeNode);
treeNode.ExpandAll();


----------------------------------------------------------------------------
------------------------
Richard Ney	Aspect Communications
Principal Software Engineer
http://www.aspect.com <http://www.aspect.com>
Main:  408.325.2200
mailto:richard.ney at aspect.com <mailto:richard.ney at aspect.com>
SJ Office: 408.325.2464
    	Home Office: 916.797.9602
----------------------------------------------------------------------------
------------------------

The Three Laws of Infernal Dynamics:
1. An object in motion will always be headed in the wrong direction.
2. An object at rest will always be in the wrong place.
3. The energy required to change either of these states will always be more
than you wish to expend, but never so much as to make the task appear
prospectively impossible.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20021105/cd9749e5/attachment.html


More information about the antlr-interest mailing list