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. 用node.js实现简单的web服务器

    node.js实现web服务器还是比较简单的,我了解node.js是从<node入门>开始的,如果你不了解node.js也可以看看! 我根据那书一步一步的练习完了,也的确大概了解了node ...

  2. RabbitHub开源情况及计划

    之前写过一篇".NET 平台下的插件化开发内核(Rabbit Kernel)",已经过去三个月了,期间RabbitHub并不是没有了发展更不是放弃了发展,在RabbitHub中的群 ...

  3. Python时间性能测量

    主要有以下三种方式: 一,CPU时间 time.clock() 测量CPU时间,比较精准,通过比较程序运行前后的CPU时间差,得出程序运行的CPU时间. 二, 时钟时间 time.time() 测量时 ...

  4. codevs 4543 treap 模板

    type rec=record lc,rc,v,rnd,size,w,fa:longint; end; var n,root,tot,ans,opt,x,i,po:longint; tr:array[ ...

  5. Oracle 死锁的检测查询及处理

    来源于: http://www.cnblogs.com/hoojo/archive/2012/08/31/2665583.html -- 死锁查询语句 SELECT bs.username " ...

  6. iOS开发小技巧--实现毛玻璃效果的方法

    一.美工出图 二.第三方框架 -- DRNRealTimeBlur,框架继承自UIView.使用方法:创建UIView直接继承自框架的View,就有了毛玻璃效果 三.CoreImage -- 图片加高 ...

  7. 【BZOJ 4515】【SDOI 2016 Round1 Day1 T3】游戏

    考场上写了lct,可惜当时对标记永久化的理解并不是十分深刻,导致调一个错误的程序调了4h+,最后这道题爆0了QwQ 现在写了树链剖分,用标记永久化的线段树维护轻重链,对于$s\rightarrow l ...

  8. 强连通 HDU 1827

    n个点m条边 n个权lcy 要叫这个人的花费 m条边 缩点后 新的图中 入度为0的点要通知  通知强连通分量中权值最小的 #include<stdio.h> #include<alg ...

  9. java 读取数据库中表定义

    将数据库中的表信息读取出来 package com.cloud.smartreport.utils; import java.sql.Connection; import java.sql.Datab ...

  10. js-JavaScript高级程序设计学习笔记14

    第十六章 HTML5脚本编程 1.跨文档消息传递.简称XDM,指的是来自不同域的页面间传递消息. XDM的核心是postMessage()方法,接收两个参数,一条消息和消息接收方来自哪个域的字符串. ...