[antlr-interest] Tree transformation

mzukowski at yci.com mzukowski at yci.com
Wed Oct 8 15:11:23 PDT 2003

Your description is a bit unclear, but to get this:

               ID(a) - ID(b) - ID(c)

Do this:

list_expr! : #(l:LIST s:stmt_list) {##=#(#[LPAREN,")"], #[NAME,"func"], s);}
stmt_list : #(STATEMENT_LIST (stmt)*)


-----Original Message-----
From: Arnar Birgisson [mailto:arnarb at oddi.is] 
Sent: Wednesday, October 08, 2003 12:03 PM
To: antlr-interest at yahoogroups.com
Subject: [antlr-interest] Tree transformation


I have a programming construct "[a,b,c]" which defines a list (in the
lisp sense) of the results of expressions a, b and c. The parser returns
this tree: #([LIST,"["], #(STATEMENT_LIST, #a, #b, #c)). I.e.

 ID - ID - ID

My languages sematnics define "[a,b,c]" to be eqivalent
"func(;a,func(;b,c))", and moreover, the user program might define
"func" to be whatever function the user wants it to be.

(As a side note: the function call syntax in the language is
func(x1,..,xN;y1,..,yM) where x1,..,xN are copy-in copy-out parameters,
and y1,...,yM are pass-by-value. Also, there is no distinction between
statements and expressions.)

I have one tree parser that takes the parser output and simplifies the
tree, before that is passed to another tree-parser, the code generator.

The simplifying treeparser (the transformer) does things like convert
"expr1 + expr1" to "+(;expr1,expr2)", and I furthermore want it to
convert the tree for "[a,b,c]" to the tree "func(;a,func(;b,c))" would
have generated.

In a nutshell, I need to convert trees of the form depicted above, to

                                 ID(a) - ID(b) - ID(c)

How would you do in a tree-parser rule? The rules I have to match the
input tree are:

list_expr : #(LIST stmt_list)
stmt_list : #(STATEMENT_LIST (stmt)*)


ps. for the sake of completeness, the actual language doesn't use "func"
as the function name, but rather ":". Operatrs in the language are
simply functions as the equivalence "a+b"="+(;a,b)" implied.


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


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

More information about the antlr-interest mailing list