List of usage examples for org.antlr.v4.runtime.atn PredictionMode SLL
PredictionMode SLL
To view the source code for org.antlr.v4.runtime.atn PredictionMode SLL.
Click Source Link
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);/* w ww . java2s .co 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();//from ww w .j a va 2 s . co 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.github.jknack.handlebars.internal.HbsParserFactory.java
License:Apache License
/** * Configure a {@link HbsParser}./*from w w w. j a va 2 s .c om*/ * * @param parser The {@link HbsParser}. * @param errorReporter The error reporter. */ @SuppressWarnings("rawtypes") private void configure(final HbsParser parser, final ANTLRErrorListener errorReporter) { configure((Recognizer) parser, errorReporter); parser.setErrorHandler(new HbsErrorStrategy()); parser.getInterpreter().setPredictionMode(PredictionMode.SLL); }
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// w ww . ja va 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();/*w ww. jav a 2 s . co 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 yqlplusParser prepareParser(final String programName, CharStream input) { yqlplusLexer lex = new yqlplusLexer(input); lex.addErrorListener(new BaseErrorListener() { @Override//www .ja va 2s . c o m public void syntaxError(@NotNull Recognizer<?, ?> recognizer, @Nullable Object offendingSymbol, int line, int charPositionInLine, @NotNull String msg, @Nullable RecognitionException e) { throw new ProgramCompileException(new Location(programName, line, charPositionInLine), msg); } }); TokenStream tokens = new CommonTokenStream(lex); yqlplusParser parser = new yqlplusParser(tokens); parser.addErrorListener(new BaseErrorListener() { @Override public void syntaxError(@NotNull Recognizer<?, ?> recognizer, @Nullable Object offendingSymbol, int line, int charPositionInLine, @NotNull String msg, @Nullable RecognitionException e) { throw new ProgramCompileException(new Location(programName, line, charPositionInLine), msg); } }); parser.getInterpreter().setPredictionMode(PredictionMode.SLL); return parser; }
From source file:de.up.ling.irtg.codec.BottomUpTreeAutomatonInputCodec.java
@Override public TreeAutomaton read(InputStream is) throws ParseException, IOException { BottomUpTreeAutomatonLexer l = new BottomUpTreeAutomatonLexer(new ANTLRInputStream(is)); BottomUpTreeAutomatonParser p = new BottomUpTreeAutomatonParser(new CommonTokenStream(l)); p.setErrorHandler(new ExceptionErrorStrategy()); p.getInterpreter().setPredictionMode(PredictionMode.SLL); automaton = new ConcreteTreeAutomaton<String>(); try {//from ww w.j av a 2s . com BottomUpTreeAutomatonParser.FtaContext result = p.fta(); for (BottomUpTreeAutomatonParser.StateContext stateContext : result.state()) { automaton.addFinalState(automaton.addState(state(stateContext))); } for (BottomUpTreeAutomatonParser.Auto_ruleContext ruleContext : result.auto_rule()) { autoRule(ruleContext); } return automaton; } catch (RecognitionException e) { throw new ParseException(e.getMessage()); } }
From source file:de.up.ling.irtg.codec.IrtgInputCodec.java
@Override public InterpretedTreeAutomaton read(InputStream is) throws IOException, ParseException { IrtgLexer l = new IrtgLexer(new ANTLRInputStream(is)); IrtgParser p = new IrtgParser(new CommonTokenStream(l)); p.setErrorHandler(new ExceptionErrorStrategy()); p.getInterpreter().setPredictionMode(PredictionMode.SLL); automaton = new ConcreteTreeAutomaton<String>(); homomorphisms = new HashMap<String, Homomorphism>(); interpretations = new HashMap<String, Interpretation>(); features = new HashMap<String, FeatureFunction>(); try {/*from www .j a v a 2 s. com*/ IrtgParser.IrtgContext result = p.irtg(); InterpretedTreeAutomaton irtg = build(result); return irtg; } catch (ParseException e) { throw e; } catch (RecognitionException e) { throw new ParseException(e.getMessage()); } }
From source file:de.up.ling.irtg.codec.PcfgIrtgInputCodec.java
@Override public InterpretedTreeAutomaton read(InputStream is) throws ParseException, IOException { PcfgAsIrtgLexer l = new PcfgAsIrtgLexer(new ANTLRInputStream(is)); PcfgAsIrtgParser p = new PcfgAsIrtgParser(new CommonTokenStream(l)); p.setErrorHandler(new ExceptionErrorStrategy()); p.getInterpreter().setPredictionMode(PredictionMode.SLL); try {/*from w ww.ja va 2 s. com*/ PcfgAsIrtgParser.PcfgContext result = p.pcfg(); ConcreteTreeAutomaton<String> auto = new ConcreteTreeAutomaton<>(); StringAlgebra stringAlgebra = new StringAlgebra(); Homomorphism stringHom = new Homomorphism(auto.getSignature(), stringAlgebra.getSignature()); TreeWithAritiesAlgebra treeAlgebra = new TreeWithAritiesAlgebra(); Homomorphism treeHom = new Homomorphism(auto.getSignature(), treeAlgebra.getSignature()); pcfg(result, auto, stringHom, treeHom); InterpretedTreeAutomaton irtg = new InterpretedTreeAutomaton(auto); irtg.addInterpretation("string", new Interpretation(stringAlgebra, stringHom)); irtg.addInterpretation("tree", new Interpretation(treeAlgebra, treeHom)); return irtg; } catch (RecognitionException e) { throw new ParseException(e.getMessage()); } }
From source file:de.up.ling.irtg.codec.TemplateIrtgInputCodec.java
@Override public TemplateInterpretedTreeAutomaton read(InputStream is) throws ParseException, IOException { TemplateIrtgLexer l = new TemplateIrtgLexer(new ANTLRInputStream(is)); TemplateIrtgParser p = new TemplateIrtgParser(new CommonTokenStream(l)); p.setErrorHandler(new ExceptionErrorStrategy()); p.getInterpreter().setPredictionMode(PredictionMode.SLL); tirtg = new TemplateInterpretedTreeAutomaton(); try {//from w ww . j a v a2 s . com TemplateIrtgParser.Template_irtgContext result = p.template_irtg(); build(result); return tirtg; } catch (ParseException e) { throw e; } catch (RecognitionException e) { throw new ParseException(e.getMessage()); } }