Search in sources :

Example 81 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.

the class TestATNParserPrediction method testLL1Ambig.

@Test
public void testLL1Ambig() 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" + "a : A | A | A B ;");
    int decision = 0;
    checkPredictedAlt(lg, g, decision, "a", 1);
    checkPredictedAlt(lg, g, decision, "ab", 3);
    // After matching these inputs for decision, what is DFA after each prediction?
    String[] inputs = { "a", "ab", "ab" };
    String[] dfa = { "s0-'a'->s1\n" + "s1-EOF->:s2=>1\n", "s0-'a'->s1\n" + "s1-EOF->:s2=>1\n" + "s1-'b'->:s3=>3\n", "s0-'a'->s1\n" + "s1-EOF->:s2=>1\n" + "s1-'b'->:s3=>3\n" };
    checkDFAConstruction(lg, g, decision, inputs, dfa);
}
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 82 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.

the class TestATNParserPrediction method testLL2Ambig.

@Test
public void testLL2Ambig() 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" + "a : A B | A B | A B C ;");
    int decision = 0;
    checkPredictedAlt(lg, g, decision, "ab", 1);
    checkPredictedAlt(lg, g, decision, "abc", 3);
    // After matching these inputs for decision, what is DFA after each prediction?
    String[] inputs = { "ab", "abc", "ab" };
    String[] dfa = { "s0-'a'->s1\n" + "s1-'b'->s2\n" + "s2-EOF->:s3=>1\n", "s0-'a'->s1\n" + "s1-'b'->s2\n" + "s2-EOF->:s3=>1\n" + "s2-'c'->:s4=>3\n", "s0-'a'->s1\n" + "s1-'b'->s2\n" + "s2-EOF->:s3=>1\n" + "s2-'c'->:s4=>3\n" };
    checkDFAConstruction(lg, g, decision, inputs, dfa);
}
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 83 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.

the class TestATNParserPrediction method testRecursiveLeftPrefixWithAorABIssue.

@Test
public void testRecursiveLeftPrefixWithAorABIssue() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n" + "LP : '(' ;\n" + "RP : ')' ;\n" + "INT : '0'..'9'+ ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "tokens {A,B,C,LP,RP,INT}\n" + "a : e A | e A B ;\n" + "e : LP e RP\n" + "  | INT\n" + "  ;");
    int decision = 0;
    checkPredictedAlt(lg, g, decision, "34a", 1);
    // PEG would miss this one!
    checkPredictedAlt(lg, g, decision, "34ab", 2);
    checkPredictedAlt(lg, g, decision, "((34))a", 1);
    checkPredictedAlt(lg, g, decision, "((34))ab", 2);
    // After matching these inputs for decision, what is DFA after each prediction?
    String[] inputs = { "34a", "34ab", "((34))a", "((34))ab" };
    String[] dfa = { "s0-INT->s1\n" + "s1-'a'->s2\n" + "s2-EOF->:s3=>1\n", "s0-INT->s1\n" + "s1-'a'->s2\n" + "s2-EOF->:s3=>1\n" + "s2-'b'->:s4=>2\n", "s0-'('->s5\n" + "s0-INT->s1\n" + "s1-'a'->s2\n" + "s2-EOF->:s3=>1\n" + "s2-'b'->:s4=>2\n" + "s5-'('->s6\n" + "s6-INT->s7\n" + "s7-')'->s8\n" + "s8-')'->s1\n", "s0-'('->s5\n" + "s0-INT->s1\n" + "s1-'a'->s2\n" + "s2-EOF->:s3=>1\n" + "s2-'b'->:s4=>2\n" + "s5-'('->s6\n" + "s6-INT->s7\n" + "s7-')'->s8\n" + "s8-')'->s1\n" };
    checkDFAConstruction(lg, g, decision, inputs, dfa);
}
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 84 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.

the class TestATNParserPrediction method testAorB.

@Test
public void testAorB() 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" + "a : A{;} | B ;");
    int decision = 0;
    checkPredictedAlt(lg, g, decision, "a", 1);
    checkPredictedAlt(lg, g, decision, "b", 2);
    // After matching these inputs for decision, what is DFA after each prediction?
    String[] inputs = { "a", "b", "a" };
    String[] dfa = { "s0-'a'->:s1=>1\n", "s0-'a'->:s1=>1\n" + "s0-'b'->:s2=>2\n", // don't change after it works
    "s0-'a'->:s1=>1\n" + "s0-'b'->:s2=>2\n" };
    checkDFAConstruction(lg, g, decision, inputs, dfa);
}
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 85 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project titan.EclipsePlug-ins by eclipse.

the class TTCN3Analyzer method parse.

/**
 * Parse TTCN-3 file using ANTLR v4
 * @param aReader file to parse (cannot be null, closes aReader)
 * @param aFileLength file length
 * @param aEclipseFile Eclipse dependent resource file
 */
