[antlr-interest] tree transformation in Parser

Gregor Pardella pardella at informatik.uni-bonn.de
Mon Sep 12 01:26:53 PDT 2005


hello ter,

On Sat, Sep 10, 2005 at 10:45:47AM -0700, Terence Parr wrote:
> Hi Gregor,
> 
> What does your construction rule look like?  It should be:
> 
> assignment: ID EQUALS^ expr ;
> 
> or some such thing.

My expression rules are standard rules for expression, mean:
expression:
  assignment_expr (ASSIGN^ assignment_expr)? ;

assignment_expr:
  next_expr (NEXT_OP^ next_expr)?

etc....

in the last expression definition ID is declared.

But I solved it the hard way:
I count the ASSIGNs and if I match 2 or more I flat the tree by hand 
after the expression rule and return my flattened tree.

I think it is not the best solution (so if anybody has some ideas :)) 
but it works.

CODE:
else if (assignCount > 1) {
CommonAST child = null, sibling = e_AST;
Stack assignStack = new Stack();
assignStack.Push(e_AST);

CommonAST prep = (CommonAST) astFactory.dupTree(e_AST);
prep.removeChildren();

CommonAST constant = (CommonAST) astFactory.create(new Token(INT));
constant.setText("1");

CommonAST plusnode = (CommonAST) astFactory.create(new Token(PLUS));
plusnode.setText("+");
	
CommonAST minusnode = (CommonAST) astFactory.create(new Token(MINUS));
minusnode.setText("-");

CommonAST changeNode;
CommonAST workingNode;

while (((child = (CommonAST) sibling.getFirstChild()) != null)
&& ((sibling = (CommonAST) child.getNextSibling()) != null))
{
	if (sibling.getText().Equals("++"))
	{
	workingNode = (CommonAST) astFactory.dupTree(plusnode);
	changeNode = (CommonAST) astFactory.dupTree(sibling.getFirstChild());
	sibling = (CommonAST) astFactory.dupTree(prep);
	sibling.setFirstChild(astFactory.dupTree(changeNode));
	workingNode.setFirstChild(astFactory.dupTree(changeNode));
	workingNode.addChild(constant);
	sibling.getFirstChild().setNextSibling(workingNode);
	}
	if (sibling.getText().Equals("--"))
	{
	workingNode = (CommonAST) astFactory.dupTree(minusnode);
	changeNode = (CommonAST) astFactory.dupTree(sibling.getFirstChild());
	sibling = (CommonAST) astFactory.dupTree(prep);
	sibling.setFirstChild(astFactory.dup(changeNode));
	workingNode.setFirstChild(astFactory.dup(changeNode));
	workingNode.addChild(constant);
	sibling.getFirstChild().setNextSibling(workingNode);
	}
	if (sibling.getText().Equals("="))
		assignStack.Push(sibling);
}
CommonAST multiassign = (CommonAST) astFactory.create(new Token(MULTIASSIGN));
multiassign.setText("===");
astFactory.makeASTRoot(currentAST, (AST) multiassign);

Stack fertig = new Stack();
CommonAST _null = (CommonAST) assignStack.Pop();
CommonAST node;

fertig.Push(astFactory.dupTree(_null));
_null = null;

while (assignStack.Count > 0)
{
node = (CommonAST) astFactory.dupTree(((CommonAST) fertig.Peek()).getFirstChild());
_null = (CommonAST) assignStack.Pop();
_null.getFirstChild().setNextSibling(node);
fertig.Push(astFactory.dupTree(_null));
_null = null;
}
		
multiassign.setFirstChild((CommonAST)fertig.Pop());
while (fertig.Count > 0)
	multiassign.addChild((CommonAST) fertig.Pop());

returnAST = currentAST.root;
}


Gregor

-- 


More information about the antlr-interest mailing list