[antlr-interest] Tree transformation

Arnar Birgisson arnarb at oddi.is
Fri Nov 14 07:23:34 PST 2003


Hello..

I'm having some trouble I can't figure out, possibly because I'm doing
something stupid.

I have this rule in a tree parser for transforming loops:

loop_stmt
	: #(L_LOOP stmt_list)
	| #(L_WHILE expr stmt_list)
	|! #(L_FOR init:stmt_list test:expr incr:stmt_list
body:stmt_list)
		{
			/* this changes "for" loops to "while" loops */
			antlr::RefAST newbody;
			antlr::RefAST lastBodyStmt =
body->getFirstChild();
			if (antlr::nullAST == lastBodyStmt) {
				newbody = incr;
			} else {
				while (antlr::nullAST !=
lastBodyStmt->getNextSibling())
					lastBodyStmt =
lastBodyStmt->getNextSibling();
	
lastBodyStmt->setNextSibling(incr->getFirstChild());
				newbody = body;
			}
			antlr::RefAST l = #([L_WHILE,"while"], test,
newbody);
			antlr::RefAST lastInitStmt =
init->getFirstChild();
			if (antlr::nullAST == lastInitStmt) {
				## = l;
			} else {
				while (antlr::nullAST !=
lastInitStmt->getNextSibling())
					lastInitStmt =
lastInitStmt->getNextSibling();
				lastInitStmt->setNextSibling(l);
				## = init->getFirstChild();
			}
		}
	;

Now, stmt_list is a simple rule

stmt_list
	: #(STMT_LIST (stmt)*)
	;

and the stmt rule is a big rule, with one alternative being this
(note that in my language there is no difference between statements and
expressions):

	|! #(OPERATOR s1:expr s2:expr)
		{
			/* this changes "x <op> b" to the function call
"<op>(x,y)"
			#OPERATOR->setType(ID);
			## = #([OPEN_PAR,"("], ADGERD,
#([stmt_list,"params"], s1, s2));
		}

Now, this alternative successfully transforms operator statements to
function alls when they are top level statements in functions (accessed
throught stmt_list), but when they're in a for-loop body (named "body")
in the above rule, no transformation takes place, i.e. #(OPERATOR expr
expr) is left as is.

I've tried removing the ! in the for-loop rule but that doesn't help..
the transformation doesn't take place.

Any ideas?

Arnar


 

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




More information about the antlr-interest mailing list