[antlr-interest] Treeview

Martin Probst mail at martin-probst.com
Fri Oct 29 03:14:13 PDT 2004


Hi,
I've ran into this problem too and I wrote a very small piece of code
which uses the good ol' UNIX tool "graphviz dot". There should be RPMs for
it for most linux distributions and there is an installer for windows too.
The code to generate source files for dot is this one:

std::string escapeDotChars( std::string s )
{
	// Replace tabs with 4 spaces
	int i = 0;
	char c = '\t';
	while ( true ) {
		i = s.find(c, i);
		if ( i == std::string::npos )
			break;
		//cout << "found " << cs[k] << " at " << i << endl;
		s = s.replace(i,1,"    ");
		i++;
		i++;
	}
	// Replace the special chars used by dot
	char * cs = "\" ><{}|";
	for (int k = 0; static_cast<unsigned int>(k) < strlen(cs); k++) {
		int i = 0;
		while ( true ) {
			i = s.find(cs[k], i); // +2: 1 for the char, 1 for the inserted backslash
			if ( i == std::string::npos )
				break;
			//cout << "found " << cs[k] << " at " << i << endl;
			s = s.insert(i,"\\");
			i++;
			i++;
		}
	}
	// Remove newlines, replace with \\l for left oriented line
	i = 0;
	c = '\n';
	while ( true ) {
		i = s.find(c, i);
		if ( i == std::string::npos )
			break;
		//cout << "found " << cs[k] << " at " << i << endl;
		s = s.replace(i,1,"\\l");
		i++;
		i++;
	}
	return s;
}

void dotTreeNodeDump( ostream & os, XQueryParser& parser, hxdb::RefASTlc
tree, int & count, int parentid )
{
	count++;
	os << "\t\tN" << count << " [shape=record,label=\"{"
		<< escapeDotChars(parser.getTokenName(tree->getType()))
		<< " (" << tree->getLine() << ":" << tree->getColumn() << ")"
		<< "|" << escapeDotChars(tree->getText())
		<< "}\"]" << std::endl;
	if (parentid != 0)
		os << "\t\tN" << parentid << " -> N" << count << std::endl;
	if (tree->getFirstChild()) {
		// Dump the tree for the child with the current count as parentid
		dotTreeNodeDump( os, parser, hxdb::RefASTlc(tree->getFirstChild()),
count, count );
	}
	if (tree->getNextSibling()) {
		// dump the tree for the sibling with the same parentid
		dotTreeNodeDump( os, parser, hxdb::RefASTlc(tree->getNextSibling()),
count, parentid );
	}
}

void dotTreeDump( ostream & os, XQueryParser& parser, hxdb::RefASTlc tree )
{
	int count = 0;
	dotTreeNodeDump( os, parser, tree, count, count );
}

In the endeffect I call my parser executable (which I generate for testing
purposes anyways) like this: "myparser --dot myquery.txt | dot -Tpng -o
myquery.png"
This gives me a beatiful dot graph as PNG (and works for ps, SVG etc.
too). The could should be easy to adjust for java, at least I think so.

mfg
Martin



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 





More information about the antlr-interest mailing list