📄 analyzerframe.java
字号:
chooser = new JFileChooser("C:\\Program Files");
int k = chooser.showSaveDialog(frm);
if (k == chooser.APPROVE_OPTION) {
try {
FileWriter fw = new FileWriter(chooser.getSelectedFile()
.getAbsolutePath());
fw.write(jTextArea1.getText());
fw.close();
} catch (IOException e) {
}
jTextArea1.setText(null);
} else
jTextArea1.setText(jTextArea1.getText());
} else if (i == 1)
jTextArea1.setText(null);
else
jTextArea1.setText(jTextArea1.getText());
}
// Method to exit
public void exit() {
System.exit(0);
}
// Method to cut words
public void cut() {
try {
String str = this.jTextArea1.getSelectedText();
if (str.length() != 0) {
this.strtext = str;
this.jTextArea1
.replaceRange("", this.jTextArea1.getSelectionStart(),
this.jTextArea1.getSelectionEnd());
this.dirty = true;
}
} catch (Exception ex) {
}
}
// Method to copy words
public void copy() {
try {
String str = this.jTextArea1.getSelectedText();
if (str.length() != 0) {
this.strtext = str;
}
} catch (Exception ex) {
}
}
// Method to paste words
public void paste() {
if (this.strtext.length() > 0) {
this.jTextArea1.insert(this.strtext, this.jTextArea1
.getCaretPosition());
this.dirty = true;
}
}
// Method to cancel
public void cancel() {
undo.undo();
}
// Method to run
public void run() {
StringReader content = new StringReader(jTextArea1.getText());
StringBuffer bf = new StringBuffer(); // Store words or number
br = new BufferedReader(content);
String strTemp = ""; // Read each row
String save = "";
if(jTextArea1.getText().equals("")){
jTextArea2.setText("请输入CMM源程序! \n");
}
else{
while (strTemp != null) {
try {
strTemp = br.readLine();
save = strTemp;
if (strTemp != null && !strTemp.equals("")) {
outStr1 += line + ": " + strTemp + "\n";
jTextArea2.append(outStr1);
outStr1 = ""; // Clear the temp
int row = 0;
int i = 0;
int N = strTemp.length();
int state = 0;
int begin = 0;
int end = 0;
for (i = 0; i < N; i++) {
row++;
char ch = strTemp.charAt(i);
switch (state) {
case 0:
if (ch == '(' || ch == ')' || ch == ';'
|| ch == '{' || ch == '}' || ch == '['
|| ch == ']') {
// if(isDigit(ch) &&
// isDigit(strTemp.charAt(end))){
// end = i;
// jTextArea2.append(" info: 数字1 " +
// strTemp.substring(begin, end) + '\n');
// }
jTextArea2.append(" info: 分隔符 " + ch + '\n');
state = 0;
} else if (ch == '+')
state = 1;
else if (ch == '-')
state = 2;
else if (ch == '*')
state = 3;
else if (ch == '/')
state = 4;
else if (ch == '=')
state = 5;
else if (ch == '<')
state = 6;
else if (isLetter(ch)) {
state = 7;
begin = i;
} else if (isDigit(ch)) {
begin = i;
state = 8;
}
break;
case 1: // For +
jTextArea2.append(" info: 运算符 +" + "\n");
i--;
row--;
state = 0;
break;
case 2: // For -
jTextArea2.append(" info: 运算符 - \n");
i--;
row--;
state = 0;
break;
case 3: // For *
if (ch == '/') {
state = 0;
jTextArea2.append(" info: 注释 */ \n");
} else {
jTextArea2.append(" info: 运算符 * \n");
i--;
row--;
state = 0;
}
break;
case 4: // For /
if (ch == '*') {
state = 0;
jTextArea2.append(" info: 注释 /* \n");
} else {
jTextArea2.append(" info: 运算符 / \n");
i--;
row--;
state = 0;
}
break;
case 5: // For =
if (ch == '=') {
jTextArea2.append(" info: 运算符 == \n");
state = 0;
} else {
state = 0;
jTextArea2.append(" info: 运算符 = \n");
}
break;
case 6: // For <
if (ch == '>') {
jTextArea2.append(" info: 运算符 <> \n");
state = 0;
} else {
state = 0;
jTextArea2.append(" info: 运算符 < \n");
}
break;
case 7: // For letters
if (isLetter(ch) || isDigit(ch)) {
state = 7;
} else {
end = i;
String id = strTemp.substring(begin, end);
if (isKey(id))
jTextArea2.append(" info: 关键字 " + id
+ '\n');
else
jTextArea2.append(" info: 标志符 " + id
+ '\n');
i--;
row--;
state = 0;
}
break;
case 8:// For digit
if (isDigit(ch)) {
state = 8;
} else {
if (isLetter(ch)) {
jTextArea2.append(" error: line " + line
+ " row " + row + " "
+ "数字格式错误或者标志符错误\n");
}else{
end = i;
String id = strTemp.substring(begin, end);
jTextArea2.append(" info: 数字 " + id
+ '\n');
}
int temp = i;
i = find(i, strTemp);
row += (i - temp);
state = 0;
}
break;
}
}
}
line++;
} catch (IOException e) {
}
}
// Clear the temp
outStr2 = "";
line = 1; // Redo
jTextArea2.append("词法分析完成! \n");
}
}
boolean isLetter(char c) {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_')
return true;
return false;
}
boolean isDigit(char c) {
if (c >= '0' && c <= '9')
return true;
return false;
}
boolean isKey(String str) {
if (str.equals("if") || str.equals("else") || str.equals("while")
|| str.equals("read") || str.equals("write")
|| str.equals("int") || str.equals("real"))
return true;
return false;
}
int find(int begin, String str) {// find the ' ' ,enter,{},[],()...
if (begin >= str.length())
return str.length();
for (int i = begin; i < str.length(); i++) {
char c = str.charAt(i);
if (c == '\n' || c == ',' || c == ' ' || c == '\t' || c == '{'
|| c == '}' || c == '(' || c == ')' || c == ';' || c == '='
|| c == '+' || c == '-' || c == '*' || c == '/')
return i - 1;
}
return str.length();
}
// Handle About menu action events
public void actionPerformed(ActionEvent e) {
if (e.getSource() == help_action) {
JOptionPane.showMessageDialog(this, "这是一个很简单的词法分析器, \n"
+ "您只需要输入CMM源程序 ,再点击运行即可", "帮助",
JOptionPane.INFORMATION_MESSAGE);
} else if (e.getSource() == about_action) {
JOptionPane.showMessageDialog(this,
"| 程序作者: 武汉大学国际软件学院软件工程专业05级 寇文波 \n"
+ "| 课 程:《编译原理与实践》 \n"
+ "| 设计时间: 2007年10月 \n"
+ "| 邮箱地址: jason_kou@163.com \n"
+ "| 欢迎大家与我交流,让我们一起共同提高!^_^ \n",
"关于词法分析器", JOptionPane.INFORMATION_MESSAGE);
}
}
// Create UndoHandler Implements the interface :UndoableListener
class UndoHandler implements UndoableEditListener {
public void undoableEditHappened(UndoableEditEvent uee) {
undo.addEdit(uee.getEdit());
}
}
// File actions
private FileAction new_action, open_action, save_action, close_action,
cut_action, copy_action, paste_action, cancel_action, run_action,
font_action;
private JMenuItem about_action, help_action;
private JFileChooser chooser = new JFileChooser();
private JFrame frm = new JFrame();
private JMenuBar menuBar = new JMenuBar();
private JToolBar toolBar = new JToolBar();
private JPopupMenu popupMenu = new JPopupMenu();
private JPanel jPanel1 = new JPanel();
private JPanel jPanel2 = new JPanel();
private JScrollPane jScrollPane1 = new JScrollPane();
private JScrollPane jScrollPane2 = new JScrollPane();
protected JTextArea jTextArea1 = new JTextArea();
protected JTextArea jTextArea2 = new JTextArea();
private UndoManager undo = new UndoManager();
private UndoableEditListener undoHandler = new UndoHandler();
boolean dirty = true;
String strtext = "";
static BufferedReader br = null;
int line = 1;
String outStr = "";
String outStr1 = "";
String outStr2 = "";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -