I am using ANTLR 3.2 for making an AST for this java code:
Test.java
public class Test {
public static void main(String args[]) {
int x = 10;
switch(x){
case 1:{
break;
}
case 2:{
break;
}
default:
return;
}
}
}
with a Java 1.5 grammar from the ANTLR wiki.
But the generated AST has a duplicated switch
node.
I want to parse the input file and find the number of case
s and compound blocks inside the switch
block for the generated AST.
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.stringtemplate.*;
public class Main1 {
public static void main(String[] args) throws Exception {
JavaLexer lexer = new JavaLexer(new ANTLRFileStream("Test.java"));
JavaParser parser = new JavaParser(new CommonTokenStream(lexer));
CommonTree tree = (CommonTree)parser.javaSource().getTree();
DOTTreeGenerator gen = new DOTTreeGenerator();
StringTemplate st = gen.toDOT(tree);
System.out.println(st);
}
}
AST:
(click the image to enlarge)
Is there is bug in the ANTLR grammar, or am I doing something wrong?