Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Display ANTLR Tree GUI

How do you display the AST GUI if you have code like this, both of console or swing? My ANTLR is version 3.

CharStream stream = new ANTLRStringStream("program XLSample1 =\n" + 
            "constant one : Integer := 1;\n" + 
            "constant two : Integer := 2;\n" + 
            "var a, b,c : Integer := 42;\n" + 
            "begin\n" + 
            " x:= (12 + 6) - (7 * 41) - x mod y;\n" + 
            " y := 21;\n" + 
            "\n" + 
            "if x < 10 then\n" + 
            "  y :=2;\n" + 
            "  elseif x < 20 then\n" + 
            "  y := 20;\n" + 
            "  else\n" + 
            "  y := 30;\n" + 
            "end if; \n" + 
            "end XLSample1.");
    SampleLexer lexer  = new SampleLexer(stream);   
    TokenStream tokenStream = new CommonTokenStream(lexer);
    SampleParser parser = new SampleParser(tokenStream);
    parser.program();       
    System.out.println("OK");       
}
like image 771
Fadly Massere Avatar asked May 22 '14 13:05

Fadly Massere


3 Answers

Using ANTLR V4 (for V3 try to find out the similar API),to show a gui AST, you can use org.antlr.v4.runtime.tree.gui.TreeViewer.

You can get the Hello demo from ANTLR's site. Once you got it, run this simple demo:

import java.util.Arrays;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.gui.TreeViewer;

/**
 * A simple demo to show AST GUI with ANTLR
 * @see http://www.antlr.org/api/Java/org/antlr/v4/runtime/tree/gui/TreeViewer.html
 * 
 * @author wangdq
 * 2014-5-24
 *
 */
public class HelloTestDrive {
    public static void main(String[] args) {
        //prepare token stream
        CharStream stream = new ANTLRInputStream("hello antlr");
        HelloLexer lexer  = new HelloLexer(stream);   
        TokenStream tokenStream = new CommonTokenStream(lexer);
        HelloParser parser = new HelloParser(tokenStream);
        ParseTree tree = parser.r(); 

        //show AST in console
        System.out.println(tree.toStringTree(parser));

        //show AST in GUI
        JFrame frame = new JFrame("Antlr AST");
        JPanel panel = new JPanel();
        TreeViewer viewer = new TreeViewer(Arrays.asList(
                parser.getRuleNames()),tree);
        viewer.setScale(1.5); // Scale a little
        panel.add(viewer);
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

Then you will get the AST print in the console and show in JFrame. more details, please refer ANTLR API.

Make sure your grammar work fine, then you can modify this demo to meet your requirement.

Update for ANTLR 4: TreeViewer has moved to org.antlr.v4.gui.TreeViewer package from ANTLR 4 Tool.

When using maven, TreeViewer requires the following dependency:

<dependency>
    <groupId>org.antlr</groupId>
    <artifactId>antlr4</artifactId>
    <version>4.7.2</version>
</dependency>
like image 72
wangdq Avatar answered Oct 11 '22 11:10

wangdq


After a few attempts trying to customize wangdq code, I figured out that it's possible to call the open method of TreeViewer class to get a delightful (because it's already done :)) Parse Tree Inspector.

Applied to wangdq example:

public class HelloTestDrive {
    public static void main(String[] args) {
        //prepare token stream
        CharStream stream = new ANTLRInputStream("hello antlr");
        HelloLexer lexer  = new HelloLexer(stream);   
        TokenStream tokenStream = new CommonTokenStream(lexer);
        HelloParser parser = new HelloParser(tokenStream);
        ParseTree tree = parser.r(); 

        //show AST in console
        System.out.println(tree.toStringTree(parser));

        //show AST in GUI
        TreeViewer viewr = new TreeViewer(Arrays.asList(
                parser.getRuleNames()),tree);
        viewr.open();
    }
}
like image 40
cl3rk90 Avatar answered Oct 11 '22 10:10

cl3rk90


use import org.antlr.v4.runtime.tree.gui.TreeViewer in ANTLR 4...its works :)

like image 20
Fachrian Luthfi Ramadhan Avatar answered Oct 11 '22 12:10

Fachrian Luthfi Ramadhan