antlr3 - ANTLR, heterogeneous AST problem -


i examine heterogeneous trees in antlr (using antlrworks 1.4.2).

here example of have 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)          // 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 use rewrite rule ^(program<program> program var function), antlr stumbles on , ast this:

antlr

whereas when use rewrite rule ^(program program var function) works:

enter image description here

  1. could explain wrong, please? frankly, not idea of heterogeneous trees , how use <…> syntax in antlr.

  2. what r0 , r1 mean (first picture)?

i have no idea these r0 , r1 mean: don't use antlrworks debugging, can't comment on that.

also, language = java; causes antlr 3.2 produce error:

error(10): internal error: no such group file java.stg

error(20): cannot find code generation templates java.stg
error(10): internal error: no such group file java.stg

error(20): cannot find code generation templates java.stg

antlr 3.2 expects language = java; (capital "j"). but, default target java, so, mind remove language = ... entirely.

now, problem: cannot reproduce it. mentioned, tested antlr 3.2, , removed language = java; part grammar, after went (i) expected.

enabling rewrite rule -> ^(program<program> program var function) produces following ats:

enter image description here

and when enabling rewrite rule -> ^(program program var function) instead, following ast created:

enter image description here

i tested both rewrite rules following class:

import org.antlr.runtime.*; import org.antlr.runtime.tree.*; import org.antlr.stringtemplate.*;  public class main {     public static void main(string[] args) throws exception {         antlrstringstream in = new antlrstringstream("program foobar; int foo; bar();");         testlexer lexer = new testlexer(in);         commontokenstream tokens = new commontokenstream(lexer);         testparser parser = new testparser(tokens);         testparser.start_return returnvalue = parser.start();         commontree tree = (commontree)returnvalue.gettree();         dottreegenerator gen = new dottreegenerator();         stringtemplate st = gen.todot(tree);         system.out.println(st);     } } 

and images produced using graph.gafol.net (and output of main class, of course).


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -