1:  package compiler;
   2:  //竟然没有对符号表检查大小,会溢出的。
   3:   
   4:  import java.io.IOException;
   5:   
   6:  public class SymbolTable {
   7:   
   8:      /**
   9:       * 当前名字表项指针(有效的符号表大小)table size
  10:       */
  11:      public int tablePtr = 0;
  12:      /**
  13:       * 符号表的大小
  14:       */
  15:      public static final int tableMax = 100;
  16:      public static final int symMax = 10;            //符号的最大长度
  17:      public static final int addrMax = 1000000;        //最大允许的数值
  18:      public static final int levMax = 3;            //最大允许过程嵌套声明层数[0,levmax]
  19:      public static final int numMax = 14;           //number的最大位数
  20:      public static boolean tableswitch;           //显示名字表与否
  21:      //名字表
  22:      public Item[] table = new Item[tableMax];
  23:   
  24:      public class Item {
  25:   
  26:          public static final int constant = 0;
  27:          public static final int variable = 1;
  28:          public static final int procedure = 2;
  29:          String name;                                             //名字
  30:          int type;                                               //类型,const var or procedur
  31:          int value;                                                 //数值,const使用
  32:          int lev;                                                 //所处层,var和procedur使用
  33:          int addr;                                                //地址,var和procedur使用
  34:          int size;                                               //需要分配的数据区空间,仅procedure使用
  35:   
  36:          public Item() {
  37:              super();
  38:              this.name = "";
  39:          }
  40:   
  41:      }
  42:   
  43:      /**
  44:       * 获得名字表某一项的内容
  45:       *
  46:       * @param i 名字表中的位置
  47:       * @return 名字表第i项的内容
  48:       */
  49:      public Item get(int i) {
  50:          if (table[i] == null) {
  51:              table[i] = new Item();
  52:          }
  53:          return table[i];
  54:      }
  55:   
  56:      /**
  57:       * 把某个符号登录到名字表中 名字表从1开始填,0表示不存在该项符号
  58:       *
  59:       * @param sym 要登记到名字表的符号
  60:       * @param k 该符号的类型:const,var,procedure
  61:       * @param lev 名字所在的层次
  62:       * @param dx 当前应分配的变量的相对地址,注意调用enter()后dx要加一
  63:       */
  64:      public void enter(Symbol sym, int type, int lev, int dx) {
  65:          tablePtr++;
  66:          Item item = get(tablePtr);
  67:          item.name = sym.id;
  68:          item.type = type;
  69:          switch (type) {
  70:              case Item.constant:                                     //常量名字
  71:                  item.value = sym.num;                               //记录下常数值的大小
  72:                  break;
  73:              case Item.variable:                                      //变量名字
  74:                  item.lev = lev;                                          //变量所在的层
  75:                  item.addr = dx;                                            //变量的偏移地址
  76:                  break;
  77:              case Item.procedure:                                    //过程名字
  78:                  item.lev = lev;
  79:   
  80:          }
  81:      }
  82:   
  83:      /**
  84:       * 在名字表中查找某个名字的位置 查找符号表是从后往前查, 这样符合嵌套分程序名字定义和作用域的规定
  85:       *
  86:       * @param idt 要查找的名字
  87:       * @return 如果找到则返回名字项的下标,否则返回0
  88:       */
  89:      public int position(String idt) {
  90:          for (int i = tablePtr; i > 0; i--) //必须从后往前找
  91:          {
  92:              if (get(i).name.equals(idt)) {
  93:                  return i;
  94:              }
  95:          }
  96:          return 0;
  97:      }
  98:   
  99:      /**
 100:       * 输出符号表内容,摘自block()函数
 101:       *
 102:       * @param start 当前符号表区间的左端
 103:       */
 104:      void debugTable(int start) {
 105:          if (tableswitch) //显示名字表与否
 106:          {
 107:              return;
 108:          }
 109:          System.out.println("**** Symbol Table ****");
 110:          if (start > tablePtr) {
 111:              System.out.println("  NULL");
 112:          }
 113:          for (int i = start + 1; i <= tablePtr; i++) {
 114:              try {
 115:                  String msg = "unknown table item !";
 116:                  switch (table[i].type) {
 117:                      case Item.constant:
 118:                          msg = "   " + i + "  const: " + table[i].name + "  val: " + table[i].value;
 119:                          break;
 120:                      case Item.variable:
 121:                          msg = "    " + i + "  var: " + table[i].name + "  lev: " + table[i].lev + "  addr: " + table[i].addr;
 122:                          break;
 123:                      case Item.procedure:
 124:                          msg = "    " + i + " proc: " + table[i].name + "  lev: " + table[i].lev + "  addr: " + table[i].size;
 125:                          break;
 126:                  }
 127:                  System.out.println(msg);
 128:                  PL0.tableWriter.write(msg + '\n');
 129:              } catch (IOException ex) {
 130:                  ex.printStackTrace();
 131:                  System.out.println("***write table intfo meet with error***");
 132:              }
 133:          }
 134:      }
 135:  }

