예제 #1
0
  public static void compile(String filename, String parameters) throws Exception {

    String source = "";
    String s = "";

    int last = filename.lastIndexOf(".cmtjava");
    if (last == -1) { // arquivos devem ter extensão cmtjava
      System.out.println("Usage: java -jar cmtjavac.jar <options> <source file>");
      System.exit(0);
    } else source = filename.substring(0, last);

    last = source.lastIndexOf('/');
    if (last != -1) source = source.substring(last + 1, source.length());

    System.out.println("Translating " + source + ".cmtjava");

    String environmentVariable = System.getenv("CMTJAVAC");
    String pathForTemplate = environmentVariable + "/CMTJava.stg";

    // load in CMTJava.stg template group, put in templates variable
    FileReader groupFileR = null;
    try {
      groupFileR = new FileReader(pathForTemplate);
    } catch (Exception e) {
      System.out.println(
          "Could not load the template file for translating the source file.\nPlease set the CMTJAVAC environment variable with the path of the template file");
      System.exit(0);
    }
    StringTemplateGroup templates = new StringTemplateGroup(groupFileR);
    groupFileR.close();

    File f = new File(filename);
    FileInputStream fis = new FileInputStream(f);
    ANTLRInputStream input = new ANTLRInputStream(fis);

    CMTJavaLexer lexer = new CMTJavaLexer(input);
    TokenRewriteStream tokens = new TokenRewriteStream(lexer);
    CMTJavaParser parser = new CMTJavaParser(tokens);
    parser.setTemplateLib(templates);

    /*CMTJavaParser.compilationUnit_return r = */ parser
        .compilationUnit(); // parse rule compilationUnit
    // StringTemplate output = (StringTemplate)r.getTemplate();

    try {
      BufferedWriter out = new BufferedWriter(new FileWriter(source + ".java"));
      out.write(tokens.toString());
      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

    Process t = null;
    try {
      System.out.println("javac " + parameters + " " + source + ".java");
      t = Runtime.getRuntime().exec("javac " + parameters + " " + source + ".java");
    } catch (Exception e) {
      System.out.println("Could not load the javac compiler.");
      System.exit(0);
    }
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(t.getInputStream()));
    BufferedReader stdError = new BufferedReader(new InputStreamReader(t.getErrorStream()));

    // read the output from the command
    System.out.println("Compiling " + source + ".java");
    while ((s = stdInput.readLine()) != null) {
      System.out.println("éoq");
      System.out.println(s);
    }

    // read any errors from the attempted command
    while ((s = stdError.readLine()) != null) {
      System.out.println(s);
    }
  }
예제 #2
0
  public static void main(String[] args) throws Exception {

    try {
      CatalogReader foo = new CatalogReader("Catalog.xml");
      Map<String, TableData> catalog = foo.getCatalog();
      // System.out.println (CatalogReader.printCatalog (res));

      InputStreamReader converter = new InputStreamReader(System.in);
      BufferedReader in = new BufferedReader(converter);

      System.out.format("\nSQL>");
      String soFar = in.readLine() + " ";

      // loop forever, or until someone asks to quit
      while (true) {

        // keep on reading from standard in until we hit a ";"
        while (soFar.indexOf(';') == -1) soFar += (in.readLine() + " ");

        // split the string
        String toParse = soFar.substring(0, soFar.indexOf(';') + 1);
        soFar = soFar.substring(soFar.indexOf(';') + 1, soFar.length());
        toParse = toParse.toLowerCase();

        // parse it
        ANTLRStringStream parserIn = new ANTLRStringStream(toParse);
        SQLLexer lexer = new SQLLexer(parserIn);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        SQLParser parser = new SQLParser(tokens);

        // if we got a quit
        if (parser.parse() == false) break;

        // print the results
        System.out.println("RESULT OF PARSING");
        System.out.println("Expressions in SELECT:");
        ArrayList<Expression> mySelect = parser.getSELECT();
        for (Expression e : mySelect) System.out.println("\t" + e);

        System.out.println("Tables in FROM:");
        Map<String, String> myFrom = parser.getFROM();
        System.out.println("\t" + myFrom);

        System.out.println("WHERE clause:");
        Expression myWhere = parser.getWHERE();
        if (myWhere != null) System.out.println("\t" + myWhere);

        System.out.println("GROUPING att:");
        String myGroup = parser.getGROUPBY();
        if (myGroup != null) System.out.println("\t" + myGroup);

        SemanticChecker sc = new SemanticChecker(mySelect, myFrom, myWhere, myGroup, catalog);
        if (sc.execute() == true)
          new Optimizer(mySelect, myFrom, myWhere, myGroup, catalog).execute();
        else System.err.println("Semantic checking failed");

        System.out.format("\nSQL>");
      }
    } catch (Exception e) {
      System.err.println("Interpreter.main.73: error!");
    }
  }