1:  package compiler;
   2:   
   3:  import java.io.BufferedReader;
   4:  import java.io.FileNotFoundException;
   5:  import java.io.FileReader;
   6:  import java.util.Arrays;
   7:   
   8:  public class Scanner {
   9:   
  10:      public int lineCnt=0;
  11:      private char curCh = ' ';
  12:      private String line;
  13:      public int lineLength = 0;
  14:      public int chCount = 0;
  15:      private int[] ssym;
  16:      private BufferedReader in;
  17:   
  18:      public Scanner(String filePath) {
  19:          try {
  20:              in = new BufferedReader(new FileReader(filePath));
  21:          } catch (FileNotFoundException ex) {
  22:              ex.printStackTrace();
  23:              System.out.println("***File not exist!***");
  24:          }
  25:          //设置单字符
  26:          ssym = new int[256];
  27:          Arrays.fill(ssym, Symbol.nul);
  28:          ssym['+'] = Symbol.plus;
  29:          ssym['-'] = Symbol.minus;
  30:          ssym['*'] = Symbol.mul;
  31:          ssym['/'] = Symbol.div;
  32:          ssym['('] = Symbol.lparen;
  33:          ssym[')'] = Symbol.rparen;
  34:          ssym['='] = Symbol.eql;
  35:          ssym[','] = Symbol.comma;
  36:          ssym['.'] = Symbol.peroid;
  37:          ssym[';'] = Symbol.semicolon;
  38:   
  39:      }
  40:   
  41:      //读取一个字符,为减少磁盘I/O次数,每次读取一行
  42:      void getch() {
  43:          if (chCount == lineLength) {
  44:              try {//如果读到行末尾,就重新读入一行
  45:                  String tmp="";
  46:                  while (tmp.equals("")) {
  47:                      tmp=in.readLine().trim()+' ';               //除去空行
  48:                  }
  49:                  line=tmp;
  50:                  lineCnt++;
  51:              } catch (Exception e) {
  52:                  // throw new Error("***program imcomplete!***");
  53:                  e.printStackTrace();
  54:                  System.out.println("***reading character meet with error!***");
  55:              }
  56:              lineLength = line.length();
  57:              chCount = 0;
  58:              System.out.println(line);
  59:          }
  60:          curCh = line.charAt(chCount++);
  61:      }
  62:   
  63:      //词法分析,获取一个词法分析符号,是词法分析器的重点
  64:      public Symbol getsym() {
  65:          Symbol sym;
  66:          while (curCh == ' ') {
  67:              getch();
  68:          }
  69:          if ((curCh >= 'a' && curCh <= 'z')||(curCh >= 'A' && curCh <= 'Z')) {
  70:              sym = matchKeywordOrIdentifier();                                     //关键字或者一般标识符
  71:          } else if (curCh >= '0' && curCh <= '9') {
  72:              sym = matchNumber();                                                       //数字
  73:          } else {
  74:              sym = matchOperator();                                                     //操作符
  75:          }
  76:          return sym;
  77:      }
  78:   
  79:      private Symbol matchKeywordOrIdentifier() {
  80:          StringBuffer sb = new StringBuffer();
  81:          do{
  82:              sb.append(curCh);
  83:              getch();
  84:          }while((curCh >= 'a' && curCh <= 'z')||(curCh>='A'&&curCh<='Z') || (curCh >= '0' && curCh <= '9'));
  85:   
  86:          String token = sb.toString();
  87:          int index = Arrays.binarySearch(Symbol.word, token);                           //搜索是不是保留字
  88:          Symbol sym = null;
  89:          if (index < 0) {
  90:              sym = new Symbol(Symbol.ident);                                            //一般标识符
  91:              sym.id = token;
  92:          } else {
  93:              sym = new Symbol(Symbol.wsym[index]);                                    //保留字对应的符号值0-31
  94:          }
  95:          return sym;
  96:      }
  97:   
  98:      private Symbol matchNumber() {
  99:          //统计数字位数
 100:          Symbol sym = new Symbol(Symbol.number);
 101:          do {                      
 102:              sym.num = 10 * sym.num + curCh - '0';                                    // 获取数字的值
 103:              getch();
 104:          } while (curCh >= '0' && curCh <= '9');                                    //!!!
 105:   
 106:          return sym;
 107:      }
 108:   
 109:      private Symbol matchOperator() {
 110:          Symbol sym = null;
 111:          switch (curCh) {
 112:              case ':':                                                                       // 赋值符号
 113:                  getch();
 114:                  if (curCh == '=') {
 115:                      sym = new Symbol(Symbol.becomes);
 116:                      getch();
 117:                  } else {
 118:                      sym = new Symbol(Symbol.nul);                               //不能识别的符号
 119:                  }
 120:                  break;
 121:              case '<':                                                                    //小于或者小于等于
 122:                  getch();
 123:                  if (curCh == '=') {
 124:                      sym = new Symbol(Symbol.leq);                             //是<=
 125:                      getch();
 126:                  } else if (curCh == '>') {
 127:                      sym = new Symbol(Symbol.neq);                           //是<>
 128:                      getch();
 129:                  } else {
 130:                      sym = new Symbol(Symbol.lss);                             //是<
 131:                  }
 132:                  break;
 133:              case '>':                                                      //大于或者大于等于
 134:                  getch();
 135:                  if (curCh == '=') {
 136:                      sym = new Symbol(Symbol.geq);                           //大于等于
 137:                      getch();
 138:                  } else {
 139:                      sym = new Symbol(Symbol.gtr);                            //大于
 140:                  }
 141:                  break;
 142:              default:
 143:                  sym = new Symbol(ssym[curCh]);
 144:                  if (sym.symtype != Symbol.peroid) {
 145:                      getch();
 146:                  }
 147:          }
 148:          return sym;
 149:      }
 150:  }

