Beispiel #1
0
  public static void main(String[] args) {
    try {
      CLI.parse(args, new String[0]);

      InputStream inputStream =
          args.length == 0 ? System.in : new java.io.FileInputStream(CLI.infile);

      if (CLI.target == CLI.SCAN) {
        DecafScanner lexer = new DecafScanner(new DataInputStream(inputStream));
        Token token;
        boolean done = false;
        while (!done) {
          try {
            for (token = lexer.nextToken();
                token.getType() != DecafParserTokenTypes.EOF;
                token = lexer.nextToken()) {
              String type = "";
              String text = token.getText();

              switch (token.getType()) {
                case DecafScannerTokenTypes.ID:
                  type = " IDENTIFIER";
                  break;
                case DecafScannerTokenTypes.CHAR:
                  type = " CHARLITERAL";
                  break;
                case DecafScannerTokenTypes.TRUE:
                case DecafScannerTokenTypes.FALSE:
                  type = " BOOLEANLITERAL";
                  break;
                case DecafScannerTokenTypes.HEX:
                case DecafScannerTokenTypes.DECIMAL:
                  type = " INTLITERAL";
                  break;
                case DecafScannerTokenTypes.STRING:
                  type = " STRINGLITERAL";
                  break;
              }
              System.out.println(token.getLine() + type + " " + text);
            }
            done = true;
          } catch (Exception e) {
            // print the error:
            System.out.println(CLI.infile + " " + e);
            lexer.consume();
          }
        }
      } else if (CLI.target == CLI.PARSE || CLI.target == CLI.DEFAULT) {
        DecafScanner lexer = new DecafScanner(new DataInputStream(inputStream));
        DecafParser parser = new DecafParser(lexer, CLI.debug);
        // DecafParser parser = new DecafParser (lexer);
        parser.program();
      } else if (CLI.target == CLI.INTER) {
        DecafScanner lexer = new DecafScanner(new DataInputStream(inputStream));
        DecafParser parser = new DecafParser(lexer, CLI.debug);
        parser.program();
        IrNode irRoot = parser.getIrTree();
        SemanticChecker checker = new SemanticChecker(CLI.infile, CLI.debug);
        if (CLI.debug) System.out.println("--- checking -----");
        checker.checkProgram((IrClassDecl) irRoot);
      } else if (CLI.target == CLI.LOWIR) {
        DecafScanner lexer = new DecafScanner(new DataInputStream(inputStream));
        DecafParser parser = new DecafParser(lexer, CLI.debug);
        parser.program();
        IrNode irRoot = parser.getIrTree();
        SemanticChecker checker = new SemanticChecker(CLI.infile, CLI.debug);
        if (CLI.debug) System.out.println("--- checking -----");
        checker.checkProgram((IrClassDecl) irRoot);
        CodeGen codegen = new CodeGen(irRoot, CLI.debug);
        codegen.genLowIr();
        codegen.printLowIr();
      }
    } catch (Exception e) {
      // print the error:
      System.out.println(CLI.infile + " " + e);
    }
  }
  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!");
    }
  }