Search in sources :

Example 31 with Grammar

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

the class TestCompositeGrammars method testImportFileNotSearchedForInOutputDir.

@Test
public void testImportFileNotSearchedForInOutputDir() throws Exception {
    String slave = "parser grammar S;\n" + "a : B {System.out.println(\"S.a\");} ;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    String outdir = tmpdir + "/out";
    BaseRuntimeTest.mkdir(outdir);
    writeFile(outdir, "S.g4", slave);
    String master = "grammar M;\n" + "import S;\n" + "s : a ;\n" + // defines B from inherited token space
    "B : 'b' ;" + "WS : (' '|'\\n') -> skip ;\n";
    writeFile(tmpdir, "M.g4", master);
    ErrorQueue equeue = BaseRuntimeTest.antlrOnString(tmpdir, "Java", "M.g4", false, "-o", outdir);
    assertEquals(ErrorType.CANNOT_FIND_IMPORTED_GRAMMAR, equeue.errors.get(0).getErrorType());
}
Also used : ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) BaseRuntimeTest(org.antlr.v4.test.runtime.BaseRuntimeTest) Test(org.junit.Test)

Example 32 with Grammar

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

the class TestCompositeGrammars method test3LevelImport.

// Make sure that M can import S that imports T.
@Test
public void test3LevelImport() throws Exception {
    ErrorQueue equeue = new ErrorQueue();
    String slave = "parser grammar T;\n" + "a : T ;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    writeFile(tmpdir, "T.g4", slave);
    String slave2 = "parser grammar S;\n" + "import T;\n" + "a : S ;\n";
    BaseRuntimeTest.mkdir(tmpdir);
    writeFile(tmpdir, "S.g4", slave2);
    String master = "grammar M;\n" + "import S;\n" + "a : M ;\n";
    writeFile(tmpdir, "M.g4", master);
    Grammar g = new Grammar(tmpdir + "/M.g4", master, equeue);
    // S and T aren't imported; overridden
    String expectedTokenIDToTypeMap = "{EOF=-1, M=1}";
    String expectedStringLiteralToTypeMap = "{}";
    String expectedTypeToTokenList = "[M]";
    assertEquals(expectedTokenIDToTypeMap, g.tokenNameToTypeMap.toString());
    assertEquals(expectedStringLiteralToTypeMap, g.stringLiteralToTypeMap.toString());
    assertEquals(expectedTypeToTokenList, realElements(g.typeToTokenList).toString());
    assertEquals("unexpected errors: " + equeue, 0, equeue.errors.size());
    boolean ok = rawGenerateAndBuildRecognizer("M.g4", master, "MParser", null);
    // should be ok
    boolean expecting = true;
    assertEquals(expecting, ok);
}
Also used : ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) Grammar(org.antlr.v4.tool.Grammar) BaseRuntimeTest(org.antlr.v4.test.runtime.BaseRuntimeTest) Test(org.junit.Test)

Example 33 with Grammar

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

the class TestAmbigParseTrees method testParseDecisionWithinAmbiguousStartRule.

@Test
public void testParseDecisionWithinAmbiguousStartRule() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "s : A x C" + "  | A B C" + "  ;" + "x : B ; \n", lg);
    testInterpAtSpecificAlt(lg, g, "s", 1, "abc", "(s:1 a (x:1 b) c)");
    testInterpAtSpecificAlt(lg, g, "s", 2, "abc", "(s:2 a b c)");
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 34 with Grammar

use of org.antlr.v4.tool.Grammar 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 35 with Grammar

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

the class TestAmbigParseTrees method testAmbigAltDipsIntoOuterContextToRoot.

@Test
public void testAmbigAltDipsIntoOuterContextToRoot() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "SELF : 'self' ;\n" + "ID : [a-z]+ ;\n" + "DOT : '.' ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "e : p (DOT ID)* ;\n" + "p : SELF" + "  | SELF DOT ID" + "  ;", lg);
    String startRule = "e";
    String input = "self.x";
    String expectedAmbigAlts = "{1, 2}";
    // decision in p
    int decision = 1;
    String expectedOverallTree = "(e:1 (p:1 self) . x)";
    String[] expectedParseTrees = { "(e:1 (p:1 self) . x)", "(p:2 self . x)" };
    testAmbiguousTrees(lg, g, startRule, input, decision, expectedAmbigAlts, expectedOverallTree, expectedParseTrees);
}
Also used : Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Aggregations

LexerGrammar (org.antlr.v4.tool.LexerGrammar)305 Test (org.junit.Test)304 Grammar (org.antlr.v4.tool.Grammar)183 ATN (org.antlr.v4.runtime.atn.ATN)65 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)62 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)59 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)53 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)47 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)43 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)31 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)29 Rule (org.antlr.v4.tool.Rule)25 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)21 STGroupString (org.stringtemplate.v4.STGroupString)21 ArrayList (java.util.ArrayList)18 ATNState (org.antlr.v4.runtime.atn.ATNState)18 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)17 ST (org.stringtemplate.v4.ST)17 ParseTree (org.antlr.v4.runtime.tree.ParseTree)15 BaseRuntimeTest (org.antlr.v4.test.runtime.BaseRuntimeTest)15