Search in sources :

Example 6 with ANTLRFileStream

use of org.antlr.v4.runtime.ANTLRFileStream in project antlr4 by antlr.

the class Tool method loadImportedGrammar.

/**
	 * Try current dir then dir of g then lib dir
	 * @param g
	 * @param nameNode The node associated with the imported grammar name.
	 */
public Grammar loadImportedGrammar(Grammar g, GrammarAST nameNode) throws IOException {
    String name = nameNode.getText();
    Grammar imported = importedGrammars.get(name);
    if (imported == null) {
        g.tool.log("grammar", "load " + name + " from " + g.fileName);
        File importedFile = null;
        for (String extension : ALL_GRAMMAR_EXTENSIONS) {
            importedFile = getImportedGrammarFile(g, name + extension);
            if (importedFile != null) {
                break;
            }
        }
        if (importedFile == null) {
            errMgr.grammarError(ErrorType.CANNOT_FIND_IMPORTED_GRAMMAR, g.fileName, nameNode.getToken(), name);
            return null;
        }
        String absolutePath = importedFile.getAbsolutePath();
        ANTLRFileStream in = new ANTLRFileStream(absolutePath, grammarEncoding);
        GrammarRootAST root = parse(g.fileName, in);
        if (root == null) {
            return null;
        }
        imported = createGrammar(root);
        imported.fileName = absolutePath;
        importedGrammars.put(root.getGrammarName(), imported);
    }
    return imported;
}
Also used : ANTLRFileStream(org.antlr.runtime.ANTLRFileStream) GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) File(java.io.File)

Example 7 with ANTLRFileStream

use of org.antlr.v4.runtime.ANTLRFileStream in project Alpha by alpha-asp.

the class HanoiTowerTest method testHanoiTower.

private void testHanoiTower(String instance) throws IOException {
    ANTLRFileStream programInputStream = new ANTLRFileStream(Paths.get("src", "test", "resources", "HanoiTower_Alpha.asp").toString());
    ANTLRFileStream instanceInputStream = new ANTLRFileStream(Paths.get("src", "test", "resources", "HanoiTower_instances", instance + ".asp").toString());
    ParsedProgram parsedProgram = parseVisit(programInputStream);
    parsedProgram.accumulate(parseVisit(instanceInputStream));
    NaiveGrounder grounder = new NaiveGrounder(parsedProgram);
    Solver solver = getInstance(grounder);
    Optional<AnswerSet> answerSet = solver.stream().findFirst();
    System.out.println(answerSet);
    checkGoal(parsedProgram, answerSet.get());
}
Also used : ANTLRFileStream(org.antlr.v4.runtime.ANTLRFileStream) NaiveGrounder(at.ac.tuwien.kr.alpha.grounder.NaiveGrounder)

Example 8 with ANTLRFileStream

use of org.antlr.v4.runtime.ANTLRFileStream in project Alpha by alpha-asp.

the class RacksTest method test.

private void test() throws IOException {
    ANTLRFileStream programInputStream = new ANTLRFileStream(Paths.get("benchmarks", "siemens", "racks", "racks.lp").toString());
    ParsedProgram parsedProgram = parseVisit(programInputStream);
    NaiveGrounder grounder = new NaiveGrounder(parsedProgram);
    Solver solver = getInstance(grounder);
    Optional<AnswerSet> answerSet = solver.stream().findFirst();
    System.out.println(answerSet);
    // TODO: check correctness of answer set
    System.out.println(((DefaultSolver) solver).getDecisionCounter() + " choices," + ((DefaultSolver) solver).getConflictCounter() + " conflicts");
}
Also used : ANTLRFileStream(org.antlr.v4.runtime.ANTLRFileStream) NaiveGrounder(at.ac.tuwien.kr.alpha.grounder.NaiveGrounder) ParsedProgram(at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram) AnswerSet(at.ac.tuwien.kr.alpha.common.AnswerSet)

Example 9 with ANTLRFileStream

use of org.antlr.v4.runtime.ANTLRFileStream in project compiler by boalang.

the class BoaCompiler method parseOnly.

