📄 cifafenxiqi.java
字号:
************************package word.wordList;**********************************
//////////////////////////////////////file word.java/////////////////////////////////////
public class word {
String value;
int ID;
public int getID() {
return ID;
}
public void setID(int id) {
ID = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
//////////////////////////////////////file word.java/////////////////////////////////////
public class WordList {
//此类定义了语言单词符号种别码
word[] w = new word[30];
public WordList(){
w[0] = new word();w[0].setID(1);w[0].setValue("main");
w[1] = new word();w[1].setID(2);w[1].setValue("int");
w[2] = new word();w[2].setID(3);w[2].setValue("char");
.........................................................................//省略
w[27] = new word();w[27].setID(28);w[27].setValue("!=");
w[28] = new word();w[28].setID(29);w[28].setValue("ERROR");
}
public int Reserve(String value){
for(int i = 0 ; i<28 ; i++){
if(value.equals(w[i].getValue())){
return w[i].getID();
}
}
return 0; //返回0表示不在保留字之中。
}
}
************************package word;;**********************************
//////////////////////////////////////file basicFunction .java/////////////////////////////////////
import word.wordList.WordList;
//在此类中定义了一组全局变量和过程,将它们作为实现转换图的基本成分
public class basicFunction {
public String input=""; //输入的源程序
public char ch; //存放最新读进的源程序的字符
public String strToken=""; //存放构成单词符号的字符串
public int index=0; //存放此时搜索指示器指向的字符位置
public int index_buf; //buffer中搜索指示器指向的字符位置
basicFunction(String input){
this.input = input;
}
public char getCh() {
return ch;
}
public void setCh(char ch) {
this.ch = ch;
}
public String getInput() {
return input;
}
public void setInput(String input) {
this.input = input;
}
public String getStrToken() {
return strToken;
}
public void setStrToken(String strToken) {
this.strToken = strToken;
}
//将下一输入字符读到ch中,搜索知识器前移一个字符位置
public int GetChar(){
this.ch = this.input.charAt(index);
index++;
return 0;
}
//检查ch中的字符是否为空白。若是,则调用GetChar直至不是字符为止
public char GetBC(){
while(ch==' '||ch=='\n'||ch=='\r'){
GetChar();
}
return ch;
}
//将ch中的字符连接到strToken之后
public String Concat(){
strToken=strToken.concat(String.valueOf(ch));
return strToken;
}
//判断ch中的字符是否为字母
public boolean IsLetter(){
boolean flag=false;
if(ch>='a' && ch<='z' || ch>='A' && ch<='Z' ){
flag=true;
}
return flag;
}
//判断ch中的字符是否为数字
public boolean IsDigit(){
boolean flag=false;
if(ch>='0' && ch<='9' ){
flag=true;
}
return flag;
}
//对strToken中的字符创查找保留字表,若是则返回它的编码,否则返回0
//注:在编写保留字表的时候要从1开始编号,不能从0开始编号!
public int Reserve(){
WordList wl = new WordList();
int f = wl.Reserve(strToken);
return f; //返回0表示不在保留字之中。
}
//将搜索指示器回调一个字符位置
public void Retract(){
ch=' ';
int l = strToken.length();
if(l>1){
strToken = strToken.substring(0,l-1);
}
index--;
}
//将strToken置空
public void RetractStr(){
strToken="";
}
}
//////////////////////////////////file: lexAnalysis .java; //////////////////////////////////
import word.wordList.word;
public class lexAnalysis {
String input;
public word[] word = new word[1000];
public String getInput() {
return input;
}
public void setInput(String input) {
this.input = input;
}
public int wordAnalysis(){
int i = 0;
basicFunction bf = new basicFunction(input);
int lo = input.length();
while(bf.index<lo){
word[i] = new word();
bf.GetChar();
bf.Concat();
if(bf.IsLetter()){
int f = 0;
if(bf.index<lo){
bf.GetChar();
bf.Concat();
while(bf.IsLetter()||bf.IsDigit()){
if(bf.index<lo){
bf.GetChar();
bf.Concat();
}else{
bf.ch=' ';
f=1;
}
}
}
if(f!=1){bf.Retract();}
int m = bf.Reserve();
if(m==0){
word[i].setValue(bf.strToken);
word[i].setID(7);
}else{
word[i].setValue(bf.strToken);
word[i].setID(m);
}
i++;
bf.RetractStr();
}else if(bf.IsDigit()){
int f = 0;
if(bf.index<lo){
bf.GetChar();
bf.Concat();
}
while(bf.IsDigit()){
if(bf.index<lo){
bf.GetChar();
bf.Concat();
}else{
bf.ch=' ';
f=1;
}
}
if(f!=1){bf.Retract();}
word[i].setValue(bf.strToken);
word[i].setID(8);
i++;
bf.RetractStr();
}else if(bf.ch=='+'||bf.ch=='-'||bf.ch=='*'||bf.ch=='/'||bf.ch=='('||bf.ch==')'||
bf.ch=='['||bf.ch==']'||bf.ch=='{'||bf.ch=='}'||bf.ch==','||bf.ch==':'||bf.ch==';'){
int m = bf.Reserve();
word[i].setValue(bf.strToken);
word[i].setID(m);
i++;
bf.RetractStr();
}else if(bf.ch=='>'||bf.ch=='<'||bf.ch=='='||bf.ch=='!'){
int f = 0;
if(bf.index<lo){
bf.GetChar();
bf.Concat();
}else{
bf.index++;
bf.ch=' ';
f=1;
}
if(bf.ch!='='&&f!=1){
bf.Retract();
}
int m = bf.Reserve();
if(m==0){
word[i].setValue(bf.strToken);
}else{
word[i].setValue(bf.strToken);
word[i].setID(m);
}
i++;
bf.RetractStr();
}else if(bf.ch==' '||bf.ch=='\n'||bf.ch=='\r'){
bf.RetractStr();
}
}
return i;
}
}
*******************************package stack;*********************************
//////////////////////////////////file: StringListElement .java; //////////////////////////////////
public class StringListElement {
public String data;
public StringListElement next = null;
StringListElement(String value){
data = value;
}
StringListElement(String value,StringListElement next){
data = value;
this.next = next;
}
}
//////////////////////////////////file: StringStack .java; //////////////////////////////////
public class StringStack {
public StringListElement top = new StringListElement("");
public int lo = 0;
public String top(){
if(top!=null){
return top.data;
}else{
return "ERROR!";
}
}
public String pop(){
String result = top();
if(top!=null){
top = top.next;
lo--;
}
return result;
}
public void push(String value){
if(top==null){
top = new StringListElement(value);
lo++;
}else{
StringListElement temp = new StringListElement(value);
temp.next= top;
top = temp;
lo++;
}
}
}
*******************************package sentence;*******************************
//////////////////////////////////file: juzi .java; //////////////////////////////////
public class juzi {
public String key;
public int lo;
public String[] content = {"","","","",""};
}
//////////////////////////////////file: grammar .java; //////////////////////////////////
public class grammar {
public juzi[] gram = new juzi[27];
public grammar(){
gram[0] = new juzi(); gram[0].key = "S";gram[0].lo = 1;gram[0].content[0]="";
gram[1] = new juzi(); gram[1].key = "S";gram[1].lo = 4;gram[1].content[0]="main";
gram[1].content[1]="(";gram[1].content[2]=")";gram[1].content[3]="K";
...............................................省略
gram[26] = new juzi(); gram[26].key = "G";gram[26].lo = 1;gram[26].content[0]="!=";
}
public static void main(String[] args) {
grammar g = new grammar();
System.out.println(g.gram[0].key);
}
}
//////////////////////////////////file: AnalysisFB .java; //////////////////////////////////
public class AnalysisFB {
//此类定义了分析表中的每一个单元的数据结构
public String key;
public String input;
public int index;
}
//////////////////////////////////file: AnalysisF .java; //////////////////////////////////
public class AnalysisF {
public AnalysisFB[] A = new AnalysisFB[43];
public AnalysisF(){
A[0] = new AnalysisFB();A[0].key = "S"; A[0].input = "#"; A[0].index = 0;
A[1] = new AnalysisFB();A[1].key = "S"; A[1].input = "main"; A[1].index = 1;
A[2] = new AnalysisFB();A[2].key = "K"; A[2].input = "{"; A[2].index = 2;
...................................................//省略
A[42] = new AnalysisFB();A[42].key = "G"; A[42].input = "!="; A[42].index = 26;
}
}
//////////////////////////////////file: SentenceAnalysis .java; //////////////////////////////////
import stack.StringStack;
import word.wordList.word;
/*
*此类中是用于语法分析
*/
public class SentenceAnalysis {
public StringStack ss = new StringStack();//此堆栈是符号栈
public grammar gg = new grammar();
public AnalysisF af = new AnalysisF();
//初始化堆栈
public SentenceAnalysis(){
ss.push("#");
ss.push("S");
}
/*
*此函数是用来判断在当前栈与输入串的情况下,用哪一个产生式,返回产生式在数组中的下标
*若输入串的第一个字符与栈顶字符相同则表示可以规约,则返回-1;
*若不能过用产生式,则返回-2;
*/
public int JuProduction(word w){
int f = -2;
String top = ss.top();
System.out.println("@@@@@@@@@@"+ss.top());
String input="";
if(w.getID() == 7){
input = "ID";
}else if(w.getID()==8){
input = "NUM";
}else{
input = w.getValue();
}
if(top.equals(input)){
f = -1;
}
for(int i = 0 ; i < 43 ; i++){
if(top.equals(af.A[i].key) ){
if(input.equals(af.A[i].input) ){
f = af.A[i].index;
}
}
}
return f;
}
/*
* 此函数是分布进行语法分析
* 根据所需要的产生式对符号栈进行操作
* 返回0表示规约;返回1表示移进;否则表示文法出错
*/
public int AnalysisBasic(word w){
int f = 5;
int pro = this.JuProduction(w);
if(pro == -1){
ss.pop();
f=0;
}else if(pro==0||pro==4 ||pro==16){
ss.pop();
f=1;
}else if(pro>=0&&pro<=26){
int l = gg.gram[pro].lo;
ss.pop();
for(int j =l-1 ; j>=0 ; j--){
ss.push(gg.gram[pro].content[j]);
}
f=1;
}
return f;
}
}
*********************************packet main**********************************
此图形界面代码不再黏贴了,只是将词法分析的函数及语法分析的函数黏贴
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new java.awt.Rectangle(599,80,89,41));
jButton.setText("词法分析");
jButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String input = getJTextArea().getText().toString().trim();
System.out.print(input);
TableModel model = getJTable().getModel();
DefaultTableModel tablemodel = (DefaultTableModel)model;
int counts = tablemodel.getRowCount();
for(int k = counts-1;k>=0;k--){
tablemodel.removeRow(k);
}
lexAnalysis lex = new lexAnalysis();
lex.setInput(input);
int i = lex.wordAnalysis();
for(int j = 0 ; j<i ; j++){
Object[] obj = {"("+lex.word[j].getID()+","+lex.word[j].getValue()+")"};
tablemodel.addRow(obj);
}
}
} );
}
return jButton;
}
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new java.awt.Rectangle(599,143,89,41));
jButton1.setText("语法分析");
jButton1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String input = getJTextArea().getText().toString().trim();
// System.out.print(input);
TableModel model = getJTable2().getModel();
DefaultTableModel tablemodel = (DefaultTableModel)model;
int counts = tablemodel.getRowCount();
for(int k = counts-1;k>=0;k--){
tablemodel.removeRow(k);
}
SentenceAnalysis sa = new SentenceAnalysis();
lexAnalysis lex = new lexAnalysis();
lex.setInput(input);
int i = lex.wordAnalysis();
// System.out.println(i);
int index = 0;
int b = 0;
//用来求输入串
String inp0 ="";
for (int j = index ; j<i ; j++){
inp0= inp0 + lex.word[j].getValue();
}
inp0 = inp0+"#";
Object[] obj={"0","#S",inp0,""};
tablemodel.addRow(obj);
while(index<i){
//用于求出进行移进归并个产生式
int gra = sa.JuProduction(lex.word[index]);
String gram = "";
if(gra>=0&&gra<=26){
gram = sa.gg.gram[gra].key;
gram = gram+"-->";
int lll = sa.gg.gram[gra].lo;
for(int m = 0 ; m<lll ; m++){
gram = gram + sa.gg.gram[gra].content[m];
}
}
//逐步计算
int f = sa.AnalysisBasic(lex.word[index]);
// System.out.println(f+"%"+lex.word[index].getValue()+"%"+lex.word[index].getID());
//以下7行是用来求出当前栈中的元素
int l = sa.ss.lo;
String stack = "";
StringListElement t = sa.ss.top;
for(int k = 0 ; k<l ; k++){
stack =t.data+stack;
t = t.next;
}
if(f==0){//表示规约
index++;
//求出输入串
String inp ="";
for (int j = index ; j<i ; j++){
inp = inp + lex.word[j].getValue();
}
inp = inp+"#";
b++;
Object[] obj1={String.valueOf(b),stack,inp,gram};
tablemodel.addRow(obj1);
}else if(f==1){//表示移进
//求出输入串
String inp ="";
for (int j = index ; j<i ; j++){
inp = inp + lex.word[j].getValue();
}
inp = inp+"#";
b++;
Object[] obj1={String.valueOf(b),stack,inp,gram};
tablemodel.addRow(obj1);
}else{
getJTextArea1().setText("此输入串不是一个语句
,不符合文法!");
break;
}
}
if(index == i){
getJTextArea1().setText("该输入串是符合文法的语句!");
}
}
});
}
return jButton1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -