• 大小: 2KB
    文件类型: .7z
    金币: 1
    下载: 0 次
    发布日期: 2021-08-05
  • 语言: 其他
  • 标签:

资源简介

可用于编译原理的课设和平时作业,程序比较简单,结构清晰,程序从文件读入

资源截图

代码片段和文件信息

package huhcun;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class MiddleCode {
static int num = 1;//四元式序列长度
public static List keyWord = new ArrayList<>();//关键字
public static List four = new ArrayList<>();//四元式
static int T = 0;
public MiddleCode() {
keyWord.add(“if“);
keyWord.add(“and“);
keyWord.add(“then“);
keyWord.add(“while“);
keyWord.add(“do“);
keyWord.add(“for“);
keyWord.add(“to“);
}

public static ArrayList getFile() {
try {
String temp = null;
File f = new File(“te.txt“);
InputStreamReader read = new InputStreamReader(new FileInputStream(f) “GBK“);
BufferedReader reader = new BufferedReader(read);
ArrayList readList = new ArrayList();
while ((temp = reader.readLine()) != null && !““.equals(temp)) {
readList.add(temp);
}
read.close();
return readList;
} catch (Exception e) {
System.out.println(“读取文件失败!文件路径错误或者文件不存在“);
e.printStackTrace();
return null;
}
}

public static ArrayList change(String a) {// 将字符串转换成字符数组,提取单个单词
String[] b = a.split(“ “);
ArrayList b1 = new ArrayList();
for (String c : b) {
if (!c.equals(““)) {
b1.add(c);
}
}
//System.out.println(b1);
return b1;

}

public static void addfour(String op String arg1 String arg2 String result) {
String str = (“(“ + op + ““ + arg1 + ““ + arg2 + ““ + result + “)“);
num++;
four.add(str);

}

public static ArrayList bexp(ArrayList d) {// 分析表达式

for (int i = 1; i < d.size(); i++) {
if (d.get(i).equals(“*“) || d.get(i).equals(“/“)) {
addfour(d.get(i) d.get(i - 1) d.get(i + 1) (“t“ + T));
String newcode = (“t“ + T);
//System.out.println(d);
d.set(i newcode);
//System.out.println(d);
d.remove(i - 1);
//System.out.println(d);
d.remove(i);
//System.out.println(d);
T++;
bexp(d);

} else if (d.get(i).equals(“+“) || d.get(i).equals(“-“)) {
addfour(d.get(i) d.get(i - 1) d.get(i + 1) (“t“ + T));
String newcode = (“t“ + T);
d.set(i newcode);
d.remove(i - 1);
d.remove(i);
T++;
bexp(d);

} else if (d.get(i).equals(“<“) || d.get(i).equals(“>“) || d.get(i).equals(“<=“) || d.get(i).equals(“>=“)) {
addfour((“j“ + d.get(i)) d.get(i - 1) d.get(i + 1) (““ + (num + 2)));
d.set(i “j“);
d.set(i - 1 “12“);
d.set(i+1 “ “);
bexp(d);
}
}
return d;
}
public static ArrayList bexp1(ArrayList d) {// 分析表达式

for (int i = 1; i < d.size(); i++) {
if (d.get(i).equals(“*“) || d.get(i).equals(“/“)) {
addfour(d.get(i) d.get(i - 1) d.get(i + 1) (“t“ + T));
String newcode = (“t“ + T);
//System.out.println(d);
d.set(i newcode);
//Sys

评论

共有 条评论

相关资源