Search code examples
parsingantlr4

Antlr4 program is calling constructor of test code twice, what am I doing wrong?


I wrote a test for a grammar and in the constructor is a print which is being executed twice. How do I fix this?

First, here is the grammar:

grammar simple_grail;

prog: tag_block ;

tag_block: tag*;

/* root definition of any tag in Grail */
tag:
    p | div | canvas | rect | button | txt | textlit;

p: '<' 'p' CLASS_NAME? '>' tag_block '<' '/' 'p' '>' ;
div: '<' 'div' CLASS_NAME? '>' tag_block '<' '/' 'div' '>' ;
canvas: '<' 'canvas' CLASS_NAME? '>' tag_block '<' '/' 'canvas' '>' ;
rect:
     '<' 'rect' CLASS_NAME?
     'x' ASSIGN_COORD 'y' ASSIGN_COORD 'w' ASSIGN_COORD 'h' ASSIGN_COORD
     '/>' ;
button:
    '<' 'button' CLASS_NAME? (NAME)? 'text' ASSIGN_TEXT '/>' ;
txt:
    '<' 'text' 'x' ASSIGN_COORD 'y' ASSIGN_COORD '/>' ;

textlit:
    TEXT
    ;

CLASS_NAME: [a-zA-Z_] [a-zA-Z_0-9]* ;

ASSIGN_COORD: '=' [0-9]+ ;

name: 'name' '=' NAME ;

NAME: [A-Za-z_][A-Za-z_0-9]* ;

ASSIGN_TEXT: '=' ('"' [^"]* '"' | '\'' [^']* '\'');

TEXT: ~[<]+ ;

The relevant part of SimpleGrailParser is:

public class simple_grailTagsImpl {
    @Override public void enterProg(simple_grailParser.ProgContext ctx) {
        numInstructions = 0;
        numInts = 0;
        numFloats = 0;
        numStrings = 0;
        System.out.println("Starting program size=0");
    }
}

The main program loading it is here. Somehow, it is entering the main rule of the grammar twice

Starting program size=0
Starting program size=0

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
public class SimpleGrailParser {
    public static void main(String[] args) {
        String text = "<p>text is here</p>";
        simple_grailLexer lexer = new simple_grailLexer(CharStreams.fromString(text));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        simple_grailParser parser = new simple_grailParser(tokens);
        simple_grailTagsImpl listener= new simple_grailTagsImpl();
        parser.addParseListener(listener);
        ParseTree tree = parser.prog();
        ParseTreeWalker.DEFAULT.walk(listener, tree);
    }
}

Solution

  • Remove parser.addParseListener(listener): that is causing the duplication. ParseTreeWalker.DEFAULT.walk(listener, tree) is enough (or remove ParseTreeWalker.DEFAULT...).

    So, either this:

    String text = "<p>text is here</p>";
    simple_grailLexer lexer = new simple_grailLexer(CharStreams.fromString(text));
    simple_grailParser parser = new simple_grailParser(new CommonTokenStream(lexer));
    simple_grailTagsImpl listener = new simple_grailTagsImpl();
    ParseTreeWalker.DEFAULT.walk(listener, parser.prog());
    

    or this:

    String text = "<p>text is here</p>";
    simple_grailLexer lexer = new simple_grailLexer(CharStreams.fromString(text));
    simple_grailParser parser = new simple_grailParser(new CommonTokenStream(lexer));
    simple_grailTagsImpl listener = new simple_grailTagsImpl();
    parser.addParseListener(listener);
    parser.prog(); // This is needed to perform the tree walk!