Search in sources :

Example 1 with GrammarParserInterpreter

use of org.antlr.v4.tool.GrammarParserInterpreter in project antlr4 by antlr.

the class TestAmbigParseTrees method testAmbiguousTrees.

public void testAmbiguousTrees(LexerGrammar lg, Grammar g, String startRule, String input, int decision, String expectedAmbigAlts, String overallTree, String[] expectedParseTrees) {
    InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(g.getRuleNames());
    LexerInterpreter lexEngine = lg.createLexerInterpreter(new ANTLRInputStream(input));
    CommonTokenStream tokens = new CommonTokenStream(lexEngine);
    final GrammarParserInterpreter parser = g.createGrammarParserInterpreter(tokens);
    parser.setProfile(true);
    parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
    // PARSE
    int ruleIndex = g.rules.get(startRule).index;
    ParserRuleContext parseTree = parser.parse(ruleIndex);
    assertEquals(overallTree, Trees.toStringTree(parseTree, nodeTextProvider));
    System.out.println();
    DecisionInfo[] decisionInfo = parser.getParseInfo().getDecisionInfo();
    List<AmbiguityInfo> ambiguities = decisionInfo[decision].ambiguities;
    assertEquals(1, ambiguities.size());
    AmbiguityInfo ambiguityInfo = ambiguities.get(0);
    List<ParserRuleContext> ambiguousParseTrees = GrammarParserInterpreter.getAllPossibleParseTrees(g, parser, tokens, decision, ambiguityInfo.ambigAlts, ambiguityInfo.startIndex, ambiguityInfo.stopIndex, ruleIndex);
    assertEquals(expectedAmbigAlts, ambiguityInfo.ambigAlts.toString());
    assertEquals(ambiguityInfo.ambigAlts.cardinality(), ambiguousParseTrees.size());
    for (int i = 0; i < ambiguousParseTrees.size(); i++) {
        ParserRuleContext t = ambiguousParseTrees.get(i);
        assertEquals(expectedParseTrees[i], Trees.toStringTree(t, nodeTextProvider));
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) GrammarParserInterpreter(org.antlr.v4.tool.GrammarParserInterpreter) DecisionInfo(org.antlr.v4.runtime.atn.DecisionInfo) AmbiguityInfo(org.antlr.v4.runtime.atn.AmbiguityInfo) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 2 with GrammarParserInterpreter

use of org.antlr.v4.tool.GrammarParserInterpreter in project antlr4 by antlr.

the class TestLookaheadTrees method testLookaheadTrees.

public void testLookaheadTrees(LexerGrammar lg, Grammar g, String input, String startRuleName, int decision, String[] expectedTrees) {
    int startRuleIndex = g.getRule(startRuleName).index;
    InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(g.getRuleNames());
    LexerInterpreter lexEngine = lg.createLexerInterpreter(new ANTLRInputStream(input));
    CommonTokenStream tokens = new CommonTokenStream(lexEngine);
    GrammarParserInterpreter parser = g.createGrammarParserInterpreter(tokens);
    parser.setProfile(true);
    ParseTree t = parser.parse(startRuleIndex);
    DecisionInfo decisionInfo = parser.getParseInfo().getDecisionInfo()[decision];
    LookaheadEventInfo lookaheadEventInfo = decisionInfo.SLL_MaxLookEvent;
    List<ParserRuleContext> lookaheadParseTrees = GrammarParserInterpreter.getLookaheadParseTrees(g, parser, tokens, startRuleIndex, lookaheadEventInfo.decision, lookaheadEventInfo.startIndex, lookaheadEventInfo.stopIndex);
    assertEquals(expectedTrees.length, lookaheadParseTrees.size());
    for (int i = 0; i < lookaheadParseTrees.size(); i++) {
        ParserRuleContext lt = lookaheadParseTrees.get(i);
        assertEquals(expectedTrees[i], Trees.toStringTree(lt, nodeTextProvider));
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) GrammarParserInterpreter(org.antlr.v4.tool.GrammarParserInterpreter) DecisionInfo(org.antlr.v4.runtime.atn.DecisionInfo) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) LookaheadEventInfo(org.antlr.v4.runtime.atn.LookaheadEventInfo)

Example 3 with GrammarParserInterpreter

use of org.antlr.v4.tool.GrammarParserInterpreter in project antlr4 by antlr.

the class TestUnicodeGrammar method binaryGrammar.

@Test
public void binaryGrammar() throws Exception {
    String grammarText = "grammar Binary;\n" + "r : HEADER PACKET+ FOOTER;\n" + "HEADER : '\\u0002\\u0000\\u0001\\u0007';\n" + "PACKET : '\\u00D0' ('\\u00D1' | '\\u00D2' | '\\u00D3') +;\n" + "FOOTER : '\\u00FF';\n";
    byte[] toParse = new byte[] { (byte) 0x02, (byte) 0x00, (byte) 0x01, (byte) 0x07, (byte) 0xD0, (byte) 0xD2, (byte) 0xD2, (byte) 0xD3, (byte) 0xD3, (byte) 0xD3, (byte) 0xD0, (byte) 0xD3, (byte) 0xD3, (byte) 0xD1, (byte) 0xFF };
    CharStream charStream;
    try (ByteArrayInputStream is = new ByteArrayInputStream(toParse);
        // U+0000 to U+00FF.
        InputStreamReader isr = new InputStreamReader(is, StandardCharsets.ISO_8859_1)) {
        charStream = new ANTLRInputStream(isr);
    }
    Grammar grammar = new Grammar(grammarText);
    LexerInterpreter lexEngine = grammar.createLexerInterpreter(charStream);
    CommonTokenStream tokens = new CommonTokenStream(lexEngine);
    GrammarParserInterpreter parser = grammar.createGrammarParserInterpreter(tokens);
    ParseTree parseTree = parser.parse(grammar.rules.get("r").index);
    InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(grammar.getRuleNames());
    String result = Trees.toStringTree(parseTree, nodeTextProvider);
    assertEquals("(r:1  ÐÒÒÓÓÓ ÐÓÓÑ ÿ)", result);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) GrammarParserInterpreter(org.antlr.v4.tool.GrammarParserInterpreter) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) Grammar(org.antlr.v4.tool.Grammar) CharStream(org.antlr.v4.runtime.CharStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Test(org.junit.Test)

Example 4 with GrammarParserInterpreter

use of org.antlr.v4.tool.GrammarParserInterpreter in project antlr4 by antlr.

the class TestGrammarParserInterpreter method testInterp.

InterpreterRuleContext testInterp(LexerGrammar lg, Grammar g, String startRule, String input, String expectedParseTree) {
    LexerInterpreter lexEngine = lg.createLexerInterpreter(new ANTLRInputStream(input));
    CommonTokenStream tokens = new CommonTokenStream(lexEngine);
    GrammarParserInterpreter parser = g.createGrammarParserInterpreter(tokens);
    ParseTree t = parser.parse(g.rules.get(startRule).index);
    InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(g.getRuleNames());
    String treeStr = Trees.toStringTree(t, nodeTextProvider);
    //		System.out.println("parse tree: "+treeStr);
    assertEquals(expectedParseTree, treeStr);
    return (InterpreterRuleContext) t;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) GrammarParserInterpreter(org.antlr.v4.tool.GrammarParserInterpreter) InterpreterRuleContext(org.antlr.v4.runtime.InterpreterRuleContext) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 5 with GrammarParserInterpreter

use of org.antlr.v4.tool.GrammarParserInterpreter in project antlr4 by antlr.

the class Grammar method createGrammarParserInterpreter.

/** @since 4.5.1 */
public GrammarParserInterpreter createGrammarParserInterpreter(TokenStream tokenStream) {
    if (this.isLexer()) {
        throw new IllegalStateException("A parser interpreter can only be created for a parser or combined grammar.");
    }
    char[] serializedAtn = ATNSerializer.getSerializedAsChars(atn);
    ATN deserialized = new ATNDeserializer().deserialize(serializedAtn);
    return new GrammarParserInterpreter(this, deserialized, tokenStream);
}
Also used : ATNDeserializer(org.antlr.v4.runtime.atn.ATNDeserializer) ATN(org.antlr.v4.runtime.atn.ATN)

Aggregations

CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)5 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)5 GrammarParserInterpreter (org.antlr.v4.tool.GrammarParserInterpreter)5 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)4 ParseTree (org.antlr.v4.runtime.tree.ParseTree)4 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)2 DecisionInfo (org.antlr.v4.runtime.atn.DecisionInfo)2 Grammar (org.antlr.v4.tool.Grammar)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStreamReader (java.io.InputStreamReader)1 CharStream (org.antlr.v4.runtime.CharStream)1 InterpreterRuleContext (org.antlr.v4.runtime.InterpreterRuleContext)1 ATN (org.antlr.v4.runtime.atn.ATN)1 ATNDeserializer (org.antlr.v4.runtime.atn.ATNDeserializer)1 AmbiguityInfo (org.antlr.v4.runtime.atn.AmbiguityInfo)1 LookaheadEventInfo (org.antlr.v4.runtime.atn.LookaheadEventInfo)1 Test (org.junit.Test)1