antlrantlr4

ANTLR4 GUI parse tree visualisator indefinitely runs without throwing an exception nor producing results


I tried to run the ANTLR4 visualization interpreter from this class with the example grammar description and program code from the antlr4-tools.

However with the latest antlr4 release (4.13.1) it runs indefinitely without producing results or throwing an exception

grammar Expr;
prog:   expr EOF ;
expr:   expr ('*'|'/') expr
    |   expr ('+'|'-') expr
    |   INT
    |   '(' expr ')'
    ;
NEWLINE : [\r\n]+ -> skip;
INT     : [0-9]+ ;
10+20*30
java -cp antlr4-4.13.1-complete.jar org.antlr.v4.gui.Interpreter Expr.g4 prog

The command runs without interruption and does not produce anything.

It was ran on a Pop!_OS 22.04 but also tried on Ubuntu 24.04.


Solution

  • I'm not sure if you can run the Interpreter like that. If you have the Python package antlr4-tools installed, you just need to run this:

    antlr4-parse Expr.g4 prog prog -gui
    

    Note the double prog: the first indicates the parser rule prog and the second indicates the file name containing the input to parse. You'll see the following window appear:

    enter image description here

    Printing a Lisp-like tree on your console can be done like this:

    antlr4-parse Expr.g4 prog prog -tree
    

    which prints:

    (prog:1 (expr:2 (expr:3 10) + (expr:1 (expr:3 20) * (expr:3 30))) <EOF>)