📄 frame1.java
字号:
package 编译技术;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* <p>Title: </p>
* <p>Description: 词法分析</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: 边缘开发室</p>
* @author 谭正辉
* @version 1.0
*/
public class Frame1
extends JFrame
{
JPanel contentPane;
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea sourceArea = new JTextArea();
JButton doButton1 = new JButton();
JButton quitButton = new JButton();
//源文件
String str = null;
JLabel jLabel1 = new JLabel();
JScrollPane jScrollPane2 = new JScrollPane();
JTextArea MessageBox = new JTextArea();
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenu1 = new JMenu();
JMenuItem jMenuItem1 = new JMenuItem();
JMenuItem jMenuItem2 = new JMenuItem();
ImageIcon image = new ImageIcon("src/image/letterTable.jpg");
JMenuItem jMenuItem3 = new JMenuItem();
int row, column, locate; //row为行,column为列
char[] sourceChar; //要分析的字符数组
int type; //类型
//用于存储当前字符串单元
String tempString;
ReservedWordTable rwt;
//用于标识是否通过词法分析,如果通过了,才可以进行语法分析,如果没有,则必须修正提示的错误
boolean passWordAnalyse=true;
JButton doButton2 = new JButton();
//Construct the frame
public Frame1()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
jbInit();
}
catch (Exception e)
{
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception
{
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(null);
this.setJMenuBar(jMenuBar1);
this.setResizable(false);
this.setSize(new Dimension(615, 459));
this.setTitle("词法分析");
jScrollPane1.setBounds(new Rectangle(8, 14, 448, 222));
sourceArea.setToolTipText("输入要词法分析的程序源码");
sourceArea.setLineWrap(true);
sourceArea.setTabSize(4);
doButton1.setBounds(new Rectangle(475, 23, 102, 36));
doButton1.setText("词法分析");
doButton1.addActionListener(new Frame1_doButton_actionAdapter(this));
quitButton.setText("退出");
quitButton.addActionListener(new Frame1_quitButton_actionAdapter(this));
quitButton.setBounds(new Rectangle(475, 343, 102, 36));
jLabel1.setText("编译信息如下");
jLabel1.setBounds(new Rectangle(8, 237, 446, 21));
MessageBox.setBorder(BorderFactory.createLoweredBevelBorder());
jScrollPane2.setBounds(new Rectangle(8, 256, 448, 126));
MessageBox.setBorder(null);
MessageBox.setText("");
MessageBox.setEditable(false);
jMenu1.setText("系统信息");
jMenuItem1.setText("类别码表");
jMenuItem1.addActionListener(new Frame1_jMenuItem1_actionAdapter(this));
jMenuItem2.setText("关于本软件");
jMenuItem2.addActionListener(new Frame1_jMenuItem2_actionAdapter(this));
jMenuItem3.setText("打开文件");
jMenuItem3.addActionListener(new Frame1_jMenuItem3_actionAdapter(this));
doButton2.setBounds(new Rectangle(475, 177, 102, 36));
doButton2.setText("语法分析");
doButton2.addActionListener(new Frame1_jButton1_actionAdapter(this));
contentPane.add(jScrollPane1, null);
contentPane.add(jLabel1, null);
contentPane.add(jScrollPane2, null);
contentPane.add(quitButton, null);
contentPane.add(doButton2);
contentPane.add(doButton1, null);
jScrollPane2.getViewport().add(MessageBox, null);
jScrollPane1.getViewport().add(sourceArea, null);
jMenuBar1.add(jMenu1);
jMenu1.add(jMenuItem3);
jMenu1.add(jMenuItem1);
jMenu1.add(jMenuItem2);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}
}
void quitButton_actionPerformed(ActionEvent e)
{
//退出程序
System.exit(0);
}
void doButton_actionPerformed(ActionEvent e)
{
MessageBox.setText(" 词法分析结果如下");
column = row = 1;
locate = 0;
//实例化构建Hash表存入保留字的类
rwt = new ReservedWordTable();
//获取待编译的字符串并存放在一个字符数组里面
String source = sourceArea.getText().toString();
sourceChar = new char[source.length()];
sourceChar = source.toCharArray();
while (locate < sourceChar.length)
{
//对源程序进行词法分析
startWordAnalyse();
}
MessageBox.setText(MessageBox.getText() + "\n 词法分析完成");
//保存结果
saveResult();
}
public void startWordAnalyse()
{
switch (sourceChar[locate])
{
case '+':
{
type = 13;
tempString = "+";
locate++;
column++;
break;
}
case '-':
{
type = 14;
tempString = "-";
locate++;
column++;
break;
}
case '*':
{
type = 15;
tempString = "*";
locate++;
column++;
break;
}
case '/':
{
type = 16;
tempString = "/";
locate++;
column++;
break;
}
//:=
case ':':
{
if (locate != sourceChar.length - 1)
{
if (sourceChar[locate + 1] == '=')
{
locate++;
column++;
type = 17;
tempString = ":=";
}
//:单独出现在中间位置
else
{
type = 1000;
tempString = ":";
}
}
//:出现在结束位置
else if (locate == sourceChar.length - 1)
{
type = 1000;
tempString = ":";
}
locate++;
column++;
break;
}
//<=,<>,<
case '<':
{
if (locate != sourceChar.length - 1)
{
if ( (sourceChar[locate + 1] != '=') &&
(sourceChar[locate + 1] != '>'))
{
//识别<
type = 20;
tempString = "<";
}
else if (sourceChar[locate + 1] == '=')
{
//识别<=
locate++;
column++;
type = 22;
tempString = "<=";
}
else if (sourceChar[locate + 1] == '>')
{
//识别<>
locate++;
column++;
type = 21;
tempString = "<>";
}
}
locate++;
column++;
break;
}
case '>':
{
if (locate != sourceChar.length - 1)
{
if (sourceChar[locate + 1] != '=')
{
//识别>
type = 23;
tempString = ">";
}
else
{
locate++;
column++;
type = 24;
tempString = ">=";
}
}
locate++;
column++;
break;
}
case '=':
{
type = 25;
tempString = "=";
locate++;
column++;
break;
}
case '#':
{
type = 0;
tempString = "#";
column++;
locate++;
break;
}
case ';':
{
type = 18;
tempString = ";";
column++;
locate++;
break;
}
default:
{
//如果是空格
if (sourceChar[locate] == ' ')
{
//对type=100为不做任何处理,只移位置
type = 100;
locate++;
column++;
}
//如果是换行符
else if (sourceChar[locate] == '\n' || sourceChar[locate] == '\r')
{
//对type=100为不做任何处理
tempString = "";
type = 100;
column = 1;
row++;
locate++;
}
else if (sourceChar[locate] == '\t')
{
//tempString="";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -