Search in sources :

Example 1 with ASPCore2Parser

use of at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser in project Alpha by alpha-asp.

the class ProgramParserImpl method parse.

public ASPCore2Program parse(CharStream stream, Map<String, PredicateInterpretation> externals) {
    // @formatter:off
    /*
		 * // In order to require less memory: use unbuffered streams and avoid constructing a full parse tree. 
		 * ASPCore2Lexer lexer = new ASPCore2Lexer(new UnbufferedCharStream(is)); 
		 * lexer.setTokenFactory(new CommonTokenFactory(true)); 
		 * final ASPCore2Parser parser = new ASPCore2Parser(new UnbufferedTokenStream<>(lexer)); 
		 * parser.setBuildParseTree(false);
		 */
    // @formatter:on
    CommonTokenStream tokens = new CommonTokenStream(new ASPCore2Lexer(stream));
    final ASPCore2Parser parser = new ASPCore2Parser(tokens);
    // Try SLL parsing mode (faster but may terminate incorrectly).
    parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    parser.removeErrorListeners();
    parser.setErrorHandler(new BailErrorStrategy());
    final CustomErrorListener errorListener = new CustomErrorListener(stream.getSourceName());
    ASPCore2Parser.ProgramContext programContext;
    try {
        // Parse program
        programContext = parser.program();
    } catch (ParseCancellationException e) {
        // retry with LL parser and DefaultErrorStrategy printing errors to console.
        if (e.getCause() instanceof RecognitionException) {
            tokens.seek(0);
            parser.addErrorListener(errorListener);
            parser.setErrorHandler(new DefaultErrorStrategy());
            parser.getInterpreter().setPredictionMode(PredictionMode.LL);
            // Re-run parse.
            programContext = parser.program();
        } else {
            throw e;
        }
    }
    // is attempted) and the user will only see the first error encountered.
    if (errorListener.getRecognitionException() != null) {
        throw errorListener.getRecognitionException();
    }
    // Abort parsing if there were some (recoverable) syntax errors.
    if (parser.getNumberOfSyntaxErrors() != 0) {
        throw new ParseCancellationException();
    }
    // The union of this parser's preloaded externals and the (program-specific) externals passed to the parse method
    Map<String, PredicateInterpretation> knownExternals;
    if (externals != null && !externals.isEmpty()) {
        knownExternals = new HashMap<>(preloadedExternals);
        knownExternals.putAll(externals);
    } else {
        knownExternals = preloadedExternals;
    }
    // Construct internal program representation.
    ParseTreeVisitor visitor = new ParseTreeVisitor(knownExternals);
    return visitor.translate(programContext);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) PredicateInterpretation(at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation) ASPCore2Parser(at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser) ASPCore2Lexer(at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Lexer) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) DefaultErrorStrategy(org.antlr.v4.runtime.DefaultErrorStrategy) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 2 with ASPCore2Parser

use of at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser in project Alpha by alpha-asp.

the class AnswerSetsParser method parse.

public static Set<AnswerSet> parse(CharStream stream) {
    final ASPCore2Parser parser = new ASPCore2Parser(new CommonTokenStream(new ASPCore2Lexer(stream)));
    // Try SLL parsing mode (faster but may terminate incorrectly).
    parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    parser.removeErrorListeners();
    parser.setErrorHandler(new BailErrorStrategy());
    return VISITOR.translate(parser.answer_sets());
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ASPCore2Lexer(at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Lexer) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) ASPCore2Parser(at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser)

Example 3 with ASPCore2Parser

use of at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser in project Alpha by alpha-asp.

the class AnswerSetsParser method parse.

public static Set<AnswerSet> parse(CharStream stream) {
    final ASPCore2Parser parser = new ASPCore2Parser(new CommonTokenStream(new ASPCore2Lexer(stream)));
    // Try SLL parsing mode (faster but may terminate incorrectly).
    parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    parser.removeErrorListeners();
    parser.setErrorHandler(new BailErrorStrategy());
    return VISITOR.translate(parser.answer_sets());
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ASPCore2Lexer(at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Lexer) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) ASPCore2Parser(at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser)

Aggregations

ASPCore2Lexer (at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Lexer)3 ASPCore2Parser (at.ac.tuwien.kr.alpha.core.antlr.ASPCore2Parser)3 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)3 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)3 PredicateInterpretation (at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation)1 DefaultErrorStrategy (org.antlr.v4.runtime.DefaultErrorStrategy)1 RecognitionException (org.antlr.v4.runtime.RecognitionException)1 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)1