示例#1
0
  public static void main(String args[]) throws Exception {
    Yylex l = null;
    parser p;
    Interpreter I;
    try {
      if (args.length == 0) l = new Yylex(System.in);
      else l = new Yylex(new FileReader(args[0]));
    } catch (FileNotFoundException e) {
      System.err.println("Error: File not found: " + args[0]);
      System.exit(1);
    }
    p = new parser(l);
    /* The default parser is the first-defined entry point. */
    /* You may want to change this. Other options are: */
    /*  */
    try {

      calc.Absyn.Exp parse_tree = p.pExp();
      I = new Interpreter();
      // I.eval(parse_tree) ;
      System.out.println(I.eval(parse_tree));
      /*System.out.println();
      System.out.println("Parse Succesful!");
      System.out.println();
      System.out.println("[Abstract Syntax]");
      System.out.println();
      System.out.println(PrettyPrinter.show(parse_tree));
      System.out.println();
      System.out.println("[Linearized Tree]");
      System.out.println();
      System.out.println(PrettyPrinter.print(parse_tree));
      */
    } catch (Throwable e) {
      System.err.println(
          "At line " + String.valueOf(l.line_num()) + ", near \"" + l.buff() + "\" :");
      System.err.println("     " + e.getMessage());
      System.exit(1);
    }
  }
示例#2
0
文件: P6.java 项目: Xodarap/Academic
  public static void main(String[] args) throws IOException // may be thrown by the scanner
      {
    // check for command-line args
    if (args.length != 2) {
      System.err.println(
          "please supply name of file to be parsed and name of file for unparsed version.");
      System.exit(-1);
    }

    try {
      Codegen.p = new PrintWriter("/home/ben/Scripts/github/Academic/cs 536/p6/gencode.asm");
    } catch (FileNotFoundException e) {
      System.err.println("File " + args[0] + " not found.");
      System.exit(-1);
    }

    // open input file
    FileReader inFile = null;
    try {
      inFile = new FileReader(args[0]);
    } catch (FileNotFoundException ex) {
      System.err.println("File " + args[0] + " not found.");
      System.exit(-1);
    }

    // open output file
    PrintWriter outFile = null;
    try {
      outFile = new PrintWriter(args[1]);
    } catch (FileNotFoundException ex) {
      System.err.println("File " + args[1] + " could not be opened for writing.");
      System.exit(-1);
    }

    parser P = new parser(new Yylex(inFile));

    Symbol root = null; // the parser will return a Symbol whose value
    // field is the translation of the root
    // nonterminal (i.e., of the nonterminal
    // "program")

    try {
      root = P.parse(); // do the parse
      System.out.println("Little program parsed correctly.");
    } catch (Exception ex) {
      System.err.println("Exception occured during parse: " + ex);
      ex.printStackTrace();
      System.exit(-1);
    }
    try {
      //		((SymTabNode)root.value).provideSymTab(new SymTab());
      ((ProgramNode) root.value).processNames();
    } catch (Exception ex) {
      System.err.println("Exception occured during parse: " + ex);
      ex.printStackTrace();
      System.exit(-1);
    }
    if (!Errors.errors) {
      ((ASTnode) root.value).unparse(outFile, 0);
      try {
        ((ProgramNode) root.value).typeCheck();
      } catch (Exception ex) {
        System.err.println("Exception occured during typecheck: " + ex);
        ex.printStackTrace();
        System.exit(-1);
      }
    }

    if (!Errors.errors) {
      try {
        ((ProgramNode) root.value).generate();
        Codegen.p.close();
      } catch (Exception ex) {
        System.err.println("Exception occured during codegen: " + ex);
        ex.printStackTrace();
        System.exit(-1);
      }
    }
    outFile.close();

    return;
  }