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;
}
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());
}
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");
}
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();
}
}
}
Aggregations