I examine heterogeneous trees in ANTLR (using ANTLRWorks 1.4.2).
Here is the example of what I have already done in ANTLR.
grammar test;
options {
language = java;
output = AST;
}
tokens {
PROGRAM;
VAR;
}
@members {
class Program extends CommonTree {
public Program(int ttype) {
token = new CommonToken(ttype, "<start>");
}
}
}
start
: program var function
// Works fine:
//-> ^(PROGRAM program var function)
// Does not work (described below):
-> ^(PROGRAM<Program> program var function)
;
program
: 'program'! ID ';'!
;
var
: TYPE^ ID ';'!
;
function
: ID '('! ')'! ';'!
;
TYPE
: 'int'
| 'string'
;
ID
: ('a'..'z' | 'A'..'Z')+
;
WHITESPACE
: (' ' | '\t' '\n'| '\r' | '\f')+ {$channel = HIDDEN;}
;
Sample input:
program foobar;
int foo;
bar();
When I use rewrite rule ^(PROGRAM<Program> program var function), ANTLR stumbles over and I get AST like this:

Whereas when I use this rewrite rule ^(PROGRAM program var function) it works:

-
Could anyone explain where am I wrong, please? Frankly, I do not really get the idea of heterogeneous trees and how do I use
<…>syntax in ANTLR. -
What do
r0andr1mean (first picture)?
I have no idea what these
r0andr1mean: I don’t use ANTLRWorks for debugging, so can’t comment on that.Also,
language = java;causes ANTLR 3.2 to produce the error:ANTLR 3.2 expects it to be
language = Java;(capital “J”). But, by default the target is Java, so, mind as well remove thelanguage = ...entirely.Now, as to you problem: I cannot reproduce it. As I mentioned, I tested it with ANTLR 3.2, and removed the
language = java;part from your grammar, after which everything went as (I) expected.Enabling the rewrite rule
-> ^(PROGRAM<Program> program var function)produces the following ATS:and when enabling the rewrite rule
-> ^(PROGRAM program var function)instead, the following AST is created:I tested both rewrite rules this with the following class:
And the images are produced using graph.gafol.net (and the output of the
Mainclass, of course).