Java版本词法分析

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

package lexical_analysis;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.PrintWriter;

public class Scanner_2 {

// 定义DFA中的所有状态表

// enum StateType {Start, Num, ID, EQ, NE, NM, NL,

// Com, LineCom, MulCom1, MulCom2, Special, Done, Str};

// 定义DFA中的所有状态表

private static final int Start = 1;

private static final int Num = 2;

private static final int ID = 3;

private static final int EQ = 4;

private static final int NE = 5;

private static final int NM = 6;

private static final int NL = 7;

private static final int Coms = 8;

private static final int LineCom = 9;

private static final int MulCom1 = 10;

private static final int MulCom2 = 11;

private static final int Special = 12;

private static final int Done = 13;

private static final int Str = 14;

// Token类型,Initial为初始类型

private enum TokenType {

Initial, ID, Special, Str, KeyWord

};

// 关键字

private String[] keyWords = new String[] {

"include", "define", "iostream", "int", "folat", "double",

"main", "if", "else", "for", "while", "do", "goto", "switch",

"case", "static", "cin", "cout"

};

// 特殊字符

private String [] special = {"{", "}", "[", "]", "(", ")", "#", ",", ".", ";", ":", "\\",

"'", "\"", ">>", "<<", "!=", "=",

"==", "<=", ">=", "++", "--"};

// 算术运算符

private String [] arithmetic = {"+", "-", "-", "/", "%"};

// 源代码文件输入流

private BufferedReader sourceFile;

// 压缩后的文件输出流

private PrintWriter compressedFileWriter;

// 上一个Token的类型

private TokenType preType = TokenType.Initial;

// 缓存去除多余空格、注释后的源代码

private StringBuilder compressedStr = new

StringBuilder();

// 扫描行的最大字符数

private static final int BUF_SIZE = 256;

// 当前行的字符长度

private int bufSize = 0;

// 当前行

private String eachLine;

// 当前扫描行的字符序列

private char [] lineBuf = new char[BUF_SIZE];

// 当前扫描的行数

private int lineNum = 0;

// 当前行的字符下标

private int charPos = 0;

// 是否已达文件尾

private boolean isEOF = false;

/**

* 每次扫描前都要初始化一些必要变量值

*/

private void initial(){

bufSize = 0;

lineNum = 0;

charPos = 0;

isEOF = false;

}

/**

* 初始化并读取源代码文件

* 扫描程序开始执行,直到读取文件结束符EOF

* @throws Exception

*/

private void scanning(String originalFile) throws Exception {

this.sourceFile = new BufferedReader(new FileReader(originalFile));

this.initial();

while(!isEOF) {

getToken();

}

System.out.println("========================> end scanning ...");

}

/**

* 获取下一个字符

* @return

* @throws Exception

相关文档
最新文档