?? themain.java
字號:
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.util.*;
public class TheMain { //程序的主方法
public static void main(String[] args) {
MyFrame mf = new MyFrame();
}
}
class MyFrame extends Frame implements ActionListener { //創建界面的類
String head1,head2,head3,head4,head5;
FileDialog filedialog_save, filedialog_load;
TextArea text1, text2;
BufferedReader in;
FileReader file_reader;
BufferedWriter out;
FileWriter tofile;
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
Button b1 = new Button("打開");
Button b2 = new Button("分析");
Button b3 = new Button("保存");
Button b4 = new Button("關閉");
Button b5 = new Button("清空");
MyFrame() {
super("DO—WHILE循環編譯器!");
this.setBounds(0, 0, 750, 500);
this.setLayout(new GridLayout(1, 2));
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
filedialog_save = new FileDialog(this, "保存文件對話框", FileDialog.SAVE);
filedialog_load = new FileDialog(this, "打開文件對話框", FileDialog.LOAD);
filedialog_save.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
filedialog_save.setVisible(false);
}
});
filedialog_load.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
filedialog_load.setVisible(false);
}
});
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setLayout(new BorderLayout());
text1 = new TextArea();
text2 = new TextArea();
text2.setEditable(false);
text1.setFont(new Font("BinnerGothic", Font.BOLD, 14));
text2.setFont(new Font("BinnerGothic", Font.BOLD, 14));
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b5);
p1.add(b4);
p2.setLayout(new BorderLayout());
p2.add(text2, BorderLayout.CENTER);
p2.add(p1, BorderLayout.SOUTH);
p3.setLayout(new BorderLayout());
p3.add(text1, BorderLayout.WEST);
p3.add(p2, BorderLayout.CENTER);
this.add(p3);
this.setVisible(true);
this.setResizable(false);
}
public void actionPerformed(ActionEvent e) { //事件監聽的方法
if (e.getSource() == b1) { //如果按的是打開按鈕,則打開文件
filedialog_load.setVisible(true);
text1.setText(null);
String s;
if (filedialog_load.getFile() != null) {
try {
File file = new File(filedialog_load.getDirectory(),
filedialog_load.getFile());
file_reader = new FileReader(file);
in = new BufferedReader(file_reader);
while ((s = in.readLine()) != null)
text1.append(s + '\n');
in.close();
file_reader.close();
} catch (IOException e2) {
}
}
} else if (e.getSource() == b3) { //如果按的是保存按鈕,則保存文件
filedialog_save.setVisible(true);
if (filedialog_save.getFile() != null) {
try {
File file = new File(filedialog_save.getDirectory(),
filedialog_save.getFile());
tofile = new FileWriter(file);
out = new BufferedWriter(tofile);
out.write(text1.getText(), 0, (text1.getText().length()));
out.flush();
out.close();
tofile.close();
} catch (IOException e2) {
}
}
} else if (e.getSource() == b4) { //如果按的是關閉按鈕,則關閉界面
this.dispose();
} else if (e.getSource() == b5) { //如果按的是清空按鈕,則清空兩個文本區
text1.setText("");
text2.setText("");
} else if (e.getSource() == b2) { //如果按的是分析按鈕,則開始進行詞法和語法分析
CiFaFenXi cf = new CiFaFenXi();
cf.CJudge();
YuFaFenXi yf = new YuFaFenXi();
yf.YJudge();
text2.append("L1:" + "\n");
Display ds = new Display();
ds.EvaluateExpression();
text2.append(head4+ " =" + head5 + "\n");
text2.append("if "+head1+head2+head3+" goto L1"+ "\n");
text2.append("else goto exit" + "\n");
}
}
public class CiFaFenXi { //詞法分析的類,與以前編寫的詞法分析程序的方法類似
KeyWord key = new KeyWord();
int i = 0;
int j = 0;
int line = 1;
char c = getChar();
String input = new String();
String output = new String();
public boolean isLetter() { //判斷是否是字母
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
return true;
else
return false;
}
public boolean isNumber() {//判斷是否是數字
if (c >= '0' && c <= '9')
return true;
else
return false;
}
public boolean isOperate() {//判斷是否是操作符
Operater oper = new Operater();
for (int i = 0; i < oper.y; i++) {
if (c == oper.op[i])
return true;
}
return false;
}
public boolean isFenJie() {//判斷是否是分解符
if (c == ';') {
return true;
} else
return false;
}
public char getChar() { //取一個字符
char[] ch = text1.getText().toCharArray();//通過把文本區的內容轉換為一個字符數組,再一個個取出
if (i < (text1.getText().length())) {
c = ch[i];
i++;
}
return c;
}
public void CJudge() { //開始詞法分析,為每一行添加一個行號
int a = text1.getText().length();
text2.append("文件總的字符數為:" + a + "\n");
text2.append("行號為:1 " + " \n");
while (j <= text1.getText().length()) {//判別條件,只要沒有取完則繼續
if (c == ' ' || c == 9) {//如果是空格后TAB,則跳過
getChar();
j++;
} else if (c == '\n') {//如果是換行,則行號加1
c = ' ';
getChar();
line++;
j++;
text2.append("行號為:" + line + "\n");
} else if (isLetter()) {//如果是字母,則把接下來的所有字母和數字組合在一起
boolean flag = false;
while (isLetter() || isNumber() ) {
output = output + c;
getChar();
j++;
}
for (int i = 0; i < key.x; i++) {//把該組合與關鍵字類中的關鍵字進行比較
if (output.equals(key.k[i])) {
flag = true;
break;
}
}
if (flag) {
text2.append(" 關鍵字為:" + output + "\n");//是關鍵字,則輸出
} else {
text2.append(" 標識符為:" + output + "\n");//否則,就是標識符
}
output = "";
} else if (isNumber()) {//是數字,并且全是數字,則組合在一起
while (isNumber()) {
output = output + c;
getChar();
j++;
}
text2.append(" 數字為:" + output + "\n");//輸出數字
output = "";
} else if (isOperate()) {//是操作符
text2.append(" 操作符為:" + c + "\n");
getChar();
j++;
} else if (isFenJie()) {//是分解符
text2.append(" 分解符為:" + c + "\n");
getChar();
j++;
}
}
}
}
class YuFaFenXi {//語法分析的類,采用的是LL(1)方法
char c = getChar(0);
int count = 0;
int k = 0;
String output = new String();
public boolean isLetter() {//以下的幾個方法同詞法分析
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
return true;
else
return false;
}
public boolean isNumber() {//同上
if (c >= '0' && c <= '9')
return true;
else
return false;
}
public boolean isOperate() {//同上
Operater oper = new Operater();
for (int i = 0; i < oper.y; i++) {
if (c == oper.op[i])
return true;
}
return false;
}
public boolean isFenJie() {//同上
if (c == ';') {
return true;
} else
return false;
}
public char getChar(int i) {//取下一個字符,不同于詞法分析的是,過濾掉$符
char[] ch = text1.getText().toCharArray();
if (i < (text1.getText().length())) {
c = ch[i];
while(c == '$') {
++i;
c = ch[i];
}
i++;
}
return c;
}
public String getString(int m) {//通過getChar()方法,來定義一個getStrig()方法,類似詞法分析
String output = new String();
c = getChar(m);
if (isLetter() || isNumber()) {//是關鍵字或標識符或數字,則將其返回
while (isLetter() || isNumber()) {//count做為計數器,是一個全局的變量,將每次所取的位置
output = output + c;//記錄下來,以方便下次取字符
c = getChar(++m);
count = m;
}
return output;
/*} else if (isNumber()) {
while (isNumber()) {
output = output + c;
c = getChar(++m);
count = m;
}
return output;*/
} else if (isOperate()) {//是操作符,則將其返回
output = "" + c;
count = m + 1;
// c = getChar();
return output;
} else if (isFenJie()) {//是分解符,則將其返回
output = "" + c;
count = m + 1;
// c = getChar();
return output;
} else
return ""; //否則返回一個空的字符
}
String[][] s = new String[8][15];//定義一個二維的字符串數組,作為預測分析表
{
for (int i = 0; i < 8; i++) {//先將所有的值置為@
for (int j = 0; j < 15; j++) {
s[i][j] = "@";
}
}
s[0][0] = ")E(w};G{d";//再分別為對應位置賦值
s[1][2] = "R=i";
s[2][2] = "Bi";
s[3][7] = "BRT";
s[3][8] = "BRT";
s[3][9] = "BRT";
s[3][10] = "BRT";
s[4][7] = "+";
s[4][8] = "-";
s[4][9] = "*";
s[4][10] = "/";
s[5][2] = "RFR";
s[6][11] = ">";
s[6][12] = "=";
s[6][13] = "<";
}
Stack<String> cs = new Stack<String>();//創建一個棧
String string = this.getString(count);//創建一個變量,用來每次取一個字符串
int getLittle() {//為每一個字符串變量,進行定位操作,即為其編號,以方便后面的比較操作
// char c1 = getChar();
if (string.equals("do"))
return 0;
if (string.equals("while"))
return 1;
/*
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -