Exemplo n.º 1
0
  @Test
  public void testSimpleC() throws IOException {
    final Compiler compiler = new Compiler();
    String code =
        "#include airport.h;"
            + "\n"
            + "#define MAX;"
            + "\n"
            + "typedef struct\n"
            + "{\n"
            + "\tint id ;\n"
            + "\tint tm ;\n"
            + "} plane;\n"
            + "\n";
    final ParseTree ast = compiler.parse(code, false);

    Assert.assertNotNull(ast);

    final TreePrinterListener treePrinterListener = new TreePrinterListener(compiler.getParser());
    ParseTreeWalker.DEFAULT.walk(treePrinterListener, ast);
    final String formatted = treePrinterListener.toString();
    System.out.println(formatted);

    final String reconstructedFileWithoutSpaces = treePrinterListener.getLeafsAsText();
    code = getStringWithoutSpaces(code);
    Assert.assertArrayEquals(reconstructedFileWithoutSpaces.getBytes(), code.getBytes());
  }
Exemplo n.º 2
0
 @NotNull
 private OutputModelObject buildModuleOutputModel() {
   ModelBuilder builder = new ModelBuilder(this, compiler.symbolTable);
   ParseTree root = module.getRoot();
   ParseTreeWalker.DEFAULT.walk(builder, root);
   return builder.built.get(root);
 }
Exemplo n.º 3
0
 public void loadProgramFromString(String progStr) {
   IntlyLexer lexer = new IntlyLexer(new ANTLRInputStream(progStr));
   IntlyParser parser = new IntlyParser(new CommonTokenStream(lexer));
   parser.setBuildParseTree(true);
   ParseTree tree = parser.program();
   ProgramBuilder builder = new ProgramBuilder();
   ParseTreeWalker.DEFAULT.walk(builder, tree);
   program = builder.getProg();
 }
Exemplo n.º 4
0
 public void loadProgramFromFile(String filename) throws IOException {
   IntlyLexer lexer = new IntlyLexer(new ANTLRFileStream(filename));
   IntlyParser parser = new IntlyParser(new CommonTokenStream(lexer));
   parser.setBuildParseTree(true);
   ParseTree tree = parser.program();
   ProgramBuilder builder = new ProgramBuilder();
   ParseTreeWalker.DEFAULT.walk(builder, tree);
   program = builder.getProg();
 }
  @Test
  public void test() {

    // Retrieve all files from `src/test/resources` ending with ".sql".
    File[] tests =
        new File("src/test/resources")
            .listFiles(
                new FileFilter() {
                  public boolean accept(File file) {
                    return file.isFile() && file.getName().endsWith(".sql");
                  }
                });

    int testCounter = 0;

    for (File test : tests) {

      testCounter++;

      try {
        SQLiteLexer lexer = new SQLiteLexer(new ANTLRFileStream(test.getAbsolutePath()));
        SQLiteParser parser = new SQLiteParser(new CommonTokenStream(lexer));
        ParseTree tree = parser.parse();

        // Attach a listener that counts the number of SQL statements.
        ParseTreeWalker.DEFAULT.walk(
            new SQLiteBaseListener() {
              @Override
              public void enterSql_stmt(@NotNull SQLiteParser.Sql_stmtContext ctx) {
                totalStatements++;
              }
            },
            tree);

        if (testCounter % 100 == 0) {
          System.out.println(testCounter + "/" + tests.length);
        }
      } catch (Exception e) {
        e.printStackTrace();
        System.out.println("could not parse file: " + test);
        return;
      }
    }

    System.out.println(
        "finished parsing "
            + tests.length
            + " test files containing "
            + totalStatements
            + " SQL statements");
  }
Exemplo n.º 6
0
  /** @param args */
  public static void main(String[] args) {
    // INFO: if you want to use the asp module as executable jar, uncomment the following comment
    handleCommandLine(args);

    ASPConverter converter = new ASPConverter();
    String src = converter.convertFromFileToString(fileToDebug);

    // do the parsing
    ANTLRInputStream input = null;
    input = new ANTLRInputStream(src);
    ASPProgramLexer lexer = new ASPProgramLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ASPProgramParser parser = new ASPProgramParser(tokens);
    ParseTree tree = parser.prog();
    ProgramListener pl = new ProgramListener();
    ParseTreeWalker.DEFAULT.walk(pl, tree);

    //       	parser.setBuildParseTree(true);
    //       	ParserRuleContext tree = parser.prog();
    //       	tree.inspect(parser);

    // fill the asp model with rules
    ASPModel model = ASPModel.getASPModelInstance();
    for (IProgramElement r : model.getRules()) {
      model.addProgramElement(r);
    }

    //        ReasonerASP reasoner = new ReasonerASP();
    //        boolean consistent = reasoner.isConsistent();
    //        System.out.println("is consistent: " + consistent);

    ASPTheory theory = new ASPTheory();
    ReasonerASP reasoner = new ReasonerASP();
    theory.setReasoner(reasoner);
    KnowledgeBase<IProgramElement> knowledgeBase = new KnowledgeBase<IProgramElement>();
    knowledgeBase.setBackgroundFormulas(model.getFacts());
    theory.setKnowledgeBase(knowledgeBase);
    theory.getKnowledgeBase().addFormulas(model.getProgramElements());

    HsTreeSearch<FormulaSet<IProgramElement>, IProgramElement> search =
        new HsTreeSearch<FormulaSet<IProgramElement>, IProgramElement>();

    // we want to use UniformCostSearch as our start strategy
    search.setSearchStrategy(new BreadthFirstSearchStrategy<IProgramElement>());

    // because we use Reiter's Tree nodes are conflicts which we start using QuickXplain
    search.setSearcher(new QuickXplain<IProgramElement>());

    // because we use UniformCostSearch we have to give a cost estimator to the start
    search.setCostsEstimator(new SimpleCostsEstimator<IProgramElement>());

    // at last we combine theory with start and get our ready to use object
    search.setSearchable(theory);
    try {
      search.start();
    } catch (SolverException e1) {
      e1.printStackTrace();
    } catch (NoConflictException e1) {
      e1.printStackTrace();
    } catch (InconsistentTheoryException e1) {
      e1.printStackTrace();
    }

    Set<FormulaSet<IProgramElement>> conflicts = search.getConflicts();
    Set<FormulaSet<IProgramElement>> diagnosis = search.getDiagnoses();
    int i = 0;
    int j = 0;
    for (FormulaSet<IProgramElement> fs : conflicts) {
      System.out.println("\nConflicts " + i + ":");
      for (IProgramElement pe : fs) {
        System.out.println(pe.getString());
      }
      i++;
    }
    for (FormulaSet<IProgramElement> fs : diagnosis) {
      System.out.println("\nDiagnosis " + j + ":");
      for (IProgramElement pe : fs) {
        System.out.println(pe.getString());
      }
      j++;
    }
  }