1 # include <stdio.h> 2 # include <string.h> 3 4 //存儲LR(0)分析表 5 struct node 6 { 7 char ch; 8 int num; 9 }; 10 struct node table[]={ 11 12 {'s',5},{'t',0},{'t',0},{'s',4},{'t',0},{'t',0},{'t',1},{'t',2},{'t',3}, 13 {'t',0},{'s',6},{'t',0},{'t',0},{'t',0},{'a',0},{'t',0},{'t',0},{'t',0}, 14 {'t',0},{'r',2},{'s',7},{'t',0},{'r',2},{'r',2},{'t',0},{'t',0},{'t',0}, 15 {'t',0},{'r',4},{'r',4},{'t',0},{'r',4},{'r',4},{'t',0},{'t',0},{'t',0}, 16 {'s',5},{'t',0},{'t',0},{'s',4},{'t',0},{'t',0},{'t',8},{'t',2},{'t',3}, 17 {'t',0},{'r',6},{'r',6},{'t',0},{'r',6},{'r',6},{'t',0},{'t',0},{'t',0}, 18 {'s',5},{'t',0},{'t',0},{'s',4},{'t',0},{'t',0},{'t',0},{'t',9},{'t',3}, 19 {'s',5},{'t',0},{'t',0},{'s',4},{'t',0},{'t',0},{'t',0},{'t',0},{'t',10}, 20 {'t',0},{'s',6},{'t',0},{'t',0},{'s',11},{'t',0},{'t',0},{'t',0},{'t',0}, 21 {'t',0},{'r',1},{'s',7},{'t',0},{'r',1},{'r',1},{'t',0},{'t',0},{'t',0}, 22 {'t',0},{'r',3},{'r',3},{'t',0},{'r',3},{'r',3},{'t',0},{'t',0},{'t',0}, 23 {'t',0},{'r',5},{'r',5},{'t',0},{'r',5},{'r',5},{'t',0},{'t',0},{'t',0}, 24 }; 25 26 //符號棧以及狀態棧 27 struct node1 28 { 29 int pop; 30 int data[10]; 31 char str[10]; 32 }q1; 33 34 int total=0; //步驟 35 int i; //輸入串下標 36 int function(int a,char c,int temp); 37 38 39 int main() 40 { 41 42 char ch[20];//存儲輸入串 43 44 //棧初始化 45 q1.data[0]=0; 46 q1.pop=1; 47 q1.str[0]='#'; 48 49 i=0; 50 int temp; //下標轉換 51 int aaa; //函數返回值,0代表輸入串成功分析,1代表出錯或者接受 52 53 gets(ch); //輸入串; 54 55 // 輸出表頭和初始化的狀態 56 printf("步驟\t狀態棧\t符號棧\t輸入串\t動作\n"); 57 printf("%d\t%d\t#\t%s\t",++total,q1.data[0],ch); 58 59 60 //循環輸出中間過程 61 while(1) 62 { 63 temp=0; 64 //面臨不同的輸入串采取不同的動作,由函數function實現 65 if(ch[i] == 'i') 66 { 67 temp=q1.data[q1.pop-1]*9+0; 68 aaa=function(0,'i',temp); 69 70 } 71 else if(ch[i] == '+') 72 { 73 temp=q1.data[q1.pop-1]*9+1; 74 aaa=function(1,'+',temp); 75 76 } 77 else if(ch[i] == '*') 78 { 79 temp=q1.data[q1.pop-1]*9+2; 80 aaa=function(2,'*',temp); 81 82 } 83 else if(ch[i] == '(') 84 { 85 temp=q1.data[q1.pop-1]*9+3; 86 aaa=function(3,'(',temp); 87 88 } 89 else if(ch[i] == ')') 90 { 91 temp=q1.data[q1.pop-1]*9+4; 92 aaa=function(4,')',temp); 93 94 } 95 else if(ch[i] == '#') 96 { 97 temp=q1.data[q1.pop-1]*9+5; 98 aaa=function(5,'#',temp); 99 100 } 101 if(aaa==0) 102 break; 103 104 //輸出 105 printf("%c%d",table[temp].ch,table[temp].num); 106 printf("\n"); 107 q1.str[q1.pop]='\0'; 108 printf("%d\t",++total); 109 for(int k=0;k<q1.pop;k++) 110 { 111 printf("%d",q1.data[k]); 112 } 113 printf("\t"); 114 printf("%s\t",q1.str); 115 for(k=i;k<strlen(ch);k++) 116 printf("%c",ch[k]); 117 printf("\t"); 118 } 119 return 0; 120 } 121 122 123 int function(int a,char c,int temp) 124 { 125 126 temp=q1.data[q1.pop-1]*9+a; 127 if(table[temp].ch=='s') 128 { 129 q1.data[q1.pop]=table[temp].num; 130 q1.str[q1.pop++]=c; 131 i++; 132 } 133 if(table[temp].ch=='r') 134 { 135 if(table[temp].num == 1) 136 { 137 int leag,flag; 138 q1.pop=q1.pop-3; 139 q1.str[q1.pop]='E'; 140 leag=q1.data[q1.pop-1]*9+6; 141 q1.data[q1.pop]=table[leag].num; 142 q1.pop++; 143 } 144 else if(table[temp].num == 2) 145 { 146 int leag,flag; 147 q1.pop=q1.pop-1; 148 q1.str[q1.pop]='E'; 149 leag=q1.data[q1.pop-1]*9+6; 150 q1.data[q1.pop]=table[leag].num; 151 q1.pop++; 152 } 153 else if(table[temp].num == 3) 154 { 155 int leag,flag; 156 q1.pop=q1.pop-3; 157 q1.str[q1.pop]='T'; 158 leag=q1.data[q1.pop-1]*9+7; 159 q1.data[q1.pop]=table[leag].num; 160 q1.pop++; 161 } 162 else if(table[temp].num == 4) 163 { 164 int leag,flag; 165 q1.pop=q1.pop-1; 166 q1.str[q1.pop]='T'; 167 leag=q1.data[q1.pop-1]*9+7; 168 q1.data[q1.pop]=table[leag].num; 169 q1.pop++; 170 } 171 else if(table[temp].num == 5) 172 { 173 int leag,flag; 174 q1.pop=q1.pop-3; 175 q1.str[q1.pop]='F'; 176 leag=q1.data[q1.pop-1]*9+8; 177 q1.data[q1.pop]=table[leag].num; 178 q1.pop++; 179 } 180 else if(table[temp].num == 6) 181 { 182 int leag,flag; 183 q1.pop=q1.pop-1; 184 q1.str[q1.pop]='F'; 185 leag=q1.data[q1.pop-1]*9+8; 186 q1.data[q1.pop]=table[leag].num; 187 q1.pop++; 188 } 189 else 190 { 191 printf("出錯\n"); 192 return 0; 193 } 194 } 195 if(table[temp].ch=='a') 196 { 197 printf("接受!\n"); 198 return 0; 199 } 200 return 1; 201 }