📄 cifafenxi.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.util.regex.*;
public class CiFaFenXi extends JFrame implements ActionListener//主类
{
private JPanel pA,pB,pC,mainP;//定义容器
private JButton b1,b2;//定义按钮
private TextArea textA,textC;//定义文本
int size;
char sentenceChar[];//定义转换字符数组
int senLength = 0;
int index=0;
boolean isB=false;//判断标志
char CHAR;
String finalAccidence;
String TOKEN;
boolean ASTATE = true;
static CiFaFenXi c;
public CiFaFenXi(String title)
{
super(title);
mainPanel();
}
public static void main(String[] args)//主函数
{
c=new CiFaFenXi("词法分析");
c.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e) //关闭
{
System.exit(0);
}
}
);
c.setResizable(false);
}
private void mainPanel()//主面板
{
textA=new TextArea("",10,30,TextArea.SCROLLBARS_VERTICAL_ONLY);
textA.setEditable(true);
textC=new TextArea("",10,30,TextArea.SCROLLBARS_VERTICAL_ONLY);
textC.setEditable(false);
b1=new JButton("分析");
b1.addActionListener(this);
b2=new JButton("清除");
b2.addActionListener(this);
pA=new JPanel();
pB=new JPanel();
pC=new JPanel();
mainP=new JPanel();
pA.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
pC.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
pA.setSize(200, 100);
pC.setSize(200, 100);
pB.setSize(20,100);
pA.add(textA);
pB.add(b1);
pB.add(b2);
pC.add(textC);
mainP.setLayout(new BorderLayout());
mainP.add(pA,BorderLayout.WEST);
mainP.add(pB,BorderLayout.CENTER);
mainP.add(pC,BorderLayout.EAST);
mainP.setSize(420, 100);
getContentPane().add(mainP);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e)//事件捕获
{
if (e.getSource()==b1)
{
datainput();
//toComperEnd();
//dataoutput();
}
else if (e.getSource()==b2){
dataclear();
}
}
public void dataclear()//清屏
{
int i=0;
textA.setText("");
textC.setText("");
}
public void dataoutput(String data)//结果输出
{
textC.append(data+" ");
}
public String datainput()//数据读入并转换
{
String s=new String(textA.getText()+" ");
size=s.length();
sentenceChar = new char[size];
sentenceChar=s.toCharArray();
this.senLength = sentenceChar.length;
int i = 0;
//分析单词
while (this.index <= this.senLength) {
//state0:
this.TOKEN = "";
this.CHAR = GETBC(sentenceChar);
//进入状态判断
switch (this.CHAR) {
//judge letter
case 'a':case 'b':case 'c':case 'd':case 'e':case 'f':case 'g':case 'h':case 'i':case 'j':case 'k':
case 'l':case 'm':case 'n':case 'o':case 'p':case 'q':case 'r':case 's':case 't':case 'u':case 'v':case 'w':case 'x':case 'y':
case 'z':case 'A':case 'B':case 'C':case 'D':case 'E':case 'F':case 'G':case 'H':case 'I':case 'J':case 'K':case 'L':case 'M':
case 'N':case 'O':case 'P':case 'Q':case 'R':case 'S':case 'T':case 'U':case 'V':case 'W':case 'X':case 'Y':case 'Z':
//do
this.TOKEN = this.CONTACT(TOKEN, CHAR);
//state1
CHAR = this.GETCHAR(sentenceChar);
while (this.ISLETTER(CHAR) || this.ISDIGIT(CHAR)) {
this.TOKEN = this.CONTACT(this.TOKEN, CHAR);
CHAR = this.GETCHAR(sentenceChar);
}
this.RETRACT();
//state2
if (keyWord(TOKEN)) {
textC.append(this.TOKEN+" [关键字]"+'\n');
}
else {
textC.append(this.TOKEN+" [标志符]"+'\n');
}
//clear up token
this.TOKEN = "";
break;
//judge ditital
case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8': case '9':
//do
this.TOKEN = this.CONTACT(TOKEN, CHAR);
//state3
CHAR = this.GETCHAR(sentenceChar);
while (this.ISDIGIT(CHAR)) {
this.TOKEN = this.CONTACT(TOKEN, CHAR);
CHAR = this.GETCHAR(sentenceChar);
}
this.RETRACT();
//state4
textC.append(this.TOKEN+" [数字]"+'\n');
//clear up token
this.TOKEN = "";
break;
case '=':
//state5
this.TOKEN = this.CONTACT(TOKEN, CHAR);
textC.append(this.TOKEN+" [等号][运算符]"+'\n');
//clear up token
this.TOKEN = "";
break;
case '+':
//state6
this.TOKEN = this.CONTACT(TOKEN, CHAR);
textC.append(this.TOKEN+" [加号][运算符]"+'\n');
//clear up token
this.TOKEN = "";
break;
case '*':
//state7
this.TOKEN = this.CONTACT(TOKEN, CHAR);
textC.append(this.TOKEN+" [乘号][运算符]"+'\n');
//clear up token
this.TOKEN = "";
break;
case ',':
//state10
this.TOKEN = this.CONTACT(TOKEN, CHAR);
textC.append(this.TOKEN+" [逗号][间隔符]"+'\n');
//clear up token
this.TOKEN = "";
break;
case '(':
//state11
this.TOKEN = this.CONTACT(TOKEN, CHAR);
textC.append(this.TOKEN+" [左括号][间隔符]"+'\n');
//clear up token
this.TOKEN = "";
break;
case ')':
//state12
this.TOKEN = this.CONTACT(TOKEN, CHAR);
textC.append(this.TOKEN+" [右括号][间隔符]"+'\n');
//clear up token
this.TOKEN = "";
break;
case '{':
//state13
this.TOKEN = this.CONTACT(TOKEN, CHAR);
textC.append(this.TOKEN+" [左大括号][间隔符]"+'\n');
//clear up token
this.TOKEN = "";
break;
case '}':
//state14
this.TOKEN = this.CONTACT(TOKEN, CHAR);
textC.append(this.TOKEN+" [右大括号][间隔符]"+'\n');
//clear up token
this.TOKEN = "";
break;
case '[':
//state15
this.TOKEN = this.CONTACT(TOKEN, CHAR);
textC.append(this.TOKEN+" [左中括号][间隔符]"+'\n');
//clear up token
this.TOKEN = "";
break;
case ']':
//state16
this.TOKEN = this.CONTACT(TOKEN, CHAR);
textC.append(this.TOKEN+" [右中括号][间隔符]"+'\n');
//clear up token
this.TOKEN = "";
break;
case '.':
//state17
this.TOKEN = this.CONTACT(TOKEN, CHAR);
textC.append(this.TOKEN+" [点号][间隔符]"+'\n');
//clear up token
this.TOKEN = "";
break;
/* default:
//state19
this.TOKEN = this.CONTACT(this.TOKEN, this.CHAR);
//追加出错信息
textC.append(this.TOKEN+" [error]"+'\n');
this.ASTATE = false;
//clear up token
this.TOKEN = "";
break;*/
}
if (this.ASTATE == false) {
break;
}
}
return this.finalAccidence;
}
//以下为字符的处理方法
/////////////////////////////////////////////////////
//////////////字符方法开始///////////////////////////
/////////////////////////////////////////////////////
public char GETBC(char[] sentenceChar) {
try {
while ( (sentenceChar[this.index]) == ' ') {
this.index++;
}
this.index++;
}
catch (java.lang.ArrayIndexOutOfBoundsException e) {
return ';'; //表示此行已经结束
}
return sentenceChar[index - 1];
}
public char GETCHAR(char[] sentenceChar) {
next();
return sentenceChar[this.index - 1];
}
public void next() {
this.index++;
}
public boolean ISLETTER(char letter) { //判断是否是letter
return java.lang.Character.isLetter(letter);
}
public boolean ISDIGIT(char letter) { //判断是否是数字
return java.lang.Character.isDigit(letter);
}
public boolean keyWord(String TOKEN)
{
String[] keyWords = {"abstract","boolean","break","byte","case","catch","char","class","const","continue","default","do",
"dobule","else","extends","final","finally","float","for","goto","if","implements","import","instanceof",
"int","interface","long","native","new","package","private","protected","public","return","short","static",};//关键字数组
for(int i=0;i<keyWords.length;i++)
if(TOKEN.equals(keyWords[i]))
return true;
return false;
}
public String CONTACT(String TOKEN, char CHAR) {
String tmpS = TOKEN + String.valueOf(CHAR);
TOKEN = tmpS;
return TOKEN;
}
public void RETRACT() {
this.index--;
}
/////////////////////////////////////////////////////
//////////////字符方法结束///////////////////////////
/////////////////////////////////////////////////////
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -