Example usage for org.antlr.v4.tool DOTGenerator DOTGenerator

List of usage examples for org.antlr.v4.tool DOTGenerator DOTGenerator

Introduction

In this page you can find the example usage for org.antlr.v4.tool DOTGenerator DOTGenerator.

Prototype

public DOTGenerator(Grammar grammar) 

Source Link

Document

This aspect is associated with a grammar

Usage

From source file:io.github.hjuergens.time.GenerateParseTreeAST.java

License:Apache License

/**
 * <a href="https://stackoverflow.com/questions/10614659/no-viable-alternative-at-input"></a>
 *//*from w  w w  .ja  v a  2 s.com*/
@Test(expectedExceptions = java.lang.NoSuchFieldError.class)
public void dotTree() {

    // the expression
    String src = "(ICOM LIKE '%bridge%' or ICOM LIKE '%Munich%')";

    // create a lexer & parser
    //DatesLexer lexer = new DatesLexer(new ANTLRStringStream(src));
    //DatesParser parser = new DatesParser(new CommonTokenStream(lexer));

    DatesLexer lexer = new DatesLexer(new ANTLRInputStream(src));
    DatesParser parser = new DatesParser(new CommonTokenStream(lexer));

    // invoke the entry point of the parser (the parse() method) and get the AST
    Tree tree = parser.date();
    String grammarFileName = lexer.getGrammarFileName();
    log.info("grammar file name=" + grammarFileName);

    // print the DOT representation of the AST 
    Grammar grammar = Grammar.load(grammarFileName);
    DOTGenerator gen = new DOTGenerator(grammar);

    ATNState startState = new ATNState() {
        @Override
        public int getStateType() {
            return ATNState.BASIC;
        }
    };
    String st = gen.getDOT(startState);
    log.info(st);
}