package 简单词法分析器;
import java.io.*;
import java.util.*;
public class LexicalAnalyzer {
/* 源文件名 */
private String sourcePath;
/* 程序语句 */
private String sentence;
/* 分析得到的字符串 */
private String word = "";
/* 分析结果字符的参数 */
private Attributes attr = new Attributes();
/* 词法分析结果表 */
private ArrayList<Attributes> analysisResult = new ArrayList<Attributes>();
private ArrayList<Attributes> errorList = new ArrayList<Attributes>();
/* 括号匹配需要用的栈 */
private Stack<Attributes> stack_p = new Stack<Attributes>();
private Stack<Attributes> stack_s = new Stack<Attributes>();
private Stack<Attributes> stack_b = new Stack<Attributes>();
/* 标识符表 */
private ArrayList<String> id = new ArrayList<String>();
/* 常数表 */
private ArrayList<String> num = new ArrayList<String>();
/* 当前读到的行数 */
private int line = 0;
/* 当前读到的字符数 */
private int index = 0;
/* 文件访问 */
private File file;// 用指定的文件名构建文件对象
private RandomAccessFile raf;// 创建文件随机访问
private int loadLine = 0; // 当前访问的行
/* 源代码总行数 */
private int fileLine = 0;
/* 关键字表 */
private final String keyWord[] = { "int", "double", "float", "char",
"public", "private", "static", "class", "void", "const", "for",
"if", "else", "then", "while", "switch", "break", "begin", "end",
"final", "return" };
public LexicalAnalyzer(String sourcePath) {
this.sourcePath = sourcePath;
try {
file = new File(this.sourcePath);// 用指定的文件名构建文件对象
raf = new RandomAccessFile(file, "r");// 创建文件随机访问
try {
raf.seek(0);
while (raf.readLine() != null) { // 获取代码总行数
this.fileLine++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件未找到,请输入正确的文件路径");
}
}
public void initData() {
this.line = 0;
this.index = 0;
this.loadLine = 0;
this.analysisResult.clear();
this.errorList.clear();
this.id.clear();
this.num.clear();
}
public void setSourcePath(String sourcePath) {
this.sourcePath = sourcePath;
}
public String getSourcePath() {
return this.sourcePath;
}
private void addResult(ArrayList<Attributes> attrList, String key,
String type, int line, int index) {
attr = new Attributes();
attr.key = key;
attr.type = type;
attr.line = line;
attr.index = index;
attrList.add(attr);
}
private boolean isDigit(char ch) {
if ((ch >= '0') && (ch <= '9'))
return true;
else
return false;
}
private boolean isLetter(char ch) {
if (((ch >= 'A') && (ch <= 'Z')) || ((ch >= 'a') && (ch <= 'z')))
return true;
else
return false;
}
private String getFullWord(String str, int start) { // 发现非法标识符时,获取全词
String result = "";
char ch = str.charAt(start);
for (int i = start; i < str.length()
&& (isDigit(ch) || isLetter(ch) || ch == '_' || ch == '$'); i++) {
ch = str.charAt(i);
result += ch;
}
result = result.substring(0, result.length() - 1); // 回退一个字符
return result;
}
private void checkKeyWord(String str) {
boolean iskeyword = false;
boolean isidentifier = false;
for (int i = 0; i < keyWord.length; i++) {
if (str.equals(keyWord[i]))// 是关键字
{
System.out.println("(" + str + "," + "K)");
this.addResult(analysisResult, str, "关键字", line, index);
iskeyword = true; // 标记为真
break; // 结束循环
}
}
if (!iskeyword) { // 普通标志符
for (int j = 0; j < id.size(); j++) { // 在标志符表中查找是否有该标志符
if (str.equals(id.get(j)))// 标志符表中已经有该标志符
{
System.out.println("(" + id.get(j) + "," + j + ",I)"); // 输出该标志符在标志符表中的位置
this.addResult(analysisResult, str, "标识符", line, index);
isidentifier = true; // 标记为是标识符
break; // 退出循环
}
}
if (!isidentifier) {// 标志符表中没有该标志符,将该标志符存入标志符表中,并返回其在符号表中的位置
id.add(str); // 标志符新增一个
int location = id.size() - 1;
System.out.println("(" + id.get(location) + "," + location
+ ",newI)");
this.addResult(analysisResult, str, "标识符", line, index);
}
}
}
private void isNumber(String str) {
boolean isnumber = false;
for (int i = 0; i < num.size(); i++) {
if (str.equals(num.get(i))) { // 常量表中是否有该数字
System.out.println("(" + num.get(i) + "," + i + ",N)");
this.addResult(analysisResult, str, "常量", line, index);
isnumber = true;
break;
}
}
if (!isnumber) { // 该数字不存在数字表中
num.add(str);
int location = num.size() - 1;
System.out.println("(" + num.get(location) + "," + location
+ ",newN)");
this.addResult(analysisResult, str, "常量", line, index);
}
}
private void checkParenthesisMatch(String str, int line, int index) {
Attributes tempattr = new Attributes();
tempattr.key = str;
tempattr.line = line;
tempattr.index = index;
if (str.equals("(")) {
tempattr.type = "小括号匹配有误";
this.stack_p.push(tempattr);
} else if (str.equals("[")) {
tempattr.type = "中括号匹配有误";
this.stack_s.push(tempattr);
} else if (str.equals("{")) {
tempattr.type = "大括号匹配有误";
this.stack_b.push(tempattr);
} else {
if (!this.stack_p.empty()) { // 非空则弹出
if (str.equals(")") && this.stack_p.peek().key.equals("(")) {
this.stack_p.pop();
}
} else {
if (str.equals(")")) { // 栈已空,说明左括号匹配完毕,用括号多余,直接进栈
tempattr.type = "小括号匹配有误";
this.stack_p.push(tempattr);
}
}
if (!this.stack_s.empty()) { // 非空则弹出
if (str.equals("]") && this.stack_s.peek().key.equals("[")) {
this.stack_s.pop();
}
} else {
if (str.equals("]")) { // 栈已空,说明左括号匹配完毕,用括号多余,直接进栈
tempattr.type = "中括号匹配有误";
this.stack_s.push(tempattr);
}
}
if (!this.stack_b.empty()) { // 非空则弹出
if (str.equals("}") && this.stack_b.peek().key.equals("{")) {
this.stack_b.pop();
}
} else {
if (str.equals("}")) { // 栈已空,说明左括号匹配完毕,用括号多余,直接进栈
tempattr.type = "大括号匹配有误";
this.stack_b.push(tempattr);
}
}
}
}
/**
* 统计括号匹配结果
*/
public void countParenthesisMatch() {
while (!this.stack_p.empty()) { // 如果栈非空加入错误列表
this.errorList.add(this.stack_p.peek());
this.stack_p.pop();
}
while (!this.stack_s.empty()) {
this.errorList.add(this.stack_s.peek());
this.stack_s.pop();
}
while (!this.stack_b.empty()) {
this.errorList.add(this.stack_b.peek());
this.stack_b.pop();
}
}
public ArrayList<Attributes> searchString(String str) {
ArrayList<Attributes> searchResult = new ArrayList<Attributes>();
for (int i = 0; i < analysisResult.size(); i++) {
attr = analysisResult.get(i);
if (str.equals(attr.key)) {
searchResult.add(attr);
}
}
return searchResult;
}
public void printTableResults() {
System.out.println("标志符表中的内容为:");
for (int i = 0; i < this.id.size(); i++) {
System.out.println(i + "\t" + this.id.get(i));
}
System.out.println("常数表中的内容为:");
for (int i = 0; i < this.num.size(); i++) {
System.out.println(i + "\t" + this.num.get(i));
}
}
public void printResults() {
System.out.println("关键字:");
System.out.println("key" + "\t" + "line" + "\t" + "index");
for (int i = 0; i < analysisResult.size(); i++) {
attr = analysisResult.get(i);
if (attr.type.equals("keyword")) {
System.out.println(attr.key + "\t" + attr.line + "\t"
+ attr.index);
}
}
System.out.println("标识符:");
System.out.println("key" +
没有合适的资源?快使用搜索试试~ 我知道了~
简单词法分析器,有穷自动机,正规文法,正则表达式的转换功能的Java实现
共65个文件
class:51个
java:12个
classpath:1个
5星 · 超过95%的资源 需积分: 50 125 下载量 137 浏览量
2016-02-01
18:51:36
上传
评论 11
收藏 93KB RAR 举报
温馨提示
该程序实现1.面向某一高级语言子集的词法分析器;2.将给定的正规文法转换为正规表达式;3.正规文法与有穷自动机的相互转换;4.利用给定的正规文法、有穷自动机或正规表达式其中之一,对给定的字符串开展词法分析,给出判定结果.程序实现图形化界面,美观大方。
资源推荐
资源详情
资源评论
收起资源包目录
编译原理.rar (65个子文件)
编译原理
bin
简单词法分析器
Test.class 1KB
GUI_LexicalAnalyzer$5.class 2KB
GUI_LexicalAnalyzer$4.class 2KB
GUI_LexicalAnalyzer$1.class 864B
GUI_LexicalAnalyzer$3.class 2KB
GUI_LexicalAnalyzer.class 9KB
GUI_LexicalAnalyzer$6.class 842B
GUI_LexicalAnalyzer$2.class 3KB
LexicalAnalyzer.class 19KB
Attributes.class 472B
main
Launcher$4.class 811B
Launcher$3.class 817B
Launcher$2.class 822B
Launcher$5.class 795B
Launcher.class 2KB
Launcher$1.class 707B
有穷自动机判断
GUI_Recognizer$5.class 1KB
Recognizer.class 3KB
GUI_Recognizer$8.class 2KB
GUI_Recognizer$1.class 805B
GUI_Recognizer$7.class 2KB
GUI_Recognizer$2.class 2KB
GUI_Recognizer.class 7KB
GUI_Recognizer$9.class 812B
GUI_Recognizer$3.class 2KB
GUI_Recognizer$6.class 2KB
GUI_Recognizer$4.class 1KB
正规文法与有穷自动机
GUI_GramAndFA$9.class 1KB
GUI_GramAndFA$4.class 2KB
GUI_GramAndFA$7.class 3KB
GramAndFA.class 6KB
GUI_GramAndFA$6.class 2KB
GUI_GramAndFA$2.class 2KB
GUI_GramAndFA$1.class 836B
GUI_GramAndFA.class 8KB
GUI_GramAndFA$11.class 853B
GUI_GramAndFA$10.class 1KB
GUI_GramAndFA$8.class 2KB
GUI_GramAndFA$5.class 2KB
GUI_GramAndFA$3.class 2KB
正规文法与正规表达式
GUI_GramerToExpr$5.class 2KB
GUI_GramerToExpr$6.class 1KB
GUI_GramerToExpr$8.class 869B
GUI_GramerToExpr.class 6KB
GUI_GramerToExpr$1.class 851B
GrammarConverter.class 3KB
GUI_GramerToExpr$7.class 1KB
GUI_GramerToExpr$2.class 1KB
GUI_GramerToExpr$4.class 2KB
GramToExpr.class 6KB
GUI_GramerToExpr$3.class 1KB
.settings
src
简单词法分析器
GUI_LexicalAnalyzer.java 11KB
Attributes.java 159B
LexicalAnalyzer.java 22KB
Test.java 800B
main
Launcher.java 3KB
有穷自动机判断
Recognizer.java 3KB
GUI_Recognizer.java 11KB
正规文法与有穷自动机
GramAndFA.java 6KB
GUI_GramAndFA.java 13KB
正规文法与正规表达式
GramToExpr.java 7KB
GrammarConverter.java 2KB
GUI_GramerToExpr.java 7KB
.project 397B
.classpath 302B
共 65 条
- 1
资源评论
- KuLe酷乐2017-11-20可以噢。不错的 嘿嘿
- alec10192019-06-11可以借鉴,谢谢分享
Boreaso
- 粉丝: 1
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功