[antlr-interest] Tree construction for function / method calls
Christian Pontesegger
christian.pontesegger at web.de
Wed Mar 17 01:57:01 PDT 2010
Hi all,
sorry for the porevious mail, webmail messed it up ...
I am desperately trying to create a tree for method calls but am struggling with recursion in the tree generation. My language allows for statements like this:
print().foo().test().me()
Where print() is a function returning an object. Then subsequently methods are called.
$x.this().is().it()
Where $x is a variable holding an object.
Previous two statements should be transformed to trees like this:
^(^(METHOD ^(METHOD ^(FUN print) foo) test) me)
^(^(METHOD ^(METHOD ^(VAR x) this) is) it)
So I started with a grammar, but got stuck here:
grammar Test;
options {
language = Java;
output = AST;
ASTLabelType=CommonTree;
}
tokens {
VAR;
FUN;
METHOD;
}
program
: statement* EOF!
;
statement
: token
(
'.' IDENT '(' ')' -> ^(METHOD token IDENT)
)*
;
token
: function
| variable
;
function
: IDENT '(' ')' -> ^(FUN IDENT)
;
variable
: '$' IDENT -> ^(VAR IDENT)
;
fragment LETTER : ('a'..'z' | 'A'..'Z');
fragment DIGIT : '0'..'9';
fragment LINEBREAK : '\r'? '\n';
IDENT : LETTER (LETTER | DIGIT | '_')*;
WHITESPACES : (' ' | '\t' | '\f' | LINEBREAK)+ { $channel = HIDDEN; };
Running this grammar on the example input results in following tree:
(METHOD (FUN print) foo) (METHOD (VAR x) this)
I see that I need to change the * from "statement" to ? somehow, but how can I call statement recursively?
Thanks
Christian
___________________________________________________________
GRATIS für alle WEB.DE-Nutzer: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://movieflat.web.de
More information about the antlr-interest
mailing list