.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; }

PL/0编译器(java version) – SymbolTable.java的更多相关文章

  1. PL/0编译器(java version) - MainFrame.java

    1: /* 2: * To change this license header, choose License Headers in Project Properties. 3: * To chan ...

  2. PL/0编译器实践---后记

    花了几天时间,把清华版的<编译原理>一书中的PL/0编译器实践了一遍.颇有收获,记录如下: 理解代码的技巧,如何理解一份代码,比如这个程序,其逻辑相对于一般程序就比较复杂了,如何翻译,虚拟 ...

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

    1: package compiler; 2:   3: import java.io.IOException; 4: import java.util.BitSet; 5:   6: /** 7: ...

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

    1: package compiler; 2:   3: import java.io.BufferedWriter; 4: import java.io.FileWriter; 5:   6: /* ...

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

    1: package compiler; 2:   3: import java.io.BufferedReader; 4: import java.io.FileNotFoundException; ...

  6. PL/0编译器(java version) - Interpreter.java

    1: package compiler; 2:   3: import java.io.BufferedReader; 4: import java.io.BufferedWriter; 5: imp ...

  7. PL/0编译器(java version) - Err.java

    1: package compiler; 2:   3: import java.io.BufferedWriter; 4:   5: public class Err { 6:   7: publi ...

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

    1: package compiler; 2:   3: /** 4: * 采用全局变量sym来存储符号码,并用全局变量id和num来传递语义值 5: * 6: * @author jiangnan ...

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

    1: package compiler; 2:   3: /** 4: * //虚拟机指令 5: * 6: * @author jiangnan 7: * 8: */ 9: public class ...

随机推荐

  1. .NET CLR 运行原理

    原文: Drill Into .NET Framework Internals to See How the CLR Creates Runtime Objects 文章讨论了: SystemDoma ...

  2. 中晟银泰国际中心酒店式公寓介绍 业主交流QQ群:319843248

    行政区域:中原区 区域板块:西北板块 项目位置:中原路与华山路东北角(中原万达北侧中原西路对面) 建筑类型:高层 物业类别:酒店式公寓 户型面积:公寓35-100平米 开发商:中晟集团 投资商:中晟集 ...

  3. [BZOJ3142][HNOI2013]数列(组合)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3142 分析: 考虑差值序列a1,a2,...,ak-1 那么对于一个确定的差值序列,对 ...

  4. MVC认知路【点点滴滴支离破碎】【四】----捆绑和缩小(BundleConfig.RegisterBundles)

    原文链接:http://www.asp.net/mvc/overview/performance/bundling-and-minification 打开App_Start\BundleConfig. ...

  5. Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo

    更新了xcode后使用goland运行项目时提示 Agreeing to the Xcode/iOS license requires admin privileges, please re-run ...

  6. iOS-- 快速集成iOS基于RTMP的视频推流

    效果图 iTools有点卡, 但是推到服务器倒是很快的. 推流 前言 这篇blog是iOS视频直播初窥:<喵播APP>的一个补充. 因为之前传到github上的项目中没有集成视频的推流.有 ...

  7. MyEclipse10中自动生成Hibernate的实体和xml配置文件

    前提:1.在项目中添加Hibernate支持 2.MyEclipse中已经创建好数据库连接 3.表已经建好并且有主键 步骤如下: 1.在DB Browser窗口的已打开连接节点中选中用户创建的所有的表 ...

  8. SpringMVC学习--springmvc原理

    简介 springmvc是spring框架的一个模块,springmvc和spring无需通过中间整合层进行整合.springmvc是一个基于mvc的web框架. spring的结构图: mvc在b/ ...

  9. JS实时定位

    <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  10. struts2 spring3.2 hibernate4.1 框架搭建 整合

    ssh是企业开发中常遇到的框架组合,现将框架的搭建过程记录下来,以便以后查看.我的搭建过程是,首先struts,然后spring,最后hibernate.struts2的最新版本为2.3.8,我下载的 ...