2

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 cases 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:

enter image description here

(click the image to enlarge)

Is there is bug in the ANTLR grammar, or am I doing something wrong?

Martin Schröder
  • 4,176
  • 7
  • 47
  • 81
user2454691
  • 435
  • 4
  • 7
  • Java grammer https://docs.google.com/document/d/1f-SN9mPxEcu5u_pwRhvtAfgMzENMHP5Z0o1I-Vyei6I/edit?usp=sharing – user2454691 Jun 12 '13 at 08:55
  • May be i am not geting you .i have used the same java grammar as on wiki page of antlr which is about 1160 line. then applied same procedure as in this question http://stackoverflow.com/questions/1931307/antlr-is-there-a-simple-example but with Java.g(Grammar) – user2454691 Jun 12 '13 at 09:48
  • grammer im using http://ideone.com/4NzSTm – user2454691 Jun 12 '13 at 10:12
  • I have used this [grammar](http://www.antlr3.org/grammar/1207932239307/Java1_5Grammars/Java.g).and changes i have done are [here](http://www.diffchecker.com/p3j31qmz) – user2454691 Jun 21 '13 at 11:15

1 Answers1

1

Edit: this answer was not correct. I misread the rewrite rule and updated my answer to address it.

Here is the fragment of that grammar that this answer previously referred to.

switchBlockLabels
    :   switchCaseLabels switchDefaultLabel? switchCaseLabels
        ->  ^(SWITCH_BLOCK_LABEL_LIST switchCaseLabels switchDefaultLabel? switchCaseLabels)
    ;

In this rewrite rule, since the references to switchCaseLabels are not written with a + or *, each will refer to only one element from the rule. Since switchCaseLabels is referenced twice in this rule, the rewrite rule will need to contain one of the following to include all results in the AST:

  • switchCaseLabels switchCaseLabels (this is the form it uses now)
  • switchCaseLabels*
  • switchCaseLabels+

I am not sure why your AST contains duplicate nodes.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • if u can help ,what changes has to be made in grammar in order to remove this duplication – user2454691 Jun 12 '13 at 13:32
  • @user2454691 My answer was previously incorrect. I fixed the mistake but now I do not know what the answer to your question is. – Sam Harwell Jun 12 '13 at 15:56
  • 2
    I have used this [grammar](http://www.antlr3.org/grammar/1207932239307/Java1_5Grammars/Java.g).and changes i have done are [here](http://www.diffchecker.com/p3j31qmz) . Is there some problem in grammar and how to rectify it. – user2454691 Jun 21 '13 at 11:17