思路:利用词法分析一章所讲的状态转化图方法,输入源程序,输出单词符号(token)串
1.单词符号类
package lexical_analyzer;
public class Token {
private String SYM;// 单词类别
private String ID;// 标识符的名字
private String NUM;// 用户定义的数
public Token(String sym, String id, String num) {
this.SYM = sym;
this.ID = id;
this.NUM = num;
}
public String getSYM() {
return SYM;
}
public String getID() {
return ID;
}
public String getNUM() {
return NUM;
}
@Override
public String toString() {
return "<" + this.SYM + "," + this.ID + "," + this.NUM + ">";
}
}
2.词法分析类LexicalAnalyzer
package lexical_analyzer;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class LexicalAnalyzer {
private StringBuffer sourceCode;// 源代码
private int pos;// 字符位置
public static ArrayList<String> remain = new ArrayList<String>();
private ArrayList<Token> wordToken = new ArrayList<Token>();//存放词法分析的结果
static { //所有关键字
remain.add("const");
remain.add("CONST");
remain.add("var");
remain.add("VAR");
remain.add("procedure");
remain.add("begin");
remain.add("end");
remain.add("if");
remain.add("then");
remain.add("call");
remain.add("while");
remain.add("do");
remain.add("read");
remain.add("write");
remain.add("ood");
}
public LexicalAnalyzer(StringBuffer buffer) {
this.sourceCode = buffer;
this.pos = 0;
filterBlank();
System.out.println("源代码:\n"+this.sourceCode);
}
// 滤掉单词间的空格
private void filterBlank() {
for (int i = 0; i < this.sourceCode.length(); i++) {
if (this.sourceCode.charAt(i) == ' ') {
this.sourceCode.deleteCharAt(i);
i--;
}
}
}
// 从处理过的输入源代码中获取一个字符
private char GetChar() {
if (this.sourceCode != null && pos < this.sourceCode.length()) {
return this.sourceCode.charAt(pos++);
}
return ' ';
}
// 当超前分析读出的不是所需的字符时回退一个pos
private void backPos() {
pos--;
}
// 判断一个字符是否是数字
private boolean IsDigit(char ch) {
if (ch == '0' || ch == '1' || ch == '2' || ch == '3' || ch == '4'
|| ch == '5' || ch == '6' || ch == '7' || ch == '8'
&n