/**
   * Converts an int token code into the name of the token by reflection on the cup symbol
   * class/interface sym
   *
   * <p>This code was contributed by Karl Meissner <*****@*****.**>
   */
  private String getTokenName(int token) {
    try {
      java.lang.reflect.Field[] classFields = GFinalCoordSym.class.getFields();
      for (int i = 0; i < classFields.length; i++) {
        if (classFields[i].getInt(null) == token) {
          return classFields[i].getName();
        }
      }
    } catch (Exception e) {
      e.printStackTrace(System.err);
    }

    return "UNKNOWN TOKEN";
  }
 /**
  * Runs the scanner on input files.
  *
  * <p>This is a standalone scanner, it will print any unmatched text to System.out unchanged.
  *
  * @param argv the command line, contains the filenames to run the scanner on.
  */
 public static void main(String argv[]) {
   if (argv.length == 0) {
     System.out.println("Usage : java GFinalCoordLexer <inputfile>");
   } else {
     for (int i = 0; i < argv.length; i++) {
       GFinalCoordLexer scanner = null;
       try {
         scanner = new GFinalCoordLexer(new java.io.FileReader(argv[i]));
         while (!scanner.zzAtEOF) scanner.next_token();
       } catch (java.io.FileNotFoundException e) {
         System.out.println("File not found : \"" + argv[i] + "\"");
       } catch (java.io.IOException e) {
         System.out.println("IO error scanning file \"" + argv[i] + "\"");
         System.out.println(e);
       } catch (Exception e) {
         System.out.println("Unexpected exception:");
         e.printStackTrace();
       }
     }
   }
 }
示例#3
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;
  }