.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) – Scanner.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) – SymbolTable.java

    1: package compiler; 2: //竟然没有对符号表检查大小,会溢出的. 3:   4: import java.io.IOException; 5:   6: public clas ...

  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. jQuery学习笔记(二):this相关问题及选择器

    上一节的遗留问题,关于this的相关问题,先来解决一下. this的相关问题 this指代的是什么 这个应该是比较好理解的,this就是指代当前操作的DOM对象. 在jQuery中,this可以用于单 ...

  2. [云上天气预报-有时有闪电]2月3日23:00-4:00阿里云SLB升级期间网络会闪断

    大家好,2月3日23:00-2月4日4:00,阿里云将对SLB(负载均衡)进行升级,在升级期间,SLB会有约4-8次的网络闪断.由此给您带来麻烦,望谅解! 阿里云官方公告内容如下: 尊敬的用户: 您好 ...

  3. j-link或者swd调试

    两种 一.JTAG调试(5针), 二.SWD调试(2针), 在JTAG/SWD模式设置库函数 (在文件stm32f10x_gpio.c中): void GPIO_PinRemapConfig(uint ...

  4. PS技能大全

    1.设置固定的图片大小 参考:http://jingyan.baidu.com/album/642c9d3418bec4644a46f72a.html?picindex=1

  5. position为absolute的元素如何实现居中

    当给div设置absolute时,该元素已经脱离文档流,呈现浮动状态,只能通过left,top,right,bottom来设置属性,要实现居中有两种方法: 一.css法 <div class=& ...

  6. tornado和django的结合使用 tornado Server for django WSGI APP

    #!/usr/bin/env python # Run this with # Serves by default at # http://localhost:8080/hello-tornado a ...

  7. 1019mysql 复制技术

    -- 第一步实现主从复制参照 http://369369.blog.51cto.com/319630/790921/核心点 :开启二进制日子和服务器ID,创建复制账号,配置连接主从服务器,查看各自状态 ...

  8. 使用D3绘制图表(2)--绘制曲线

    上一篇是使用D3绘制画布,这一篇的内容是在画布上绘制曲线. 1.之前的html代码没有变化,但是我还是贴出来 <!DOCTYPE html> <html> <head&g ...

  9. android时区

    <timezones>    <timezone id="Pacific/Majuro">马朱罗</timezone>    <timez ...

  10. java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 错误

    你的ArrayList 是一个没有值的对象(不是null),也就是里面什么对象也没有存(即:arrayList.size()==0).但是,你有取它下标为0值的操作.所以,数组越界了!!比如array ...