import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.io.*;
public class compile extends JFrame
{
private JButton jButton1 = new JButton("运行");
private JButton jButton2= new JButton("清空");
private JLabel inLabel = new JLabel("原码");
private JLabel outLabel = new JLabel("分析结果");
private TextArea inTextArea = new TextArea(15,35);
private TextArea outTextArea = new TextArea(15,35);
String[] keyWords = {"auto","break","case","char","const","continue",
"default","do","double","else","enum","float","for","if","int",
"long","rebister","return","short","signed","sizeof","static","struct",
"switch","typedef","union","unsigned","void","volatile","while","main",
"cin","cout"};//关键字数组
String[] operator = {"+","-","*","/","%","=",".",">","<","?"};//运算符
String[] sepretor = {";",":","{","}","(",")","[","]"};//分隔符
private void initListener() //事件监听器
{
ActListener actListener = new ActListener();
jButton1.addActionListener(actListener);
jButton2.addActionListener(actListener);
}
class ActListener implements ActionListener //构建事件监听器
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == jButton1)
{
porRun();
}
if(e.getSource()==jButton2)
{
inTextArea.setText("");
outTextArea.setText("");
}
}
}
private void porRun() //分析过程
{
String inText = inTextArea.getText();//定义字符串
StringTokenizer str1 = new StringTokenizer(inText," /n/t/r;+=-/"/')(}{][<>",true);//分隔符集合
int n = str1.countTokens();
String[] intext = new String[str1.countTokens()];
for(int a = 0;a<n;a++)
{
intext[a] = str1.nextToken();
}
outTextArea.append("1 为关键字 2 为标识符"+"/n"+"3 为常数 4 为运算符"+"/n"+"5 为分隔符"+"/n");
outTextArea.append("====================================/n");
for(int i=0;i<intext.length;i++)
{
outer: for(int j=0;j<keyWords.length;j++) //判断为关键字
{
if(intext[i].equals(keyWords[j]))
{
outTextArea.append(intext[i]+" 1"+"/n");
i++;
break outer;
}
}
for(int k=0;k<operator.length;k++) //判断为运算符
{
if(intext[i].equals(operator[k]))
outTextArea.append(intext[i]+" 4"+"/n");
}
for(int m=0;m<sepretor.length;m++)
{
if(intext[i].equals(sepretor[m])) //判断为分隔符
outTextArea.append(intext[i]+" 5"+"/n");
}
char[] inChar=intext[i].toCharArray();
if(Character.isDigit(inChar[0]))
outTextArea.append(intext[i]+" 3"+"/n");
if(Character.isLetter(inChar[0]))
outTextArea.append(intext[i]+" 2"+"/n");
}
}
public compile ()throws IOException//构造函数
{
super("词法分析器");
setSize(300,640);
this.setLocation(320,70);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new java.awt.FlowLayout(FlowLayout.CENTER));//流式布局
this.add(this.inLabel);
this.add(this.inTextArea);
this.add(this.outLabel);
this.add(this.outTextArea);
this.add(this.jButton1);
this.add(this.jButton2);
File file = new File("in.txt");//从原文件读
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line = in.readLine();
while(line!=null)
{
this.inTextArea.append(line+"/r/n");
line=in.readLine();
}
//this.outTextArea.enable(false);
initListener();
this.setVisible(true);
}
public static void main(String[] args)throws IOException //入口
{
compile t = new compile(); //对象的引用
}
}