Search in sources :

Example 1 with ANTLRFileStream

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

the class Tool method parseGrammar.

public GrammarRootAST parseGrammar(String fileName) {
    try {
        File file = new File(fileName);
        if (!file.isAbsolute()) {
            file = new File(inputDirectory, fileName);
        }
        ANTLRFileStream in = new ANTLRFileStream(file.getAbsolutePath(), grammarEncoding);
        GrammarRootAST t = parse(fileName, in);
        return t;
    } catch (IOException ioe) {
        errMgr.toolError(ErrorType.CANNOT_OPEN_FILE, ioe, fileName);
    }
    return null;
}
Also used : ANTLRFileStream(org.antlr.runtime.ANTLRFileStream) GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) IOException(java.io.IOException) File(java.io.File)

Example 2 with ANTLRFileStream

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

the class Main method main.

public static void main(String[] args) {
    final Options options = new Options();
    Option numAnswerSetsOption = new Option("n", OPT_NUM_AS, true, "the number of Answer Sets to compute");
    numAnswerSetsOption.setArgName("number");
    numAnswerSetsOption.setRequired(false);
    numAnswerSetsOption.setArgs(1);
    numAnswerSetsOption.setType(Number.class);
    options.addOption(numAnswerSetsOption);
    Option inputOption = new Option("i", OPT_INPUT, true, "read the ASP program from this file");
    inputOption.setArgName("file");
    inputOption.setRequired(true);
    inputOption.setArgs(1);
    inputOption.setType(FileInputStream.class);
    options.addOption(inputOption);
    Option helpOption = new Option("h", OPT_HELP, false, "show this help");
    options.addOption(helpOption);
    Option grounderOption = new Option("g", OPT_GROUNDER, false, "name of the grounder implementation to use");
    grounderOption.setArgs(1);
    grounderOption.setArgName("grounder");
    options.addOption(grounderOption);
    Option solverOption = new Option("s", OPT_SOLVER, false, "name of the solver implementation to use");
    solverOption.setArgs(1);
    solverOption.setArgName("solver");
    options.addOption(solverOption);
    Option filterOption = new Option("f", OPT_FILTER, true, "predicates to show when printing answer sets");
    filterOption.setArgs(1);
    filterOption.setArgName("filter");
    filterOption.setValueSeparator(',');
    options.addOption(filterOption);
    Option sortOption = new Option("sort", OPT_SORT, false, "sort answer sets");
    options.addOption(sortOption);
    Option deterministicOption = new Option("d", OPT_DETERMINISTIC, false, "disable randomness");
    options.addOption(deterministicOption);
    Option seedOption = new Option("e", OPT_SEED, true, "set seed");
    seedOption.setArgName("number");
    seedOption.setRequired(false);
    seedOption.setArgs(1);
    seedOption.setType(Number.class);
    options.addOption(seedOption);
    Option debugFlags = new Option(OPT_DEBUG_INTERNAL_CHECKS, "run additional (time-consuming) safety checks.");
    options.addOption(debugFlags);
    Option branchingHeuristicOption = new Option("b", OPT_BRANCHING_HEURISTIC, false, "name of the branching heuristic to use");
    branchingHeuristicOption.setArgs(1);
    branchingHeuristicOption.setArgName("heuristic");
    options.addOption(branchingHeuristicOption);
    try {
        commandLine = new DefaultParser().parse(options, args);
    } catch (ParseException e) {
        System.err.println(e.getMessage());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("java -jar alpha.jar\njava -jar alpha_bundled.jar", options);
        System.exit(1);
        return;
    }
    if (commandLine.hasOption(OPT_HELP)) {
        HelpFormatter formatter = new HelpFormatter();
        // TODO(flowlo): This is quite optimistic. How do we know that the program
        // really was invoked as "java -jar ..."?
        formatter.printHelp("java -jar alpha.jar OR java -jar alpha-bundled.jar", options);
        System.exit(0);
        return;
    }
    java.util.function.Predicate<Predicate> filter = p -> true;
    if (commandLine.hasOption(OPT_FILTER)) {
        Set<String> desiredPredicates = new HashSet<>(Arrays.asList(commandLine.getOptionValues(OPT_FILTER)));
        filter = p -> {
            return desiredPredicates.contains(p.getPredicateName());
        };
    }
    int limit = 0;
    try {
        Number n = (Number) commandLine.getParsedOptionValue(OPT_NUM_AS);
        if (n != null) {
            limit = n.intValue();
        }
    } catch (ParseException e) {
        bailOut("Failed to parse number of answer sets requested.", e);
    }
    boolean debugInternalChecks = commandLine.hasOption(OPT_DEBUG_INTERNAL_CHECKS);
    ParsedProgram program = null;
    try {
        // Parse all input files and accumulate their results in one ParsedProgram.
        String[] inputFileNames = commandLine.getOptionValues(OPT_INPUT);
        program = parseVisit(new ANTLRFileStream(inputFileNames[0]));
        for (int i = 1; i < inputFileNames.length; i++) {
            program.accumulate(parseVisit(new ANTLRFileStream(inputFileNames[i])));
        }
    } catch (RecognitionException e) {
        // In case a recognitionexception occured, parseVisit will
        // already have printed an error message, so we just exit
        // at this point without further logging.
        System.exit(1);
    } catch (FileNotFoundException e) {
        bailOut(e.getMessage());
    } catch (IOException e) {
        bailOut("Failed to parse program.", e);
    }
    // Apply program transformations/rewritings (currently none).
    IdentityProgramTransformation programTransformation = new IdentityProgramTransformation();
    ParsedProgram transformedProgram = programTransformation.transform(program);
    Grounder grounder = GrounderFactory.getInstance(commandLine.getOptionValue(OPT_GROUNDER, DEFAULT_GROUNDER), transformedProgram, filter);
    // NOTE: Using time as seed is fine as the internal heuristics
    // do not need to by cryptographically securely randomized.
    long seed = commandLine.hasOption(OPT_DETERMINISTIC) ? 0 : System.nanoTime();
    try {
        Number s = (Number) commandLine.getParsedOptionValue(OPT_SEED);
        if (s != null) {
            seed = s.longValue();
        }
    } catch (ParseException e) {
        bailOut("Failed to parse seed.", e);
    }
    LOGGER.info("Seed for pseudorandomization is {}.", seed);
    String chosenSolver = commandLine.getOptionValue(OPT_SOLVER, DEFAULT_SOLVER);
    String chosenBranchingHeuristic = commandLine.getOptionValue(OPT_BRANCHING_HEURISTIC, DEFAULT_BRANCHING_HEURISTIC);
    Heuristic parsedChosenBranchingHeuristic = null;
    try {
        parsedChosenBranchingHeuristic = Heuristic.get(chosenBranchingHeuristic);
    } catch (IllegalArgumentException e) {
        bailOut("Unknown branching heuristic: {}. Please try one of the following: {}.", chosenBranchingHeuristic, Heuristic.listAllowedValues());
    }
    Solver solver = SolverFactory.getInstance(chosenSolver, grounder, new Random(seed), parsedChosenBranchingHeuristic, debugInternalChecks);
    Stream<AnswerSet> stream = solver.stream();
    if (limit > 0) {
        stream = stream.limit(limit);
    }
    if (commandLine.hasOption(OPT_SORT)) {
        stream = stream.sorted();
    }
    stream.forEach(System.out::println);
}
Also used : org.antlr.v4.runtime(org.antlr.v4.runtime) java.util(java.util) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) Logger(org.slf4j.Logger) Solver(at.ac.tuwien.kr.alpha.solver.Solver) org.apache.commons.cli(org.apache.commons.cli) SolverFactory(at.ac.tuwien.kr.alpha.solver.SolverFactory) LoggerFactory(org.slf4j.LoggerFactory) PredictionMode(org.antlr.v4.runtime.atn.PredictionMode) AnswerSet(at.ac.tuwien.kr.alpha.common.AnswerSet) Grounder(at.ac.tuwien.kr.alpha.grounder.Grounder) IdentityProgramTransformation(at.ac.tuwien.kr.alpha.grounder.transformation.IdentityProgramTransformation) GrounderFactory(at.ac.tuwien.kr.alpha.grounder.GrounderFactory) ASPCore2Lexer(at.ac.tuwien.kr.alpha.antlr.ASPCore2Lexer) ASPCore2Parser(at.ac.tuwien.kr.alpha.antlr.ASPCore2Parser) Stream(java.util.stream.Stream) java.io(java.io) Heuristic(at.ac.tuwien.kr.alpha.solver.heuristics.BranchingHeuristicFactory.Heuristic) Predicate(at.ac.tuwien.kr.alpha.common.Predicate) ParsedTreeVisitor(at.ac.tuwien.kr.alpha.grounder.parser.ParsedTreeVisitor) ParsedProgram(at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram) Solver(at.ac.tuwien.kr.alpha.solver.Solver) ParsedProgram(at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram) AnswerSet(at.ac.tuwien.kr.alpha.common.AnswerSet) Predicate(at.ac.tuwien.kr.alpha.common.Predicate) IdentityProgramTransformation(at.ac.tuwien.kr.alpha.grounder.transformation.IdentityProgramTransformation) Grounder(at.ac.tuwien.kr.alpha.grounder.Grounder) Heuristic(at.ac.tuwien.kr.alpha.solver.heuristics.BranchingHeuristicFactory.Heuristic)

Example 3 with ANTLRFileStream

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

the class OmigaBenchmarksTest method test.

private void test(String folder, String aspFileName) throws IOException {
    ANTLRFileStream programInputStream = new ANTLRFileStream(Paths.get("benchmarks", "omiga", "omiga-testcases", folder, aspFileName).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
}
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 4 with ANTLRFileStream

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

the class BoaCompiler method main.

public static void main(final String[] args) throws IOException {
    CommandLine cl = processCommandLineOptions(args);
    if (cl == null)
        return;
    final ArrayList<File> inputFiles = BoaCompiler.inputFiles;
    // get the name of the generated class
    final String className = getGeneratedClass(cl);
    // get the filename of the jar we will be writing
    final String jarName;
    if (cl.hasOption('o'))
        jarName = cl.getOptionValue('o');
    else
        jarName = className + ".jar";
    // make the output directory
    File outputRoot = null;
    if (cl.hasOption("cd")) {
        outputRoot = new File(cl.getOptionValue("cd"));
    } else {
        outputRoot = new File(new File(System.getProperty("java.io.tmpdir")), UUID.randomUUID().toString());
    }
    final File outputSrcDir = new File(outputRoot, "boa");
    if (!outputSrcDir.mkdirs())
        throw new IOException("unable to mkdir " + outputSrcDir);
    // 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());
    final File outputFile = new File(outputSrcDir, className + ".java");
    final BufferedOutputStream o = new BufferedOutputStream(new FileOutputStream(outputFile));
    try {
        final List<String> jobnames = new ArrayList<String>();
        final List<String> jobs = new ArrayList<String>();
        final List<Integer> seeds = new ArrayList<Integer>();
        boolean isSimple = true;
        final List<Program> visitorPrograms = new ArrayList<Program>();
        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()));
                // use the whole input string to seed the RNG
                seeds.add(lexer._input.getText(new Interval(0, lexer._input.size())).hashCode());
                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);
                if (cl.hasOption("ast"))
                    new ASTPrintingVisitor().start(p);
                final String jobName = "" + i;
                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"));
                        isSimple &= !simpleVisitor.isComplex();
                        new InheritedAttributeTransformer().start(p);
                        new LocalAggregationTransformer().start(p);
                        // also let jobs have own methods if visitor merging is disabled
                        if (!simpleVisitor.isComplex() || maxVisitors < 2 || inputFiles.size() == 1) {
                            new VisitorOptimizingTransformer().start(p);
                            if (cl.hasOption("pp"))
                                new PrettyPrintVisitor().start(p);
                            if (cl.hasOption("ast2"))
                                new ASTPrintingVisitor().start(p);
                            final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(jobName);
                            cg.start(p);
                            jobs.add(cg.getCode());
                            jobnames.add(jobName);
                        } else // if a job has visitors, fuse them all together into a single program
                        {
                            p.getProgram().jobName = jobName;
                            visitorPrograms.add(p.getProgram());
                        }
                    }
                } 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() + ": compilation failed: ");
                e.printStackTrace();
            }
        }
        if (!visitorPrograms.isEmpty())
            try {
                for (final Program p : new VisitorMergingTransformer().mergePrograms(visitorPrograms, maxVisitors)) {
                    new VisitorOptimizingTransformer().start(p);
                    if (cl.hasOption("pp"))
                        new PrettyPrintVisitor().start(p);
                    if (cl.hasOption("ast2"))
                        new ASTPrintingVisitor().start(p);
                    final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(p.jobName);
                    cg.start(p);
                    jobs.add(cg.getCode());
                    jobnames.add(p.jobName);
                }
            } catch (final Exception e) {
                System.err.println("error fusing visitors - falling back: " + e);
                e.printStackTrace();
                for (final Program p : visitorPrograms) {
                    new VisitorOptimizingTransformer().start(p);
                    if (cl.hasOption("pp"))
                        new PrettyPrintVisitor().start(p);
                    if (cl.hasOption("ast2"))
                        new ASTPrintingVisitor().start(p);
                    final CodeGeneratingVisitor cg = new CodeGeneratingVisitor(p.jobName);
                    cg.start(p);
                    jobs.add(cg.getCode());
                    jobnames.add(p.jobName);
                }
            }
        if (jobs.size() == 0)
            throw new RuntimeException("no files compiled without error");
        final ST st = AbstractCodeGeneratingVisitor.stg.getInstanceOf("Program");
        st.add("name", className);
        st.add("numreducers", inputFiles.size());
        st.add("jobs", jobs);
        st.add("jobnames", jobnames);
        st.add("combineTables", CodeGeneratingVisitor.combineAggregatorStrings);
        st.add("reduceTables", CodeGeneratingVisitor.reduceAggregatorStrings);
        st.add("splitsize", isSimple ? 64 * 1024 * 1024 : 10 * 1024 * 1024);
        st.add("seeds", seeds);
        if (DefaultProperties.localDataPath != null) {
            st.add("isLocal", true);
        }
        o.write(st.render().getBytes());
    } finally {
        o.close();
    }
    compileGeneratedSrc(cl, jarName, outputRoot, outputFile);
}
Also used : BoaParser(boa.parser.BoaParser) Start(boa.compiler.ast.Start) ArrayList(java.util.ArrayList) URL(java.net.URL) VisitorOptimizingTransformer(boa.compiler.transforms.VisitorOptimizingTransformer) BoaErrorListener(boa.compiler.listeners.BoaErrorListener) TypeCheckingVisitor(boa.compiler.visitors.TypeCheckingVisitor) BufferedOutputStream(java.io.BufferedOutputStream) BoaLexer(boa.parser.BoaLexer) PrettyPrintVisitor(boa.compiler.visitors.PrettyPrintVisitor) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ST(org.stringtemplate.v4.ST) Program(boa.compiler.ast.Program) LexerErrorListener(boa.compiler.listeners.LexerErrorListener) BaseErrorListener(org.antlr.v4.runtime.BaseErrorListener) InheritedAttributeTransformer(boa.compiler.transforms.InheritedAttributeTransformer) VisitorMergingTransformer(boa.compiler.transforms.VisitorMergingTransformer) LocalAggregationTransformer(boa.compiler.transforms.LocalAggregationTransformer) IOException(java.io.IOException) 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) FileOutputStream(java.io.FileOutputStream) AbstractCodeGeneratingVisitor(boa.compiler.visitors.AbstractCodeGeneratingVisitor) CodeGeneratingVisitor(boa.compiler.visitors.CodeGeneratingVisitor) ASTPrintingVisitor(boa.compiler.visitors.ASTPrintingVisitor) File(java.io.File) RecognitionException(org.antlr.v4.runtime.RecognitionException) Interval(org.antlr.v4.runtime.misc.Interval)

Example 5 with ANTLRFileStream

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

the class BaseCppTest method writeParserTestFile.

protected void writeParserTestFile(String parserName, String lexerName, String listenerName, String visitorName, String parserStartRuleName, boolean debug, boolean trace) {
    if (!parserStartRuleName.endsWith(")"))
        parserStartRuleName += "()";
    ST outputFileST = new ST("#include \\<iostream>\n" + "\n" + "#include \"antlr4-runtime.h\"\n" + "#include \"<lexerName>.h\"\n" + "#include \"<parserName>.h\"\n" + "\n" + "using namespace antlr4;\n" + "\n" + "class TreeShapeListener : public tree::ParseTreeListener {\n" + "public:\n" + "  void visitTerminal(tree::TerminalNode *) override {}\n" + "  void visitErrorNode(tree::ErrorNode *) override {}\n" + "  void exitEveryRule(ParserRuleContext *) override {}\n" + "  void enterEveryRule(ParserRuleContext *ctx) override {\n" + "    for (auto child : ctx->children) {\n" + "      tree::ParseTree *parent = child->parent;\n" + "      ParserRuleContext *rule = dynamic_cast\\<ParserRuleContext *>(parent);\n" + "      if (rule != ctx) {\n" + "        throw \"Invalid parse tree shape detected.\";\n" + "      }\n" + "    }\n" + "  }\n" + "};\n" + "\n" + "\n" + "int main(int argc, const char* argv[]) {\n" + "  ANTLRFileStream input(argv[1]);\n" + "  <lexerName> lexer(&input);\n" + "  CommonTokenStream tokens(&lexer);\n" + "<createParser>" + "\n" + "  tree::ParseTree *tree = parser.<parserStartRuleName>;\n" + "  TreeShapeListener listener;\n" + "  tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);\n" + "\n" + "  return 0;\n" + "}\n");
    String stSource = "  <parserName> parser(&tokens);\n";
    if (debug) {
        stSource += "  DiagnosticErrorListener errorListener;\n";
        stSource += "  parser.addErrorListener(&errorListener);\n";
    }
    if (trace)
        stSource += "  parser.setTrace(true);\n";
    ST createParserST = new ST(stSource);
    outputFileST.add("createParser", createParserST);
    outputFileST.add("parserName", parserName);
    outputFileST.add("lexerName", lexerName);
    outputFileST.add("listenerName", listenerName);
    outputFileST.add("visitorName", visitorName);
    outputFileST.add("parserStartRuleName", parserStartRuleName);
    writeFile(tmpdir, "Test.cpp", outputFileST.render());
}
Also used : ST(org.stringtemplate.v4.ST) STGroupString(org.stringtemplate.v4.STGroupString) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)

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