📄 lalr_creator.java
字号:
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import javax.swing.tree.*;
class Word{
int type; //文法符号所对应的整型编码
String value; //文法符号
int line; //所在行数
int start;
int end;
Word(){
}
Word(int type, String value, int line){
this.type = type;
this.value = value;
this.line = line;
}
Word(Word w){
type = w.type;
value = w.value;
line = w.line;
start = w.start;
end = w.end;
}
}
class KeyWord{
String word;
int code;
KeyWord(String word, int code){
this.word = word;
this.code = code;
}
}
class TableDialog
extends Frame{
TextArea source;
LinkedList tokenSet;
TableDialog(String s){
super(s);
setBounds(100, 100, 500, 500);
setVisible(false);
this.validate();
source = new TextArea(10, 20);
source.setEditable(false);
add(source, BorderLayout.CENTER);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
setVisible(false);
}
});
}
String getTokenName(int sign){
int i;
for(i = 0; i < tokenSet.size(); i++){
KeyWord kw = (KeyWord)tokenSet.get(i);
if(sign == kw.code){
return kw.word;
}
}
return null;
}
void update(Integer[][] gto, String[][] action, LinkedList ts,
LinkedList unts, int stateNum, LinkedList tokenSet){
this.tokenSet = tokenSet;
if(gto == null || action == null){
return;
}
source.setText("ACTION\n");
source.append("State | ");
int i, j;
int tab = 15;
for(i = 0; i < ts.size(); i++){
Integer tI = (Integer)ts.get(i);
String str = this.getTokenName(tI.intValue());
int len;
len = str.length();
source.append(str + "");
source.append(" | ");
}
source.append("\n");
int index = 0;
for(i = 0; i < stateNum; i++){
source.append(index + "");
for(j = 0; j < 7 - (String.valueOf(index)).length(); j++){
source.append(" ");
}
source.append("|");
index++;
for(j = 0; j < ts.size(); j++){
String token = this.getTokenName(((Integer)ts.get(j)).intValue());
char ch = token.charAt(0);
int add;
int l = token.length();
if(l > 1){
add = 2;
}
else{
add = 4;
}
if(ch < 21 || ch > 125){
tab = token.length() * 2 + 4;
}
else{
tab = token.length() + 4;
}
int k;
String str = action[i][j];
int len;
len = str.length();
for(k = 0; k < (tab - len) / 2; k++){
source.append(" ");
}
source.append(str);
for(k = 0; k < tab - len - (tab - len) / 2; k++){
source.append(" ");
}
source.append("|");
}
source.append("\n");
}
source.append("GOTO\n");
source.append("State | ");
for(i = 0; i < unts.size(); i++){
Integer tI = (Integer)unts.get(i);
String str = this.getTokenName(tI.intValue());
int len;
len = str.length();
source.append(str + "");
source.append(" | ");
}
source.append("\n");
index = 0;
for(i = 0; i < stateNum; i++){
source.append(index + "");
for(j = 0; j < 7 - (String.valueOf(index)).length(); j++){
source.append(" ");
}
source.append("|");
index++;
for(j = 0; j < unts.size(); j++){
String token = this.getTokenName(((Integer)unts.get(j)).intValue());
char ch = token.charAt(0);
if(ch < 21 || ch > 125){
tab = token.length() * 2 + 4;
}
else{
tab = token.length() + 4;
}
int k;
String str = String.valueOf(gto[i][j]);
int len;
len = str.length();
for(k = 0; k < (tab - len) / 2; k++){
source.append(" ");
}
source.append(str);
for(k = 0; k < tab - len - (tab - len) / 2; k++){
source.append(" ");
}
source.append("|");
}
source.append("\n");
}
}
}
class ResultDialog
extends Frame{
TextArea result;
ResultDialog(String s){
super(s);
setVisible(false);
setBounds(700, 100, 300, 400);
result = new TextArea("", 15, 25);
result.setEditable(false);
result.setBackground(Color.white);
add(result, BorderLayout.CENTER);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
result.setText("");
setVisible(false);
}
});
}
void setDialogBounds(int x, int y, int a, int b){
this.setBounds(x, y, a, b);
}
public void setText(String s){
result.setText(s);
}
public void append(String s){
result.append(s);
}
}
class ErrList
extends java.awt.List{
LinkedList start;
LinkedList end;
ErrList(int i){
super(i);
start = new LinkedList();
end = new LinkedList();
}
int getStart(int i){
return((Integer)start.get(i)).intValue();
}
void clear(int i){
this.clear();
start.clear();
end.clear();
}
int getEnd(int i){
return((Integer)end.get(i)).intValue();
}
void add(int start, int end){
this.start.add(new Integer(start));
this.end.add(new Integer(end));
}
}
class Mywin
extends Frame
implements ActionListener{
boolean isAcciSuc = false, isSynSuc = false;
int indexOfProcess = 0;
ErrList errList;
LinkedList wordSet, //词法分析结果
formulaSet, //文法
tokenSet, //文法符号映射表
process, //语法分析
C, //项目集族
codeList,
newErrList,
idList;
JTree tree;
String str;
ResultDialog result, helpDial, synDial;
TableDialog tabDial;
MenuBar bar;
Menu fil, help;
MenuItem open, acci, exit, hel, makeTable, tabChecker, fmulaChecker, syntax,
showErr, save, saveAs, temp, synChecker, treeChecker, semantic,
codeChecker;
LinkedList keytocode;
TextArea source;
FileDialog filedial;
String directory = null, name = null;
JSplitPane js;
Integer[][] gto;
String[][] action;
LinkedList ts;
LinkedList unts;
int stateNum;
Mywin(String s){
super(s);
wordSet = new LinkedList();
setBounds(250, 100, 450, 400);
errList = new ErrList(3);
errList.addActionListener(this);
errList.setVisible(false);
codeList = new LinkedList();
newErrList = new LinkedList();
idList = new LinkedList();
keytocode = new LinkedList();
tokenSet = new LinkedList();
process = new LinkedList();
helpDial = new ResultDialog("");
synDial = new ResultDialog("");
fmulaChecker = new MenuItem("");
synChecker = new MenuItem("");
treeChecker = new MenuItem("");
codeChecker = new MenuItem("");
showErr = new MenuItem("");
temp = new MenuItem("");
open = new MenuItem("");
save = new MenuItem("");
saveAs = new MenuItem("");
acci = new MenuItem("");
exit = new MenuItem("");
hel = new MenuItem("");
syntax = new MenuItem("");
bar = new MenuBar();
fil = new Menu("");
help = new Menu("");
semantic = new MenuItem("");
synDial.setDialogBounds(250, 300, 450, 200);
KeyWord t = new KeyWord("", -1);
keytocode.add(t);
try{
BufferedReader in = new BufferedReader(new FileReader(new File(
"KeyWord.txt")));
String st;
while((st = in.readLine()) != null){
StringTokenizer parse = new StringTokenizer(st, " ");
String temp = parse.nextToken();
int i = Integer.parseInt(parse.nextToken());
t = new KeyWord(temp, i);
keytocode.add(t);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -