This repository has been archived by the owner on Feb 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC语言声明分析器.c
300 lines (299 loc) · 5.03 KB
/
C语言声明分析器.c
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
288
289
290
291
292
293
294
295
296
297
298
299
//cdecl.c --madeby Alex Jas de Brad and Steve Green
//
#include <evest.h>
#define ST_ST_TYPE void
#define ST_ST_MARG
#include <steve.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <windows.h>
#define MAXTOKENS 100
#define MAXTOKENLEN 64
#define pop (stack[top--])
#define push(s) (stack[++top]=s)
int leixing;
int yige;
int boolinline;
int boolvirtual;
enum type_tag {IDENTIFIER,QUALIFIER,TYPE};
struct token
{
char type;
char string[MAXTOKENLEN];
};
int top=-1;
struct token stack[MAXTOKENS];
struct token this;
enum type_tag classify_string(void)
{
char *s=this.string;
if(!strcmp(s,"extern"))
{
strcpy(s,",其具有外部链接");
return QUALIFIER;
};
if(!strcmp(s,"inline"))
{
boolinline=1;
strcpy(s,"");
return QUALIFIER;
};
if(!strcmp(s,"namespace")&&leixing)
{
strcpy(s,"名称空间");
leixing=0;
return QUALIFIER;
};
if(!strcmp(s,"virtual"))
{
boolvirtual=1;
strcpy(s,"");
return QUALIFIER;
};
if(!strcmp(s,"const"))
{
strcpy(s,"只读");
return QUALIFIER;
};
if(!strcmp(s,"volatile"))
{
strcpy(s,"易变");
return QUALIFIER;
};
if(!strcmp(s,"signed"))
{
strcpy(s,"有符号");
return QUALIFIER;
};
if(!strcmp(s,"unsigned"))
{
strcpy(s,"无符号");
return QUALIFIER;
};
if(!strcmp(s,"short"))
{
strcpy(s,"短");
return QUALIFIER;
};
if(!strcmp(s,"long"))
{
strcpy(s,"长");
return QUALIFIER;
};
if(!strcmp(s,"int")&&leixing)
{
strcpy(s,"整数类型");
leixing=0;
return QUALIFIER;
};
if(!strcmp(s,"void")&&leixing)
{
strcpy(s,"空类型");
leixing=0;
return QUALIFIER;
};
if(!strcmp(s,"char")&&leixing)
{
strcpy(s,"字符类型");
leixing=0;
return QUALIFIER;
};
if(!strcmp(s,"float")&&leixing)
{
strcpy(s,"单精度浮点数类型");
leixing=0;
return QUALIFIER;
};
if(!strcmp(s,"double")&&leixing)
{
strcpy(s,"双精度浮点数类型");
leixing=0;
return QUALIFIER;
};
if(!strcmp(s,"struct")&&leixing)
{
strcpy(s,"结构体类型");
leixing=0;
return QUALIFIER;
};
if(!strcmp(s,"union")&&leixing)
{
strcpy(s,"联合体类型");
leixing=0;
return QUALIFIER;
};
if(!strcmp(s,"enum")&&leixing)
{
strcpy(s,"枚举体类型");
leixing=0;
return QUALIFIER;
};
if(!strcmp(s,"auto")&&leixing)
{
strcpy(s,"自动(未知)类型");
leixing=0;
return QUALIFIER;
};
if(!strcmp(s,"bool")&&leixing)
{
strcpy(s,"布尔数类型");
leixing=0;
return QUALIFIER;
};
return IDENTIFIER;
}
void gettoken(void)
{
char *p=this.string;
while((*p=getchar())==' ');
if(isalnum(*p))
{
while(isalnum(*++p=getchar()));
ungetc(*p,stdin);
*p='\0';
this.type=classify_string();
return ;
};
this.string[1]='\0';
this.type=*p;
return;
}
void initialize(),get_array(),get_params(),get_lparen(),get_ptr_part(),get_type(),declmain(),quit();
void (*nextstate)(void);
void stst(int a,char **b)
{
rename(b[0],"cdecl.exe");
system("title cdecl");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|BACKGROUND_INTENSITY);
system("cls");
puts("声明:本程序不进行任何语法错误检查,在声明具有语法错误时程序可能崩溃。");
declmain();
return;
}
void declmain()
{
a:
printf("请键入一个在一行内的c语言声明以供程序分析\n声明:");
nextstate=initialize;
top=-1;
yige=1;
leixing=1;
boolinline=0;
boolvirtual=0;
while(nextstate!=NULL)
if(nextstate==declmain)
goto a;
else
(*nextstate)();;;
return;
}
void initialize()
{
gettoken();
while(this.type!=IDENTIFIER)
{
push(this);
gettoken();
};
printf("“%s”是",this.string);
gettoken();
nextstate=get_array;
}
void get_array()
{
nextstate=get_params;
while(this.type=='[')
{
if(yige)printf("一个");
printf("数组,数组内容");
yige=0;
gettoken();
if(isdigit(this.string[0]))
{
printf("是从0排列至%d的,",atoi(this.string)-1);
gettoken();
};
gettoken();
printf("都是");
nextstate=get_lparen;
};
}
void get_params()
{
nextstate=get_lparen;
if(this.type=='(')
{
while(this.type!=')')
gettoken();;
gettoken();
if(yige)printf("一个");
if(boolinline)printf("内联");
if(boolvirtual)printf("虚");
printf("函数,函数返回");
boolinline=0;
boolvirtual=0;
yige=0;
};
}
void get_lparen()
{
nextstate=get_ptr_part;
if(top>=0)
{
if(stack[top].type=='(')
{
pop;
gettoken();
nextstate=get_array;
};
};
}
void get_ptr_part()
{
nextstate=get_type;
if(stack[top].type=='*')
{
if(yige)printf("一个");
printf("指针,指针内容指向");
yige=0;
pop;
nextstate=get_lparen;
}
else
if(stack[top].type==QUALIFIER)
{
printf("%s",pop.string);
nextstate=get_lparen;
};;
}
void get_type()
{
nextstate=quit;
while(top>=0)
printf("%s",pop.string);;
if(leixing)
printf("整数类型");;
puts("。");
}
void quit()
{
puts("下一个?(Y/N)");
char a;
a:
a=toupper(getchar());
if(a=='Y')
{
nextstate=declmain;
return;
};
if(a=='N')
{
nextstate=NULL;
return;
};
//puts("出错,请重新输入。");
goto a;
}
//