Example usage for org.antlr.v4.runtime.atn PredictionMode LL

List of usage examples for org.antlr.v4.runtime.atn PredictionMode LL

Introduction

In this page you can find the example usage for org.antlr.v4.runtime.atn PredictionMode LL.

Prototype

PredictionMode LL

To view the source code for org.antlr.v4.runtime.atn PredictionMode LL.

Click Source Link

Document

The LL(*) prediction mode.

Usage

From source file:boa.compiler.BoaCompiler.java

License:Apache License

private static Start parse(final CommonTokenStream tokens, final BoaParser parser,
        final BoaErrorListener parserErrorListener) {

    parser.setBuildParseTree(false);//from   ww w .ja  v a  2 s.  c o  m
    parser.getInterpreter().setPredictionMode(PredictionMode.SLL);

    try {
        return parser.start().ast;
    } catch (final ParseCancellationException e) {
        // fall-back to LL mode parsing if SLL fails
        tokens.reset();
        parser.reset();

        parser.removeErrorListeners();
        parser.addErrorListener(parserErrorListener);
        parser.getInterpreter().setPredictionMode(PredictionMode.LL);

        return parser.start().ast;
    }
}

From source file:com.facebook.presto.type.TypeCalculation.java

License:Apache License

private static ParserRuleContext parseTypeCalculation(String calculation) {
    TypeCalculationLexer lexer = new TypeCalculationLexer(
            new CaseInsensitiveStream(new ANTLRInputStream(calculation)));
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    TypeCalculationParser parser = new TypeCalculationParser(tokenStream);

    lexer.removeErrorListeners();// w  w  w. j  a v  a 2 s. c  o  m
    lexer.addErrorListener(ERROR_LISTENER);

    parser.removeErrorListeners();
    parser.addErrorListener(ERROR_LISTENER);

    ParserRuleContext tree;
    try {
        // first, try parsing with potentially faster SLL mode
        parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
        tree = parser.typeCalculation();
    } catch (ParseCancellationException ex) {
        // if we fail, parse with LL mode
        tokenStream.reset(); // rewind input stream
        parser.reset();

        parser.getInterpreter().setPredictionMode(PredictionMode.LL);
        tree = parser.typeCalculation();
    }
    return tree;
}

From source file:com.ibm.bi.dml.parser.python.PyDMLParserWrapper.java

License:Open Source License

/**
 * This function is supposed to be called directly only from PydmlSyntacticValidator when it encounters 'import'
 * @param fileName/*from   w  ww. j  a v  a  2  s  .c o  m*/
 * @return null if atleast one error
 */
public DMLProgram doParse(String fileName, String dmlScript, HashMap<String, String> argVals)
        throws ParseException {
    DMLProgram dmlPgm = null;

    ANTLRInputStream in;
    try {
        if (dmlScript == null) {
            dmlScript = DMLParserWrapper.readDMLScript(fileName);
        }

        InputStream stream = new ByteArrayInputStream(dmlScript.getBytes());
        in = new org.antlr.v4.runtime.ANTLRInputStream(stream);
        //         else {
        //            if(!(new File(fileName)).exists()) {
        //               throw new ParseException("ERROR: Cannot open file:" + fileName);
        //            }
        //            in = new ANTLRInputStream(new FileInputStream(fileName));
        //         }
    } catch (FileNotFoundException e) {
        throw new ParseException("ERROR: Cannot find file:" + fileName);
    } catch (IOException e) {
        throw new ParseException("ERROR: Cannot open file:" + fileName);
    } catch (LanguageException e) {
        throw new ParseException("ERROR: " + e.getMessage());
    }

    PmlprogramContext ast = null;
    CustomDmlErrorListener errorListener = new CustomDmlErrorListener();

    try {
        PydmlLexer lexer = new PydmlLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        PydmlParser antlr4Parser = new PydmlParser(tokens);

        boolean tryOptimizedParsing = false; // For now no optimization, since it is not able to parse integer value. 

        if (tryOptimizedParsing) {
            // Try faster and simpler SLL
            antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
            antlr4Parser.removeErrorListeners();
            antlr4Parser.setErrorHandler(new BailErrorStrategy());
            try {
                ast = antlr4Parser.pmlprogram();
                // If successful, no need to try out full LL(*) ... SLL was enough
            } catch (ParseCancellationException ex) {
                // Error occurred, so now try full LL(*) for better error messages
                tokens.reset();
                antlr4Parser.reset();
                if (fileName != null) {
                    errorListener.pushCurrentFileName(fileName);
                } else {
                    errorListener.pushCurrentFileName("MAIN_SCRIPT");
                }
                // Set our custom error listener
                antlr4Parser.addErrorListener(errorListener);
                antlr4Parser.setErrorHandler(new DefaultErrorStrategy());
                antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.LL);
                ast = antlr4Parser.pmlprogram();
            }
        } else {
            // Set our custom error listener
            antlr4Parser.removeErrorListeners();
            antlr4Parser.addErrorListener(errorListener);
            errorListener.pushCurrentFileName(fileName);

            // Now do the parsing
            ast = antlr4Parser.pmlprogram();
        }
    } catch (Exception e) {
        throw new ParseException("ERROR: Cannot parse the program:" + fileName);
    }

    try {
        // Now convert the parse tree into DMLProgram
        // Do syntactic validation while converting 
        ParseTree tree = ast;
        // And also do syntactic validation
        ParseTreeWalker walker = new ParseTreeWalker();
        PydmlSyntacticValidatorHelper helper = new PydmlSyntacticValidatorHelper(errorListener);
        PydmlSyntacticValidator validator = new PydmlSyntacticValidator(helper, fileName, argVals);
        walker.walk(validator, tree);
        errorListener.popFileName();
        if (errorListener.isAtleastOneError()) {
            return null;
        }
        dmlPgm = createDMLProgram(ast);
    } catch (Exception e) {
        throw new ParseException("ERROR: Cannot translate the parse tree into DMLProgram:" + e.getMessage());
    }

    return dmlPgm;
}

From source file:com.xiaomi.linden.bql.BQLCompiler.java

License:Apache License

public LindenRequest compile(String bqlStmt) {
    // Lexer splits input into tokens
    ANTLRInputStream input = new ANTLRInputStream(bqlStmt);
    BQLLexer lexer = new BQLLexer(input);
    lexer.removeErrorListeners();/*from  w  w w. j  a va 2 s. c o m*/
    TokenStream tokens = new CommonTokenStream(lexer);

    // Parser generates abstract syntax tree
    BQLParser parser = new BQLParser(tokens);
    parser.removeErrorListeners();
    BQLCompilerAnalyzer analyzer = new BQLCompilerAnalyzer(parser, lindenSchema);

    /*You can save a great deal of time on correct inputs by using a two-stage parsing strategy.
      1. Attempt to parse the input using BailErrorStrategy and PredictionMode.SLL.
         If no exception is thrown, you know the answer is correct.
      2. If a ParseCancellationException is thrown, retry the parse using the
         default settings (DefaultErrorStrategy and PredictionMode.LL).
    */
    parser.setErrorHandler(new BailErrorStrategy());
    parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    try {
        BQLParser.StatementContext ret = parser.statement();
        ParseTreeWalker.DEFAULT.walk(analyzer, ret);
        return analyzer.getLindenRequest(ret);
    } catch (ParseCancellationException e) {
        try {
            parser.reset();
            parser.getInterpreter().setPredictionMode(PredictionMode.LL);
            BQLParser.StatementContext ret = parser.statement();
            ParseTreeWalker.DEFAULT.walk(analyzer, ret);
            return analyzer.getLindenRequest(ret);
        } catch (Exception e2) {
            throw new RuntimeException(getErrorMessage(e2));
        }
    } catch (Exception e) {
        throw new RuntimeException(getErrorMessage(e));
    }
}

From source file:com.yahoo.yqlplus.language.parser.ProgramParser.java

private ProgramContext parseProgram(yqlplusParser parser) throws RecognitionException {
    try {//ww w  .  j a  va 2s  .co m
        return parser.program();
    } catch (RecognitionException e) {
        //Retry parsing using full LL mode
        parser.reset();
        parser.getInterpreter().setPredictionMode(PredictionMode.LL);
        return parser.program();
    }
}

From source file:edu.clemson.cs.rsrg.init.Controller.java

License:Open Source License

/**
 * <p>This method uses the {@link ResolveFile} provided
 * to construct a parser and create an ANTLR4 module AST.</p>
 *
 * @param file The RESOLVE file that we are going to compile.
 *
 * @return The inner representation for a module. See {@link ModuleDec}.
 *
 * @throws MiscErrorException Some how we couldn't instantiate an {@link CharStream}.
 * @throws SourceErrorException There are errors in the source file.
 *///from  w w w  .j  a  va2  s .c  o m
private ModuleDec createModuleAST(ResolveFile file) {
    CharStream input = file.getInputStream();
    if (input == null) {
        throw new MiscErrorException("CharStream null", new IllegalArgumentException());
    }

    // Create a RESOLVE language lexer
    ResolveLexer lexer = new ResolveLexer(input);
    ResolveTokenFactory factory = new ResolveTokenFactory(file);
    lexer.removeErrorListeners();
    lexer.addErrorListener(myAntlrLexerErrorListener);
    lexer.setTokenFactory(factory);

    // Create a RESOLVE language parser
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ResolveParser parser = new ResolveParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(myAntlrParserErrorListener);
    parser.setTokenFactory(factory);

    // Two-Stage Parsing
    // Reason: We might not need the full power of LL.
    // The solution proposed by the ANTLR folks (found here:
    // https://github.com/antlr/antlr4/blob/master/doc/faq/general.md)
    // is to use SLL prediction mode first and switch to LL if it fails.
    ParserRuleContext rootModuleCtx;
    parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    try {
        rootModuleCtx = parser.module();
    } catch (Exception ex) {
        tokens.seek(0);
        parser.reset();
        parser.getInterpreter().setPredictionMode(PredictionMode.LL);
        rootModuleCtx = parser.module();
    }

    // Check for any parsing errors
    int numParserErrors = parser.getNumberOfSyntaxErrors();
    if (numParserErrors != 0) {
        throw new MiscErrorException("Found " + numParserErrors + " errors while parsing " + file.toString(),
                new IllegalStateException());
    }

    // Build the intermediate representation
    TreeBuildingListener v = new TreeBuildingListener(file, myCompileEnvironment.getTypeGraph());
    ParseTreeWalker.DEFAULT.walk(v, rootModuleCtx);

    return v.getModule();
}

From source file:edu.iastate.cs.boa.ui.errors.FetchCompilerError.java

License:Apache License

protected StartContext parse(final String input, final String[] errors) throws IOException {
    final CommonTokenStream tokens = lex(input);
    final BoaParser parser = new BoaParser(tokens);
    final List<String> foundErr = new ArrayList<String>();
    parser.removeErrorListeners();//w w  w .  jav a 2  s . c  o  m
    parser.addErrorListener(new BaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
                int charPositionInLine, String msg, RecognitionException e) throws ParseCancellationException {
            throw new ParseCancellationException(e);
        }
    });

    parseErrorListener = new ParserErrorListener();
    parser.setBuildParseTree(false);
    parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    StartContext p;
    try {
        p = parser.start();
    } catch (final Exception e) {
        // fall-back to LL mode parsing if SLL fails
        tokens.reset();
        parser.reset();

        parser.removeErrorListeners();
        parser.addErrorListener(new BaseErrorListener() {
            @Override
            public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol,
                    final int line, final int charPositionInLine, final String msg,
                    final RecognitionException e) {
                foundErr.add(line + "," + charPositionInLine + ": " + msg);
            }
        });
        parser.getInterpreter().setPredictionMode(PredictionMode.LL);

        p = parser.start();
    }

    return p;
}

From source file:io.prestosql.sql.parser.SqlParser.java

License:Apache License

private Node invokeParser(String name, String sql, Function<SqlBaseParser, ParserRuleContext> parseFunction,
        ParsingOptions parsingOptions) {
    try {/*from w w  w.j av  a 2  s  .c o m*/
        SqlBaseLexer lexer = new SqlBaseLexer(new CaseInsensitiveStream(CharStreams.fromString(sql)));
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        SqlBaseParser parser = new SqlBaseParser(tokenStream);

        // Override the default error strategy to not attempt inserting or deleting a token.
        // Otherwise, it messes up error reporting
        parser.setErrorHandler(new DefaultErrorStrategy() {
            @Override
            public Token recoverInline(Parser recognizer) throws RecognitionException {
                if (nextTokensContext == null) {
                    throw new InputMismatchException(recognizer);
                } else {
                    throw new InputMismatchException(recognizer, nextTokensState, nextTokensContext);
                }
            }
        });

        parser.addParseListener(new PostProcessor(Arrays.asList(parser.getRuleNames())));

        lexer.removeErrorListeners();
        lexer.addErrorListener(LEXER_ERROR_LISTENER);

        parser.removeErrorListeners();

        if (enhancedErrorHandlerEnabled) {
            parser.addErrorListener(PARSER_ERROR_HANDLER);
        } else {
            parser.addErrorListener(LEXER_ERROR_LISTENER);
        }

        ParserRuleContext tree;
        try {
            // first, try parsing with potentially faster SLL mode
            parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
            tree = parseFunction.apply(parser);
        } catch (ParseCancellationException ex) {
            // if we fail, parse with LL mode
            tokenStream.reset(); // rewind input stream
            parser.reset();

            parser.getInterpreter().setPredictionMode(PredictionMode.LL);
            tree = parseFunction.apply(parser);
        }

        return new AstBuilder(parsingOptions).visit(tree);
    } catch (StackOverflowError e) {
        throw new ParsingException(name + " is too large (stack overflow while parsing)");
    }
}

From source file:org.apache.olingo.server.core.uri.parser.Parser.java

License:Apache License

private ParserRuleContext parseRule(final String input, final ParserEntryRules entryPoint)
        throws UriParserSyntaxException {
    UriParserParser parser = null;//from  w w w . j  a v a  2s.  c om
    UriLexer lexer = null;
    ParserRuleContext ret = null;

    // Use 2 stage approach to improve performance
    // see https://github.com/antlr/antlr4/issues/192

    // stage = 1
    try {

        // create parser
        if (logLevel > 0) {
            //TODO: Discuss if we should keep this code
            lexer = new UriLexer(new ANTLRInputStream(input));
            showTokens(input, lexer.getAllTokens());
        }

        lexer = new UriLexer(new ANTLRInputStream(input));
        parser = new UriParserParser(new CommonTokenStream(lexer));

        // Set error strategy
        addStage1ErrorStategy(parser);

        // Set error collector
        addStage1ErrorListener(parser);

        // user the faster LL parsing
        parser.getInterpreter().setPredictionMode(PredictionMode.SLL);

        // parse
        switch (entryPoint) {
        case All:
            ret = parser.allEOF();
            break;
        case Batch:
            ret = parser.batchEOF();
            break;
        case CrossJoin:
            ret = parser.crossjoinEOF();
            break;
        case Metadata:
            ret = parser.metadataEOF();
            break;
        case PathSegment:
            ret = parser.pathSegmentEOF();
            break;
        case FilterExpression:
            lexer.mode(Lexer.DEFAULT_MODE);
            ret = parser.filterExpressionEOF();
            break;
        case Orderby:
            lexer.mode(Lexer.DEFAULT_MODE);
            ret = parser.orderByEOF();
            break;
        case ExpandItems:
            lexer.mode(Lexer.DEFAULT_MODE);
            ret = parser.expandItemsEOF();
            break;
        case Entity:
            ret = parser.entityEOF();
            break;
        case Select:
            ret = parser.selectEOF();
            break;
        default:
            break;

        }

    } catch (ParseCancellationException hardException) {
        // stage = 2
        try {

            // create parser
            lexer = new UriLexer(new ANTLRInputStream(input));
            parser = new UriParserParser(new CommonTokenStream(lexer));

            // Set error strategy
            addStage2ErrorStategy(parser);

            // Set error collector
            addStage2ErrorListener(parser);

            // Use the slower SLL parsing
            parser.getInterpreter().setPredictionMode(PredictionMode.LL);

            // parse
            switch (entryPoint) {
            case All:
                ret = parser.allEOF();
                break;
            case Batch:
                ret = parser.batchEOF();
                break;
            case CrossJoin:
                ret = parser.crossjoinEOF();
                break;
            case Metadata:
                ret = parser.metadataEOF();
                break;
            case PathSegment:
                ret = parser.pathSegmentEOF();
                break;
            case FilterExpression:
                lexer.mode(Lexer.DEFAULT_MODE);
                ret = parser.filterExpressionEOF();
                break;
            case Orderby:
                lexer.mode(Lexer.DEFAULT_MODE);
                ret = parser.orderByEOF();
                break;
            case ExpandItems:
                lexer.mode(Lexer.DEFAULT_MODE);
                ret = parser.expandItemsEOF();
                break;
            case Entity:
                ret = parser.entityEOF();
                break;
            case Select:
                ret = parser.selectEOF();
                break;
            default:
                break;
            }

        } catch (final RecognitionException weakException) {
            throw new UriParserSyntaxException("Error in syntax", weakException,
                    UriParserSyntaxException.MessageKeys.SYNTAX);

            // exceptionOnStage = 2;
        }
    } catch (final RecognitionException hardException) {
        throw new UriParserSyntaxException("Error in syntax", hardException,
                UriParserSyntaxException.MessageKeys.SYNTAX);
    }

    return ret;
}

From source file:org.apache.sysml.parser.dml.DMLParserWrapper.java

License:Apache License

/**
 * This function is supposed to be called directly only from DmlSyntacticValidator when it encounters 'import'
 * @param fileName script file name//from ww w. ja  v a2  s .  co m
 * @param dmlScript script file contents
 * @param sourceNamespace namespace from source statement
 * @param argVals script arguments
 * @return dml program, or null if at least one error
 * @throws ParseException if ParseException occurs
 */
public DMLProgram doParse(String fileName, String dmlScript, String sourceNamespace,
        Map<String, String> argVals) throws ParseException {
    DMLProgram dmlPgm = null;

    ANTLRInputStream in;
    try {
        if (dmlScript == null) {
            dmlScript = readDMLScript(fileName, LOG);
        }

        InputStream stream = new ByteArrayInputStream(dmlScript.getBytes());
        in = new ANTLRInputStream(stream);
    } catch (FileNotFoundException e) {
        throw new ParseException("Cannot find file/resource: " + fileName, e);
    } catch (IOException e) {
        throw new ParseException("Cannot open file: " + fileName, e);
    } catch (LanguageException e) {
        throw new ParseException(e.getMessage(), e);
    }

    ProgramrootContext ast = null;
    CustomErrorListener errorListener = new CustomErrorListener();

    try {
        DmlLexer lexer = new DmlLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        DmlParser antlr4Parser = new DmlParser(tokens);

        boolean tryOptimizedParsing = false; // For now no optimization, since it is not able to parse integer value. 

        if (tryOptimizedParsing) {
            // Try faster and simpler SLL
            antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
            antlr4Parser.removeErrorListeners();
            antlr4Parser.setErrorHandler(new BailErrorStrategy());
            try {
                ast = antlr4Parser.programroot();
                // If successful, no need to try out full LL(*) ... SLL was enough
            } catch (ParseCancellationException ex) {
                // Error occurred, so now try full LL(*) for better error messages
                tokens.reset();
                antlr4Parser.reset();
                if (fileName != null) {
                    errorListener.setCurrentFileName(fileName);
                } else {
                    errorListener.setCurrentFileName("MAIN_SCRIPT");
                }
                // Set our custom error listener
                antlr4Parser.addErrorListener(errorListener);
                antlr4Parser.setErrorHandler(new DefaultErrorStrategy());
                antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.LL);
                ast = antlr4Parser.programroot();
            }
        } else {
            // Set our custom error listener
            antlr4Parser.removeErrorListeners();
            antlr4Parser.addErrorListener(errorListener);
            errorListener.setCurrentFileName(fileName);

            // Now do the parsing
            ast = antlr4Parser.programroot();
        }
    } catch (Exception e) {
        throw new ParseException("ERROR: Cannot parse the program:" + fileName, e);
    }

    // Now convert the parse tree into DMLProgram
    // Do syntactic validation while converting 
    ParseTree tree = ast;
    // And also do syntactic validation
    ParseTreeWalker walker = new ParseTreeWalker();
    // Get list of function definitions which take precedence over built-in functions if same name
    DmlPreprocessor prep = new DmlPreprocessor(errorListener);
    walker.walk(prep, tree);
    // Syntactic validation
    DmlSyntacticValidator validator = new DmlSyntacticValidator(errorListener, argVals, sourceNamespace,
            prep.getFunctionDefs());
    walker.walk(validator, tree);
    errorListener.unsetCurrentFileName();
    this.parseIssues = errorListener.getParseIssues();
    this.atLeastOneWarning = errorListener.isAtLeastOneWarning();
    this.atLeastOneError = errorListener.isAtLeastOneError();
    if (atLeastOneError) {
        throw new ParseException(parseIssues, dmlScript);
    }
    if (atLeastOneWarning) {
        LOG.warn(CustomErrorListener.generateParseIssuesMessage(dmlScript, parseIssues));
    }
    dmlPgm = createDMLProgram(ast, sourceNamespace);

    return dmlPgm;
}