📄 70c7ca73a64d001a101ed6a24c807b69
字号:
import java.awt.*;
import java.awt.event.*;
public class analyse
{
static TextArea t1=new TextArea();
static TextArea t2=new TextArea();
static String save[]={"program","type","var","procedure","begin","end","array","of"
,"record","if","then","else","fi","while","do","endwh","read","write","return",
"integer","char"};
static String fj[]={"+","-","*","/","(",")",".","[","]",";",":",",","<","=","{","}"};
static String message[]={"PLUS","MINUS","TIMES","OVER","LPAREN","RPAREN","DOT","LMIDPAREN","RMIDPAREN","SEMI","COLON","COMMA","LT","EQ","ENDFILE"};
public static void main(String[] args)
{
analyse a1=new analyse();
Frame f=new Frame();
Button b=new Button("词法分析");
Panel p1=new Panel();
Panel p2=new Panel();
// TextArea t1=new TextArea();
// TextArea t2=new TextArea();
f.setLayout(new BorderLayout());
f.add("North",p1);
f.add("Center",p2);
f.addWindowListener(new wla());
p1.setLayout(new GridLayout(1,2));
p2.setLayout(new GridLayout(1,2));
p1.add(b);
p2.add(t1);
//analyse.t1.setText("请输入SNL");
p2.add(t2);
b.addActionListener(new ActionListener()
{
String ha=new String();
String h=new String();
int n=0; //判断位
// Node no=new Node();
tklist tk=new tklist();
idlist id=new idlist();
public void actionPerformed(ActionEvent e)
{
ha=t1.getText();
for(int i=0;i<ha.length();i++)
{
switch (ha.charAt(i))
{
case ' ':
h=ha.substring(n,i);
n=i;
for(int i1=0;i1<save.length;i1++)
{
if(h.equals(save[i1]))
{
tk.insert(tk);
tk.zl=1;
tk.wz=i1;
// t2.setText(""+tk.zl+tk.wz);
}
else
{
id.insert(id);
id.c=h;
}
}
break;
case '\n':
++tk.line;
h=ha.substring(n,i-1);
for(int i1=0;i1<fj.length;i1++)
{
if(h.equals(fj[i1]))
break;
}
for(int i1=0;i1<save.length;i1++)
{
if(h.equals(save[i1]))
{
tk.insert(tk);
++tk.line;
tk.zl=1;
tk.wz=i1;
}
else if(!h.equals(save[i1])&&i1==(save.length-1))
id.insert(id);
id.c=h;
t2.setText(""+tk.line);
}
break;
}
}
}
});
f.setSize(200,160);
f.setVisible(true);
}
}
class Node
{
Object data;
Node next;
Node(Object d)
{
data=d;
next=null;
}
}
class Nodelist
{
private Node Head=null;
private Node Tail=null;
private Node Pointer=null;
private int Length=0;
private Node cursor()//获取当前结点
{
if(Head==null)
throw new java.lang.NullPointerException();
else if(Pointer==null)
return Head;
else
return Pointer.next;
}
public void insert(Object d)
{
Node e=new Node(d);
if(Length==0)
{
Head=e;
Tail=e;
}
else
{
Node temp=cursor();
e.next=temp;
if(Pointer==null)
Head=e;
else
Pointer.next=e;
}
Length++;
}
}
class tklist extends Nodelist
{
int line=1;
int zl=0;
int wz=0;
/*
tklist(Object d)
{
line=0;
zl=0;
wz=0;
}*/
}
class idlist extends Nodelist
{
String c;
}
class wla extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -