PL/0编译器(java version)–PL0.java

时间:2022-07-29 19:27:59
   1:  package compiler;
   2:   
   3:  import java.io.BufferedWriter;
   4:  import java.io.FileWriter;
   5:   
   6:  /**
   7:   * 组织输入输出接口
   8:   *
   9:   * @author jiangnan
  10:   */
  11:  public class PL0 {
  12:   
  13:      
  14:      public static final String pcodeFile = "d:\\pcode.txt";
  15:      public static final String tableFile = "d:\\table.txt";
  16:      public static final String runtimeFile = "d:\\runtime.txt";
  17:      public static final String errFile = "d:\\error.txt";
  18:      public static final String inputFile="d:\\input.txt";
  19:      public static BufferedWriter pcodeWriter;                 //输出虚拟机代码
  20:      public static BufferedWriter runtimeWriter;               //输出结果
  21:      public static BufferedWriter tableWriter;                //输出名字表
  22:      public static BufferedWriter errWriter;                   //输出错误信息
  23:   
  24:      public Praser praser;
  25:   
  26:      public PL0(String filepath) {
  27:          Scanner scan = new Scanner(filepath);
  28:          praser = new Praser(scan,//词法分析器
  29:                  new SymbolTable(),//名字表
  30:                  new Interpreter());
  31:      }
  32:   
  33:      public boolean compile() {
  34:          try {
  35:              pcodeWriter = new BufferedWriter(new FileWriter(pcodeFile));
  36:              tableWriter = new BufferedWriter(new FileWriter(tableFile));
  37:              runtimeWriter = new BufferedWriter(new FileWriter(runtimeFile));
  38:              errWriter = new BufferedWriter(new FileWriter(errFile));
  39:              praser.nextsym();                                    //前瞻分析需要预先读入一个符号
  40:              praser.parse();                                        //开始语法分析过程(连同语法检查,目标代码生成)
  41:              pcodeWriter.close();
  42:              tableWriter.close();
  43:              runtimeWriter.close();
  44:              errWriter.close();
  45:          } catch (Exception e) {
  46:              e.printStackTrace();
  47:              System.out.println("***compile error***");
  48:          } finally {
  49:   
  50:          }
  51:          //编译成功是指完成编译过程并且没有错误
  52:          return (praser.myErr.errCount == 0);
  53:      }
  54:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }