Example usage for org.antlr.v4.tool Grammar load

List of usage examples for org.antlr.v4.tool Grammar load

Introduction

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

Prototype

public static Grammar load(String fileName) 

Source Link

Document

convenience method for Tool.loadGrammar()

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.j ava2 s. c  o  m*/
@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);
}