user2454691
user2454691

Reputation: 493

Using antlr the AST for java code with switch cases having duplicated nodes

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?

Upvotes: 2

Views: 1098

Answers (1)

Sam Harwell
Sam Harwell

Reputation: 99869

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.

Upvotes: 1

Related Questions