private void parse(final Reader aReader, final int aFileLength, final IFile aEclipseFile) {
    CharStream charStream = new UnbufferedCharStream(aReader);
    Ttcn3Lexer lexer = new Ttcn3Lexer(charStream);
    lexer.setCommentTodo(true);
    lexer.setTokenFactory(new CommonTokenFactory(true));
    lexer.initRootInterval(aFileLength);
    TitanListener lexerListener = new TitanListener();
    // remove ConsoleErrorListener
    lexer.removeErrorListeners();
    lexer.addErrorListener(lexerListener);
    // 1. Previously it was UnbufferedTokenStream(lexer), but it was changed to BufferedTokenStream, because UnbufferedTokenStream seems to be unusable. It is an ANTLR 4 bug.
    // Read this: https://groups.google.com/forum/#!topic/antlr-discussion/gsAu-6d3pKU
    // pr_PatternChunk[StringBuilder builder, boolean[] uni]:
    // $builder.append($v.text); <-- exception is thrown here: java.lang.UnsupportedOperationException: interval 85..85 not in token buffer window: 86..341
    // 2. Changed from BufferedTokenStream to CommonTokenStream, otherwise tokens with "-> channel(HIDDEN)" are not filtered out in lexer.
    final CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    Ttcn3Parser parser = new Ttcn3Parser(tokenStream);
    ParserUtilities.setBuildParseTree(parser);
    PreprocessedTokenStream preprocessor = null;
    if (aEclipseFile != null && GlobalParser.TTCNPP_EXTENSION.equals(aEclipseFile.getFileExtension())) {
        lexer.setTTCNPP();
        preprocessor = new PreprocessedTokenStream(lexer);
        preprocessor.setActualFile(aEclipseFile);
        if (aEclipseFile.getProject() != null) {
            preprocessor.setMacros(PreprocessorSymbolsOptionsData.getTTCN3PreprocessorDefines(aEclipseFile.getProject()));
        }
        parser = new Ttcn3Parser(preprocessor);
        ParserUtilities.setBuildParseTree(parser);
        preprocessor.setActualLexer(lexer);
        preprocessor.setParser(parser);
    }
    if (aEclipseFile != null) {
        lexer.setActualFile(aEclipseFile);
        parser.setActualFile(aEclipseFile);
        parser.setProject(aEclipseFile.getProject());
    }
    // remove ConsoleErrorListener
    parser.removeErrorListeners();
    TitanListener parserListener = new TitanListener();
    parser.addErrorListener(parserListener);
    // This is added because of the following ANTLR 4 bug:
    // Memory Leak in PredictionContextCache #499
    // https://github.com/antlr/antlr4/issues/499
    DFA[] decisionToDFA = parser.getInterpreter().decisionToDFA;
    parser.setInterpreter(new ParserATNSimulator(parser, parser.getATN(), decisionToDFA, new PredictionContextCache()));
    // try SLL mode
    try {
        parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
        final ParseTree root = parser.pr_TTCN3File();
        ParserUtilities.logParseTree(root, parser);
        warnings = parser.getWarnings();
        mErrorsStored = lexerListener.getErrorsStored();
        mErrorsStored.addAll(parserListener.getErrorsStored());
    } catch (RecognitionException e) {
    // quit
    }
    if (!warnings.isEmpty() || !mErrorsStored.isEmpty()) {
        // SLL mode might have failed, try LL mode
        try {
            CharStream charStream2 = new UnbufferedCharStream(aReader);
            lexer.setInputStream(charStream2);
            // lexer.reset();
            parser.reset();
            parserListener.reset();
            parser.getInterpreter().setPredictionMode(PredictionMode.LL);
            final ParseTree root = parser.pr_TTCN3File();
            ParserUtilities.logParseTree(root, parser);
            warnings = parser.getWarnings();
            mErrorsStored = lexerListener.getErrorsStored();
            mErrorsStored.addAll(parserListener.getErrorsStored());
        } catch (RecognitionException e) {
        }
    }
    unsupportedConstructs = parser.getUnsupportedConstructs();
    rootInterval = lexer.getRootInterval();
    actualTtc3Module = parser.getModule();
    if (preprocessor != null) {
        // if the file was preprocessed
        mErrorsStored.addAll(preprocessor.getErrorStorage());
        warnings.addAll(preprocessor.getWarnings());
        unsupportedConstructs.addAll(preprocessor.getUnsupportedConstructs());
        if (actualTtc3Module != null) {
            actualTtc3Module.setIncludedFiles(preprocessor.getIncludedFiles());
            actualTtc3Module.setInactiveCodeLocations(preprocessor.getInactiveCodeLocations());
        }
    }
    try {
        aReader.close();
    } catch (IOException e) {
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CommonTokenFactory(org.antlr.v4.runtime.CommonTokenFactory) IOException(java.io.IOException) PredictionContextCache(org.antlr.v4.runtime.atn.PredictionContextCache) CharStream(org.antlr.v4.runtime.CharStream) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) TitanListener(org.eclipse.titan.common.parsers.TitanListener) ParserATNSimulator(org.antlr.v4.runtime.atn.ParserATNSimulator) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) DFA(org.antlr.v4.runtime.dfa.DFA) ParseTree(org.antlr.v4.runtime.tree.ParseTree) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Aggregations

DFA (org.antlr.v4.runtime.dfa.DFA)37 DFAState (org.antlr.v4.runtime.dfa.DFAState)28 Test (org.junit.Test)19 Grammar (org.antlr.v4.tool.Grammar)18 LexerGrammar (org.antlr.v4.tool.LexerGrammar)18 LexerATNSimulator (org.antlr.v4.runtime.atn.LexerATNSimulator)16 ArrayList (java.util.ArrayList)14 STGroupString (org.stringtemplate.v4.STGroupString)14 CharStream (org.antlr.v4.runtime.CharStream)13 IOException (java.io.IOException)10 IntegerList (org.antlr.v4.runtime.misc.IntegerList)10 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)9 InputStream (java.io.InputStream)8 BitSet (java.util.BitSet)8 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)7 ATN (org.antlr.v4.runtime.atn.ATN)7 Interval (org.antlr.v4.runtime.misc.Interval)7 NotNull (org.antlr.v4.runtime.misc.NotNull)7 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)6 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)5