📄 wordanalyze.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class WordAnalyze extends JFrame{
ArrayList<String> wordList=new ArrayList<String>();
//char [] operators={'=','+','-','*','/','(',')','{','}',',',';','#','[',']','<','>','\"','.'};
StringBuffer temp=new StringBuffer();
String origin=null;
char ch=' ';
int index=0;
Properties keywords=new Properties();
Properties operators=new Properties();
JToolBar toolbar=new JToolBar();
int strLength=0;
JTextArea textArea;
DefaultTableModel tableModel;
JTable table;
JSplitPane split;
WordAnalyze() {
super("--简易C语言词法分析器--");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int x,y;
Dimension size=Toolkit.getDefaultToolkit().getScreenSize();
x=(size.width-400)/2;
y=(size.height-400)/2;
setSize(450,400);
setLocation(x,y);
textArea=new JTextArea();
textArea.setForeground(Color.BLUE);
textArea.setLineWrap(true);
String [] columnNames={"单词","编码","属性"};
tableModel=new DefaultTableModel();
table=new JTable(tableModel);
table.setForeground(Color.BLUE);
tableModel.addColumn("单词");
tableModel.addColumn("编码");
tableModel.addColumn("说明");
split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,new JScrollPane(textArea),new JScrollPane(table));
toolbar.add(new analyze("analyze",new ImageIcon("images\\analyze.gif")));
toolbar.add(new quit("quit",new ImageIcon("images\\quit.gif")));
getContentPane().add(toolbar,BorderLayout.NORTH);
getContentPane().add(split);
split.setDividerLocation(210);
split.setResizeWeight(1);
}
class analyze extends AbstractAction {
public analyze(String text, Icon icon) {
super(text, icon);
}
public void actionPerformed(ActionEvent e) {
origin=textArea.getText();
tableModel.setRowCount(0);
strLength=origin.length();
wordList.clear();
loadKeywordAndOperator();
if(analyze())
showResault();
}
}
class quit extends AbstractAction {
public quit(String text, Icon icon){
super(text, icon);
}
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
void showResault() {
//textArea.setText(Arrays.toString(wordList.toArray()));
Object [] wordArr=wordList.toArray();
String row[]=new String [3];
String code=null;
for(int i=0;i<wordArr.length;i++){
row[0]=(String)wordArr[i];
if((code=keywords.getProperty(row[0]))!=null){
row[1]=code;
row[2]="关键字";
}
else if((code=operators.getProperty(row[0]))!=null){
row[1]=code;
row[2]="运算符/界符";
}
else if(row[0].charAt(0)>47&&row[0].charAt(0)<58){
row[1]="0";
row[2]="常数";
}
else {
row[1]="1";
row[2]="标识符";
}
tableModel.addRow(row);
}
}
void loadKeywordAndOperator() {
try {
keywords.load(new FileInputStream(new File("keywords.properties")));
operators.load(new FileInputStream(new File("operators.properties")));
}
catch(Exception e){
System.out.println("导入文件keywords.properties或operators.properties出错:\n"+e.toString());
System.exit(1);
}
}
void getChar(){
ch=origin.charAt(index);
index++;
System.out.println("index:"+index);
System.out.println("ch:"+ch);
}
boolean isLetter(){
if((ch>64&&ch<91)||(ch>96&&ch<123))
return true;
else
return false;
}
boolean isDigit(){
if(ch>47&&ch<58)
return true;
else
return false;
}
boolean isOperator(){
if(operators.getProperty(String.valueOf(ch))!=null)
return true;
return false;
}
void wordAdd(){
wordList.add(temp.toString());
temp.delete(0,temp.length());
}
boolean analyze(){
index=0;
while(index<strLength){
getChar();
if(isLetter()){
while(isLetter()||isDigit()){
temp.append(ch);
if(index<strLength)
getChar();
else
break;
}
wordAdd();
if((index<strLength+1)&&isOperator())
index--;
}
else if(isDigit()){
while(isDigit()){
temp.append(ch);
if(index<strLength)
getChar();
else
break;
}
wordAdd();
if((index<strLength+1)&&isOperator())
index--;
}
else if(isOperator()) {
temp.append(ch);
wordAdd();
}
else {
if(ch!=' '&&ch!='\n'&&ch!='\r'&&ch!='\t') {
JOptionPane.showMessageDialog(this,"无法识别或暂不支持符号:"+ch,"错误!",JOptionPane.ERROR_MESSAGE);
return false;
}
}
}
return true;
}
public static void main(String [] args) {
new WordAnalyze().setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -