📄 duck.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
public class Duck extends JPanel{
JTextField mekey;
JTextField youkey;
JButton button;
String Keyword[]={"char","short","int","unsigned","long", "float","double","struct","union","void ",
"enum","signed","const","volatile","typedef","auto","register","static","extem","break","extem ",
"break","case ","continue","default","do","while","else","for","goto","if",
"return","switch","while","sizeof"};
Duck()
{
mekey = new JTextField(20);
button = new JButton("确定");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
int key;
String text=mekey.getText();
key=laoding(text);
if(key==0)
youkey.setText("这什么都不是");
else if(key==1)
youkey.setText("求余");
else if (key==2)
youkey.setText("这是数字(整数)");
else if(key==3)
youkey.setText("这是分隔符");
else if (key==4)
youkey.setText("数字(浮点数)");
else if (key==5)
youkey.setText("这是保留字");
else if (key==6)
youkey.setText("????????");
else if (key==7)
youkey.setText("数字呢!(科学记数发的数值)");
else if (key==8)
youkey.setText("标志符");
else if (key==9)
youkey.setText("特殊字符!");
else if (key==10)
youkey.setText("减号");
else if (key==11)
youkey.setText("加号");
else if (key==12)
youkey.setText("乘号");
else if (key==13)
youkey.setText("除号");
else if (key==14)
youkey.setText("附值符号");
else if (key==15)
youkey.setText("等号");
else if(key==16)
youkey.setText("小于");
else if (key==17)
{youkey.setText("小于等于");
}
else if (key==18)
{youkey.setText("大于");
}
else if (key==19)
{youkey.setText("大于等于");
}
else if (key==20)
{youkey.setText("逻辑与");
}
else if (key==21)
{youkey.setText("逻辑或");
}
else if (key==22)
{youkey.setText("逻辑非");
}
else if (key==101)
youkey.setText("非法的字符!");
else
youkey.setText("这是错误的,请重新输入一下!");
}
});
youkey = new JTextField(20);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.RIGHT,30,20));
panel.add(mekey);
panel.add(button);
this.setLayout(new BorderLayout());
this.add(panel,BorderLayout.NORTH);
this.add(youkey,BorderLayout.SOUTH);
}
int yugaochao(String s)
{ int key;
int i;
for(i=0;i<35;i++)
{
if(s.equals(Keyword[i]))
{ key=5;
return key;
}
}
key=8;
return key;
}
int laoding(String s)
{
char ch;
int STATE ;
int key=101;
int i=0;
int Accept=100;
int Erro=101;
int j=s.length();
STATE = 0;
if(j==0)
return 0;
while(STATE!=Accept){
if(((i==j)&&((STATE==2)||(STATE==4)||(STATE==5)||(STATE==7)||(STATE==8)||(STATE==9)||(STATE==10)))||STATE==Erro)
{
key=STATE;
if(STATE==8)
{
key=yugaochao(s);
}
STATE=Accept;
return key;
}
ch=s.charAt(i);
i++;
System.out.println(STATE);
System.out.println(key);
switch(STATE)
{
case 0:
if(ch=='-'||ch=='+')
{
STATE=1;
if((i==j)&&(ch=='-'))
return 10;
if((i==j)&&(ch=='+'))
return 11;
}
else if (Character.isDigit(ch))
{STATE=2;
}
else if (Character.isLetter(ch))
STATE=8;
else if (ch=='*'&&i==j)
return 12;
else if(((ch==' ')||(ch=='\t')||(ch=='\n'))&&(i!=j))
STATE=0;
else if (ch=='/'&&i==j)
return 13;
else if (ch=='='&&i==j)
return 14;
else if(ch=='='&&i!=j)
STATE= 9;
else if (ch=='%'&&i==j)
{
return 1;
}
else if (ch=='<'&&i==j)
{return 16;
}
else if (ch=='<'&&i!=j)
{
STATE= 10;
}
else if (ch=='>'&&i==j)
{return 18;
}
else if (ch=='>'&&i!=j)
{STATE =11;
}
else if((ch=='&')&&(i!=j))
STATE=12;
else if ((ch=='|')&&(i!=j))
{STATE=13;
}
else if (ch=='!'&&i==j)
{return 22;
}
else if (((ch==';')||(ch ==' ')||(ch==','))&&i==j)
{return 3;
}
else
STATE=Erro;
break;
case 1:
if(Character.isDigit(ch))
STATE=2;
else
STATE=Erro;
break;
case 2:
if(Character.isDigit(ch))
STATE=2;
else if (ch=='E'||ch=='e')
{ STATE=5;
}
else if(ch=='.')
{
STATE=3;
if(i==j)
STATE=Erro;}
else
STATE=Erro;
break;
case 3:
if(Character.isDigit(ch))
STATE=4;
else
STATE=Erro;
break;
case 4:
if(Character.isDigit(ch))
STATE=4;
else if (ch=='E'||ch=='e')
{ STATE=5;
}
else
STATE=Erro;
break;
case 5:
if (ch=='+'||ch=='-')
{STATE=6;
}
else if(Character.isDigit(ch))
STATE=7;
else
STATE=Erro;
break;
case 6:
if(Character.isDigit(ch))
STATE=7;
else
STATE=Erro;
break;
case 7:
if(Character.isDigit(ch))
STATE=7;
else
STATE=Erro;
break;
case 8:
if(Character.isLetter(ch)||Character.isDigit(ch))
STATE=8;
else
STATE=Erro;
break;
case 9:
if(ch=='='&&i==j)
return 15;
else
STATE=Erro;
break;
case 10:
if(ch=='='&&i==j)
return 17;
else STATE =Erro;
break;
case 11:
if(ch=='='&&i==j)
return 19;
else STATE = Erro;
break;
case 12:
if(ch=='&'&&i==j)
return 20;
else STATE=Erro;
case 13:
if(ch=='|'&&i==j)
return 21;
else STATE=Erro;
case 101:
i=j;
break;
default:
STATE=Erro;
}
}
return key;
}
public static void main(String arg[]){
JFrame frame = new JFrame("词法分析器");
frame.addWindowListener(new closewindow());
frame.getContentPane().add(new Duck());
frame.pack();
frame.show();
}
}
class closewindow extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -