[antlr-interest] Re: matching an AST segment with Java 5

John Green greenj at ix.netcom.com
Wed Jul 26 09:53:32 PDT 2006


John Green wrote:
>     // Looking for: #(PLUS #(PLUS NUMBER NUMBER) NUMBER)
>     // and if found, print the text of the first NUMBER node.
>     Object [] match = match(currentNode,
>         new Object[]{PLUS, new Object[]{PLUS, NUMBER, NUMBER}, NUMBER}
>         );


Filling in the code for match() was easy. Here's a quick test mTest(), which calls a sample usage m(), which calls match().


private static final int PLUS = 20;
private static final int NUMBER = 30;
void mTest() {
	AST p1 = new CommonAST(); p1.setType(PLUS);
	AST p2 = new CommonAST(); p2.setType(PLUS);
	AST n1 = new CommonAST(); n1.setType(NUMBER); n1.setText("1");
	AST n2 = new CommonAST(); n2.setType(NUMBER); n2.setText("2");
	AST n3 = new CommonAST(); n3.setType(NUMBER); n3.setText("3");
	p1.setFirstChild(p2);
	p2.setFirstChild(n1);
	n1.setNextSibling(n2);
	p2.setNextSibling(n3);
	m(p1);
}

void m(AST currentNode) {
	// Looking for: #(PLUS #(PLUS NUMBER NUMBER) NUMBER)
	// and if found, print the text of the first NUMBER node.
	Object [] match = match(
			currentNode
			, new Object[]{PLUS, new Object[]{PLUS, NUMBER, NUMBER}, NUMBER}
			);
	if (match!=null) {
		System.out.println(((AST)((Object[])match[1])[1]).getText());
	} else {
		System.out.println("No match");
	}
}

Object[] match(AST ast, Object[] objarray) {
	if (ast==null) return null;
	Object[] ret = new Object[objarray.length];
	ret[0] = ast;
	AST currAST = ast.getFirstChild();
	for (int count = 1; count < objarray.length; count++) {
		if (currAST==null) return null;
		if (objarray[count] instanceof Object[]) {
			Object[] nonterminal = match(currAST, (Object[])objarray[count]);
			if (nonterminal==null) return null;
			ret[count] = nonterminal;
		} else {
			ret[count] = currAST;
		}
		currAST = currAST.getNextSibling();
	}
	return ret;
}





John Green wrote:
> With Java 5 and autoboxing, I could use nested Object arrays as a quick 
> and dirty description for a segment of an AST. Has anybody done this 
> sort of thing before? Specifically, I wonder if match(AST, Object[]) as 
> per my "usage example" below has been written and exists in any of the 
> Antlr or other libraries.
> 
> It'll be easy to write, I'd just rather use existing libraries.  :)
> <snip>
> 
> Cheers,
> John
> www.joanju.com
> 


More information about the antlr-interest mailing list