public static void parseOnly(final String[] args) throws IOException {
    final CommandLine cl = processParseCommandLineOptions(args);
    if (cl == null)
        return;
    final ArrayList<File> inputFiles = BoaCompiler.inputFiles;
    // find custom libs to load
    final List<URL> libs = new ArrayList<URL>();
    if (cl.hasOption('l'))
        for (final String lib : cl.getOptionValues('l')) libs.add(new File(lib).toURI().toURL());
    SymbolTable.initialize(libs);
    final int maxVisitors;
    if (cl.hasOption('v'))
        maxVisitors = Integer.parseInt(cl.getOptionValue('v'));
    else
        maxVisitors = Integer.MAX_VALUE;
    for (int i = 0; i < inputFiles.size(); i++) {
        final File f = inputFiles.get(i);
        try {
            final BoaLexer lexer = new BoaLexer(new ANTLRFileStream(f.getAbsolutePath()));
            lexer.removeErrorListeners();
            lexer.addErrorListener(new LexerErrorListener());
            final CommonTokenStream tokens = new CommonTokenStream(lexer);
            final BoaParser parser = new BoaParser(tokens);
            parser.removeErrorListeners();
            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);
                }
            });
            final BoaErrorListener parserErrorListener = new ParserErrorListener();
            final Start p = parse(tokens, parser, parserErrorListener);
            try {
                if (!parserErrorListener.hasError) {
                    new TypeCheckingVisitor().start(p, new SymbolTable());
                    final TaskClassifyingVisitor simpleVisitor = new TaskClassifyingVisitor();
                    simpleVisitor.start(p);
                    LOG.info(f.getName() + ": task complexity: " + (!simpleVisitor.isComplex() ? "simple" : "complex"));
                }
            } catch (final TypeCheckException e) {
                parserErrorListener.error("typecheck", lexer, null, e.n.beginLine, e.n.beginColumn, e.n2.endColumn - e.n.beginColumn + 1, e.getMessage(), e);
            }
        } catch (final Exception e) {
            System.err.print(f.getName() + ": parsing failed: ");
            e.printStackTrace();
        }
    }
}
Also used : BoaParser(boa.parser.BoaParser) Start(boa.compiler.ast.Start) ArrayList(java.util.ArrayList) URL(java.net.URL) BoaErrorListener(boa.compiler.listeners.BoaErrorListener) TypeCheckingVisitor(boa.compiler.visitors.TypeCheckingVisitor) BoaLexer(boa.parser.BoaLexer) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) LexerErrorListener(boa.compiler.listeners.LexerErrorListener) BaseErrorListener(org.antlr.v4.runtime.BaseErrorListener) TaskClassifyingVisitor(boa.compiler.visitors.TaskClassifyingVisitor) FileNotFoundException(java.io.FileNotFoundException) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) IOException(java.io.IOException) RecognitionException(org.antlr.v4.runtime.RecognitionException) ParserErrorListener(boa.compiler.listeners.ParserErrorListener) CommandLine(org.apache.commons.cli.CommandLine) ANTLRFileStream(org.antlr.v4.runtime.ANTLRFileStream) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) File(java.io.File) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Aggregations

ANTLRFileStream (org.antlr.v4.runtime.ANTLRFileStream)5 File (java.io.File)4 AnswerSet (at.ac.tuwien.kr.alpha.common.AnswerSet)3 NaiveGrounder (at.ac.tuwien.kr.alpha.grounder.NaiveGrounder)3 ParsedProgram (at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram)3 IOException (java.io.IOException)3 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)3 Start (boa.compiler.ast.Start)2 BoaErrorListener (boa.compiler.listeners.BoaErrorListener)2 LexerErrorListener (boa.compiler.listeners.LexerErrorListener)2 ParserErrorListener (boa.compiler.listeners.ParserErrorListener)2 TaskClassifyingVisitor (boa.compiler.visitors.TaskClassifyingVisitor)2 TypeCheckingVisitor (boa.compiler.visitors.TypeCheckingVisitor)2 BoaLexer (boa.parser.BoaLexer)2 BoaParser (boa.parser.BoaParser)2 FileNotFoundException (java.io.FileNotFoundException)2 URL (java.net.URL)2 ASPCore2Lexer (at.ac.tuwien.kr.alpha.antlr.ASPCore2Lexer)1 ASPCore2Parser (at.ac.tuwien.kr.alpha.antlr.ASPCore2Parser)1 Predicate (at.ac.tuwien.kr.alpha.common.Predicate)1