예제 #1
0
 /**
  * Create comment AST from token. Depending on token type SINGLE_LINE_COMMENT or
  * BLOCK_COMMENT_BEGIN is created.
  *
  * @param token Token object.
  * @return DetailAST of comment node.
  */
 private static DetailAST createCommentAstFromToken(Token token) {
   if (token.getType() == TokenTypes.SINGLE_LINE_COMMENT) {
     return createSlCommentNode(token);
   } else {
     return createBlockCommentNode(token);
   }
 }
예제 #2
0
 public String getDocumentation() {
   String result = "";
   Token t = getHiddenBefore();
   if (t != null && t.getType() == DataScriptParserTokenTypes.DOC) {
     result = t.getText();
   }
   return result;
 }
예제 #3
0
파일: Main.java 프로젝트: yurius-r/decaf
  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;
              }
              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);
        parser.program();
      }

    } catch (Exception e) {
      // print the error:
      System.out.println(CLI.infile + " " + e);
    }
  }
예제 #4
0
 protected void showTokenStreamOf(String text) throws CharStreamException, TokenStreamException {
   Reader input = new StringReader(text);
   LexerImpl lexer = new LexerImpl(input);
   Token t = lexer.nextToken();
   while (t.getType() != Token.EOF_TYPE) {
     System.out.println(t.toString());
     t = lexer.nextToken();
   }
   System.out.println("<END OF STREAM>");
 }
예제 #5
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);
    }
  }
예제 #6
0
 protected boolean isEofToken(antlr.Token token) {
   return token.getType() == antlr.Token.EOF_TYPE;
 }
예제 #7
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 == Action.SCAN) {
        CScanner scanner = new CScanner(new DataInputStream(inputStream));
        scanner.setTrace(CLI.debug);
        Token token;
        boolean done = false;
        while (!done) {
          try {
            for (token = scanner.nextToken();
                token.getType() != CParserTokenTypes.EOF;
                token = scanner.nextToken()) {
              System.out.println(token.getType());
            }
            done = true;
          } catch (Exception e) {
            // print the error:
            System.out.println("ERROR: " + CLI.infile + " " + e);
            scanner.consume();
          }
        }
      } else if (CLI.target == Action.PARSE
          || CLI.target == Action.DOT
          || CLI.target == Action.DEFAULT) {
        CScanner scanner = new CScanner(new DataInputStream(inputStream));
        CParser parser = new CParser(scanner);
        parser.setTrace(CLI.debug);
        parser.program();
        if (parser.getError()) {
          System.out.println("ERROR");
          System.exit(-1);
        }
        if (CLI.target == Action.DOT) {
          System.out.println(TreeVisualizer.generateDOT(parser.getAST()));
        }
      } else if (CLI.target == Action.LOWIR) {
        CScanner scanner = new CScanner(new DataInputStream(inputStream));
        CParser parser = new CParser(scanner);
        parser.setTrace(CLI.debug);
        parser.program();
        if (parser.getError()) {
          System.out.println("ERROR");
          System.exit(-1);
        }
        IrProgram cfg = (IrProgram) IrGenerator.getIr(parser.getAST());

        SemanticChecker checker = new SemanticChecker();
        checker.check(cfg);
        SymbolTable table = checker.getSymbolTable();

        OutputGenerator gen = new OutputGenerator();
        gen.generate(cfg, table);
      } else {
        System.out.println("Unsupported target");
      }
    } catch (Exception e) {
      // print the error:
      System.out.println(CLI.infile + " " + e);
    }
  }