-
Notifications
You must be signed in to change notification settings - Fork 0
/
y.cpp
287 lines (275 loc) · 6.94 KB
/
y.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <ctype.h>
void A(); // A -> V:=E
void E(); // E -> TE'
void T(); // T -> FT'
void E1(); // E'-> +TE'|-TE'|null
void T1(); // T'-> *FT'|/FT'|null
void F(); // F -> V|(E)
void V(); // V -> a|b|c|d|e...|z
void qingkong();//初始化全局变量
char s[50],n='1'; //s[50]用于存放输入的赋值表达式
char Table[50][3]; //产生中间代码所需的符号表
int SIGN,sym,leap=0; //sym为s[50]中当前读入符号的下标,sym比当前输出字符多一位
int list_n=0; //符号表的下标
/*消除左递归及回溯
A -> V:=E
E -> TE'
E'-> +TE'|-TE'|null
T -> FT'
T'-> *FT'|/FT'|null
F -> V|(E)
V -> a|b|c|d|e...|z
*/
int main()
{
qingkong();
scanf("%s",s);
A();
system("pause");
return 0;
}
void qingkong()
{
leap=0;
SIGN=0;
}
void A() // A -> V:=E,E规约完再规约:=
{
V();
if(s[sym]==':'&&s[sym+1]=='=') //判断赋值号是否有拼写错误
{
sym+=2;
E();
printf("%s:=%s",Table[list_n-2],Table[list_n-1]);
printf(" (:=,%s,-,%s)\n",Table[list_n-1],Table[list_n-2]);
}
}
void V() // V -> a|b|c|d|e...|z,读入字符
{
if(islower(s[sym]))
{
Table[list_n][0] = s[sym]; //把读取的小写字母存入符号表,便于分析是生成中间代码
Table[list_n][1] = '\0'; //动态构造字母表二维数组
list_n++;
sym++;
}
}
void E() //E -> TE',消除左递归
{
if(SIGN==0)
{
T();//消除左递归第一步,先规约*,/号
E1();//消除左递归第二步,规约+,-号
}
}
void T() // T -> FT'
{
if(SIGN==0)
{
F();//先算()号
T1();//规约*,/号,
}
}
void E1() //E'-> +TE'|-TE'|null
{
int p;
if(SIGN==0)
{
if(s[sym] == '+'||s[sym]=='-')
{
p=sym; //用p记录出现'+'或'-'时sym的值
sym++;
T();
char ch[3];
ch[0] = 'T';
ch[1] = n;
ch[2] = '\0';
if(s[p] == '+')
{
printf("%s:=%s+%s",ch,Table[list_n-2],Table[list_n-1]); //输出三地址代码
printf(" (+,%s,%s,%s)\n", Table[list_n-2],Table[list_n-1],ch); //输出四元式
}
else
{
printf("%s:=%s-%s",ch,Table[list_n-2],Table[list_n-1]); //输出三地址代码
printf(" (-,%s,%s,%s)\n", Table[list_n-2],Table[list_n-1],ch); //输出四元式
}
strcpy(Table[list_n-2],ch); //将当前结果归结式放在符号表中
list_n--;
n++;
E1();
}
}
}
void T1() // T'-> *FT'|/FT'|null
{
int p;
if(SIGN==0)
{
if(s[sym] == '*'||s[sym]=='/')
{
p=sym;
sym++;
F();
char ch[3];
ch[0] = 'T';
ch[1] = n;
ch[2] = '\0';
if(s[p] == '*')
{
printf("%s:=%s*%s",ch,Table[list_n-2],Table[list_n-1]); //输出三地址代码
printf(" (*,%s,%s,%s)\n", Table[list_n-2],Table[list_n-1],ch);//输出四元式
}
else
{
printf("%s:=%s/%s",ch,Table[list_n-2],Table[list_n-1]); //输出三地址代码
printf(" (/,%s,%s,%s)\n", Table[list_n-2],Table[list_n-1],ch);//输出四元式
}
strcpy(Table[list_n-2],ch); //将当前结果归结式放在符号表中
list_n--;
n++;
T1();
}
}
}
void F() //F -> V|(E)
{
if(SIGN==0)
{
if(s[sym]=='(')
{
sym++;
E();
if(s[sym]==')')
{
sym++;
}
else
{
printf("ERROR!出错\n");
SIGN=1;leap=1;
main();
exit(0);
}
}
else if(islower(s[sym])) //判断s[sym]是否是小写字母
V();
else
{
printf("ERROR!出错\n");
SIGN=1;leap=1;
main(); exit(0);
}
}
}
/*x:=a+b*c/d-(e+f)*/
/*
T1:=b*c (*,b,c,T1)
T2:=T1/d (/,T1,d,T2)
T3:=a+T2 (+,a,T2,T3)
T4:=e+f (+,e,f,T4)
T5:=T3-T4 (-,T3,T4,T5)
x:=T5 (:=,T5,-,x)
*/
/*值的信息,符号表,类型,出错处理
简单赋值语句
递归下降分析法的原理是利用函数之间的递归调用模拟语法树自上而下的构造过程,具体实现方法概述如下:
1)每个非终结符对应一个解析函数;
2)产生式右侧为该产生式左侧非终结符所对应解析函数的“函数体”;
3)产生式右侧终结符对应从输入串中“消耗”该终结符的操作;
4)产生式中的‘|’对应函数体中的“if-else”语句;
5)对于扩展的BNF文法,产生式中的‘{}’对应函数体中的“while”语句。*/
/*#include<stdio.h>
#include<stdlib.h>
#define MAX 100
void SET_Mul_Div(int i,int m);
void SET_Add_Sub(int j,int m);
void print();
int m=0;
int count=0;
char tempvar='A';
int temp;
char string[MAX]; //用于存放表达式
int main() {
int p[MAX];
char ch;
int c=-1,q=0;
printf("请输入赋值语句:\n");
while((ch = getchar())!='\n') {
string[m++]=ch;
if(ch=='='||ch=='+'||ch=='-'||ch=='*'||ch=='/')count++;
else if(ch=='(') {
p[++c]=m-1; }
else if(ch==')'){
q=m-1;
SET_Mul_Div(p[c],q); //从左括号处理到又括号
SET_Add_Sub(p[c],q);
temp=(int)tempvar-1;
tempvar=(char)temp;
string[p[c]]=string[m-1]=tempvar;
c--;
temp=(int)tempvar+1;
tempvar=(char)temp; }}
/********调用生成四元式的函数********/
/*print();
/*********判断是否成功**********/
/*if(count==0)
printf("Successful!\n");
else
printf("Wrong!");
printf("\n");
system("pause");
}
void SET_Mul_Div(int i,int m) //处理乘除运算
{for(i++;i<=m-1;i++){
if(string[i]=='*'||string[i]=='/') {
printf("(%c %c %c %c)\n",string[i],string[i-1],string[i+1], tempvar);
string[i-1]=string[i]=string[i+1]=tempvar;
count--;
temp=(int)tempvar+1;
tempvar=(char)temp;}}}
void SET_Add_Sub(int j,int m) //处理加减运算
{j++;
for(;j<=m-1;j++){
if(string[j]=='+'||string[j]=='-') {
printf("(%c %c %c %c)\n",string[j],string[j-1],string[j+1],tempvar);
string[j-1]=string[j]=string[j+1]=tempvar;
count--;
temp=(int)tempvar+1;
tempvar=(char)temp; }}}
/*打印*/
/*void print(){
int i;
for(i=0;i<=m-1;i++)//处理乘除运算
{
if(string[i]=='*'||string[i]=='/')
{printf("(%c %c %c %c)\n",string[i],string[i-1],string[i+1],tempvar);
string[i-1]=string[i]=string[i+1]=tempvar;
count--;
temp=(int)tempvar+1;
tempvar=(char)temp;
}
}
int j;
for(j=0;j<=m-1;j++)//处理加减运算
{
if(string[j]=='+'||string[j]=='-')
{
printf("(%c %c %c %c)\n",string[j],string[j-1],string[j+1],tempvar);
string[j-1]=string[j]=string[j+1]=tempvar;
count--;
temp=(int)tempvar+1;
tempvar=(char)temp;
}}
int k;
for(k=0;k<=m-1;k++) //处理赋值运算
{
if(string[k]=='='){
temp=(int)tempvar--;
tempvar=(char)temp;
printf("(%c %c %c) \n",string[k],string[k+1], string[k-1],tempvar);
count--;
string[k-1]=tempvar; }}} */