📄 evaluateana.java
字号:
import java.util.ArrayList;
import java.util.Stack;
import java.util.StringTokenizer;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class EvaluateAna extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
public Stack<Integer> status = new Stack<Integer>();
public Stack<String> symbol = new Stack<String>();
public int[] attribute = new int[256];
Generate[] forGen = new Generate[4];
Action[] actfor = new Action[12];
Goto[] gofor = new Goto[12];
Generate[] gene = new Generate[5];
Action[] act = new Action[12];
Goto[] go = new Goto[12];
Generate[] geneJud = new Generate[5];
Action[] actJud = new Action[8];
Goto[] goJud = new Goto[8];
String[] array0={"for","E1","to","E2","until","E3","do","S1","#"};
String[] array1={"+","*","(",")","i","=","#"};
String[] array2={">","<","=","i","#"};
int ret;
int count=0;
int nextstat=100;
EvaluateAna()
{
evaTable();
judgeTbale();
forTable();
}
public void forTable()
{
forGen[0] = new Generate("S","F3 do S1");
forGen[1] = new Generate("F3","F2 until E3");
forGen[2] = new Generate("F2","F1 to E2");
forGen[3] = new Generate("F1","for E1");
//Initlize_Action
actfor[0] = new Action(0,4,0);
actfor[1] = new Action(6,5,0);
actfor[2] = new Action(4,6,0);
actfor[3] = new Action(2,7,0);
actfor[4] = new Action(1,8,0);
actfor[5] = new Action(7,9,0);
actfor[6] = new Action(5,10,0);
actfor[7] = new Action(3,11,0);
actfor[8] = new Action(2,0,3);
actfor[9] = new Action(8,0,0);
actfor[10] = new Action(6,0,1);
actfor[11] = new Action(4,0,2);
//Initlize_Goto
gofor[0] = new Goto(0,"F1",3);
gofor[0].head[1]="F2"; gofor[0].gt[1]=2;
gofor[0].head[2]="F3"; gofor[0].gt[2]=1;
}
public void evaTable()
{
if(count==1)
gene[0] = new Generate("E1","i=E");
else if(count>1&&count%2==1)
gene[0] = new Generate("S1","i=E");
else if(count%2==0)
gene[0] = new Generate("E2","i=E");
gene[1] = new Generate("E","E+E");
gene[2] = new Generate("E","E*E");
gene[3] = new Generate("E","(E)");
gene[4] = new Generate("E","i");
//Initlize_Action
act[0] = new Action(4,1,0);
act[1] = new Action(5,2,0);
act[2] = new Action(2,4,0); act[2].st[4] = 5;
act[3] = new Action(0,6,0); act[3].st[1] = 7; act[3].re[6]=0;
act[4] = new Action(2,4,0); act[4].st[4] = 5;
act[5] = new Action(0,0,4); act[5].re[0]=act[5].re[1]=act[5].re[3]= act[5].re[6]=4;
act[6] = new Action(2,4,0); act[6].st[4] = 5;
act[7] = new Action(2,4,0); act[7].st[4] = 5;
act[8] = new Action(0,6,0); act[8].st[1] = 7; act[8].st[3] = 11;
act[9] = new Action(0,0,1); act[9].st[1] = 7;
act[9].re[3] = 1; act[9].re[6] = 1;
act[10] = new Action(0,0,2); act[10].re[1] = 2; act[10].re[6] = 2;
act[11] = new Action(0,0,3); act[11].re[1] = 3;
act[11].re[3] = 3; act[11].re[6] = 3;
//Initlize_Goto
go[2] = new Goto(1,"E",3);
go[4] = new Goto(1,"E",8);
go[6] = new Goto(1,"E",9);
go[7] = new Goto(1,"E",10);
}
public void judgeTbale()
{
geneJud[0] = new Generate("E3","iEi");
geneJud[1] = new Generate("E",">");
geneJud[2] = new Generate("E","<");
geneJud[3] = new Generate("E",">=");
geneJud[4] = new Generate("E","<=");
//Initlize_Action
actJud[0] = new Action(3,1,0);
actJud[1] = new Action(0,3,0); actJud[1].st[1]=4;
actJud[2] = new Action(3,5,0);
actJud[3] = new Action(2,6,0); actJud[3].re[3] = 1;
actJud[4] = new Action(2,7,0); actJud[4].re[3] = 2;
actJud[5] = new Action(4,0,0);
actJud[6] = new Action(3,0,3);
actJud[7] = new Action(3,0,4);
//Initlize_Goto
goJud[1] = new Goto(1,"E",2);
}
public int isOperate(String c){
String s[]={"+","-","*","/","=","(",")","<",">"};
for(int i=0;i<s.length;i++){
if(c!=null&&c.equals(s[i])){
return 1;
}
}
return 0;
}
public int isKeyword(String c){
String s1[]={"for","E1","to","E2","until","E3","do","S1"," "};
for(int i=0;i<s1.length;i++){
if(c!=null&&c.equals(s1[i])){
return 1;
}
}
return 0;
}
int index(String ch,Generate[] gen1){
int i;
if(gen1==gene)
{
for(i=0;i<7;i++){
if(ch.equals(array1[i]))
return i;
}
}
if(gen1==geneJud)
{
for(i=0;i<5;i++){
if(ch.equals(array2[i]))
return i;
}
}
if(gen1==forGen)
{
for(i=0;i<9;i++){
if(ch.equals(array0[i]))
return i;
}
}
return 0;
}
public String evuAna(String input,int num,int val1,int val2,int val3,int val4)
{
return analysize_Process(input,gene,act,go,num,val1,val2, val3,val4);
}
public String judAna(String input,int num,int val1,int val2,int val3,int val4)
{
return analysize_Process(input,geneJud,actJud,goJud,num,val1,val2,val3,val4);
}
public String forAna(String input,int num,int val1,int val2,int val3,int val4)
{
return analysize_Process(input,forGen,actfor,gofor,num,val1,val2, val3,val4);
}
public String analysize_Process(String input,Generate[] gen1,Action[] act1,Goto[] go1,int num,int val1,int val2,int val3,int val4){
count=num;
ArrayList<String> list=new ArrayList<String>();
StringTokenizer st=new StringTokenizer(input," \n\t\r;+=-*\"\'/)(}{[]<>?:! ",true);
while(st.hasMoreTokens()){
list.add(new String(st.nextToken()));
}
String ci[]=new String[list.size()+1];
for(int e=0;e<list.size();e++){
ci[e]=list.get(e).toString();
}
ci[list.size()]="#";
status.push(0);
symbol.push("#");
int length=ci.length;
int attr = 0;
for(int i=0;i<length-1;i++){
if(isOperate(ci[i])==0){
if(isKeyword(ci[i])==0)
{
if(!ci[i].equals("i")&&!ci[i].equals("s"))
{
attribute[attr++]=Integer.valueOf(ci[i]);
attribute[attr++]=i;
}
ci[i]="i";
}
}
}
int point1=0;
attr=0;
int temp = 0;//用来临时保存变量的值
while(point1<length)
{
int col = index(ci[point1],gen1);
int sta = status.peek();
if(gen1==gene&&sta==3&&col==6){
System.out.print("赋值语句"+count+"分析success!"+"\n");
evaTable();
gen1[act1[sta].st[col]].value = temp;
return gen1[0].head;//acc
}
else if(gen1==geneJud&&sta==5&&col==4)
{
geneJud[0].value = temp;
System.out.print("关系语句分析success!"+"\n");
return gen1[0].head;//acc
}
else if(gen1==forGen&&sta==9&&col==8)
{
semanticAna(gen1[0]);
return gen1[0].head;//acc
}
else if(act1[sta].st[col]>0){//移入
status.push(act1[sta].st[col]);
symbol.push(ci[point1]);
if(symbol.peek().equals("E1"))
forGen[3].value = val1;
if(symbol.peek().equals("E2"))
forGen[2].value = val2;
if(symbol.peek().equals("E3"))
forGen[1].value = val3;
if(symbol.peek().equals("S1"))
forGen[0].value = val4;
if(isKeyword(ci[point1])==1&&ci[point1+1]!="#")
point1++;
point1++;
}
else if(act1[sta].re[col]>0){//规约
if(point1==attribute[attr+1])
{
gen1[act1[sta].st[col]].value=attribute[attr];
temp = gen1[act1[sta].st[col]].value;
}
if((point1-1)==attribute[attr+1])
{
gen1[act1[sta].st[col]].value=attribute[attr];
attr = attr+2;
temp = gen1[act1[sta].st[col]].value;
}
int m = act1[sta].re[col];
if(gen1[m].head=="E1"||gen1[m].head=="E2"||gen1[m].head=="E3"||gen1[m].head=="S1"||gen1[m].head=="E")
{
for(int i=0;i<gen1[m].gen.length();i++)
{
symbol.pop();
status.pop();
}
status.push(go1[status.peek()].gt[1]);
}
else
{
for(int i=0;i<(gen1[m].Getlen(gen1[m].gen));i++)
{
symbol.pop();
status.pop();
}
if(gen1[m].head=="F1")
status.push(go1[0].gt[0]);
else if(gen1[m].head=="F2")
status.push(go1[0].gt[1]);
else if(gen1[m].head=="F3")
status.push(go1[0].gt[2]);
semanticAna(gen1[m]);
}
symbol.push(gen1[m].head);
}
else {
System.out.print("ERROR!"+"\n");
return "ERROR!";
}
}//while
return "ERROR!";
}
public int getValue()
{
return gene[0].value;
}
public int getValue1()
{
return geneJud[0].value;
}
void emit(String arg1,String arg2,String arg3,String arg4){
System.out.append(String.valueOf(nextstat)+" "+arg1+" "+arg2+" "+arg3+" "+arg4+"\n");
nextstat++;
}
void backpatch(int s,int t)
{
s = t;
}
public void semanticAna(Generate gen2)
{
int q;
if(gen2 == forGen[3])
{
emit("i","=",String.valueOf(gen2.value),"");
//forGen[3].setPlace();
forGen[3].setChain(nextstat);
emit("goto",String.valueOf(nextstat+2),"","");
forGen[3].setCodebegin(nextstat);
}
if(gen2 == forGen[2])
{
forGen[2].setCodebegin(forGen[3].getCodebegin());
forGen[2].setPlace(forGen[3].getPlace());
emit("i","=","i+",String.valueOf(gen2.value));
backpatch(forGen[3].chain,nextstat);
}
if(gen2 == forGen[1])
{
forGen[1].setCodebegin(forGen[2].getCodebegin());
q = nextstat;
emit("if i<=",String.valueOf(gen2.value)," goto",String.valueOf((q+2)));
forGen[1].setChain(nextstat);
emit("goto",String.valueOf((q+4)),"","");
}
if(gen2 == forGen[0])
{
emit("s","=","i+",String.valueOf(gen2.value));
emit("goto",String.valueOf(forGen[1].getCodebegin()),"","");
backpatch(forGen[0].chain,forGen[1].codebegin);
forGen[0].setChain(forGen[1].chain);
emit("--"," "," "," ");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -