-
Notifications
You must be signed in to change notification settings - Fork 1
/
statement.cpp
268 lines (244 loc) · 8.33 KB
/
statement.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
#include<string>
#include"LexicalAnalyzer.class.h"
#include"SyntacticAnalyzer.class.h"
#include"keywordlist.h"
#include"operatorlist.h"
#include"wordlist.h"
#define VALUEBUFFERLENGTH 10
SyntacticAnalyzer::SyntacticAnalyzerResult SyntacticAnalyzer::statement(LexicalAnalyzer& lexical_analyzer)
{
int type=lexical_analyzer.peeksymtype();
switch(type)
{
case IDENTTYPE: return ident(lexical_analyzer);
case CALLTYPE: return callsym(lexical_analyzer);
case BEGINTYPE: return beginsym(lexical_analyzer);
case IFTYPE: return ifsym(lexical_analyzer);
case WHILETYPE: return whilesym(lexical_analyzer);
case READTYPE: return readsym(lexical_analyzer);
case WRITETYPE: return writesym(lexical_analyzer);
}
return RIGHT;
}
SyntacticAnalyzer::SyntacticAnalyzerResult SyntacticAnalyzer::ident(LexicalAnalyzer& lexical_analyzer)
{
int value;
SyntacticAnalyzer::SyntacticAnalyzerResult res;
SyntacticAnalyzer::IntermediateCodeNode newcodenode(BECOMESOPER);
std::deque<SyntacticAnalyzer::IdentStackNode>::const_iterator iter;
std::string varname;
if(lexical_analyzer.getsymtype()!=IDENTTYPE)
{
this->errorinfo="这里应该有一个标识符,参考赋值语句的语法:X:=2;";
return ERROR;
}
else if((iter=this->ident_stack_find(lexical_analyzer.getprevsymword().c_str(),QUERY_ALL_PROC))==this->ident_stack.end())
{
this->errorinfo="找不到这个标识符哦,它确实已经定义了吗?";
return ERROR;
}
else if(iter->flag!=VAR)
{
this->errorinfo="喂喂喂!这不是变量,是常量吧!你怎么能对常量赋值呢??";
return ERROR;
}
else varname=iter->ident;
if(lexical_analyzer.getsymtype()!=BECOMESTYPE)
{
this->errorinfo="这里应该是一个赋值符号 := ,参考赋值语句的语法:X:=2;";
return ERROR;
}
if((res=expression(lexical_analyzer,value))==ERROR)
{
return ERROR;
}
else
{
newcodenode.oper1=this->tempvar.prev_temp_varname;
newcodenode.oper2=varname;
this->intermediate_code_deque_vector[this->cur_procedure_index].push_back(newcodenode);
}
}
SyntacticAnalyzer::SyntacticAnalyzerResult SyntacticAnalyzer::callsym(LexicalAnalyzer& lexical_analyzer)
{
std::string word;
if(lexical_analyzer.getsymtype()!=CALLTYPE)
{
this->errorinfo="这里应该是一个call语句,call语句的语法是这样紫滴:CALL NIMEI; 其中NIMEI是个过程的名字,事先用procedure语句定义它";
return ERROR;
}
if(lexical_analyzer.getsymtype()!=IDENTTYPE)
{
this->errorinfo="这个应该是一个标识符,否则我怎么知道你要call谁?call语句的语法是这样紫滴:CALL NIMEI; 其中NIMEI是个过程的名字,事先用procedure语句定义它";
return ERROR;
}
else if(!this->procedure_find(word=lexical_analyzer.getprevsymword()))
{
this->errorinfo="这神马过程啊,我真的不认识这货!";
return ERROR;
}
else
{
this->intermediate_code_deque_vector[this->cur_procedure_index].push_back(SyntacticAnalyzer::IntermediateCodeNode(CALLSYM,word));
}
return RIGHT;
}
SyntacticAnalyzer::SyntacticAnalyzerResult SyntacticAnalyzer::beginsym(LexicalAnalyzer& lexical_analyzer)
{
if(lexical_analyzer.getsymtype()!=BEGINTYPE)
{
this->errorinfo="这里应该有一个begin语句的吧";
return ERROR;
}
if(!statement(lexical_analyzer)) return ERROR;
while(lexical_analyzer.peeksymtype()!=ENDTYPE)
{
if(lexical_analyzer.getsymtype()!=SEMICOLONTYPE)
{
this->errorinfo="这里必须有一个分号的啊,如果BEGIN..END语句中有多于一句语句的话,每句语句都必须用分号分隔";
return ERROR;
}
if(!statement(lexical_analyzer)) return ERROR;
}
lexical_analyzer.getsymtype();
return RIGHT;
}
SyntacticAnalyzer::SyntacticAnalyzerResult SyntacticAnalyzer::ifsym(LexicalAnalyzer& lexical_analyzer)
{
bool value;
std::string point;
SyntacticAnalyzer::SyntacticAnalyzerResult res;
if(lexical_analyzer.getsymtype()!=IFTYPE)
{
this->errorinfo="这里应该是一个if语句";
return ERROR;
}
point=this->temppoint.next();
if(!(res=condition(lexical_analyzer,point,value))) return ERROR;
//这里不生成任何条件语句,因为condition会为我们完成的
if(lexical_analyzer.getsymtype()!=THENTYPE)
{
this->errorinfo="喂喂..if语句没有跟then,你这个if语句是干啥的?if语句参考语法:IF X=0 THEN X:=1;";
return ERROR;
}
if(!statement(lexical_analyzer)) return ERROR;
else if(res!=VALUE || !value) //和生成point的逻辑条件完全一致
{
this->intermediate_code_deque_vector[this->cur_procedure_index].push_back(
SyntacticAnalyzer::IntermediateCodeNode(DECLARE,point));
}
return RIGHT;
}
SyntacticAnalyzer::SyntacticAnalyzerResult SyntacticAnalyzer::whilesym(LexicalAnalyzer& lexical_analyzer)
{
bool value;
std::string point1,point2;
SyntacticAnalyzer::SyntacticAnalyzerResult res;
if(lexical_analyzer.getsymtype()!=WHILETYPE)
{
this->errorinfo="这里应该是一个while语句";
return ERROR;
}
point1=this->temppoint.next();
point2=this->temppoint.next();
this->intermediate_code_deque_vector[this->cur_procedure_index].push_back(
SyntacticAnalyzer::IntermediateCodeNode(DECLARE,point1));
if(!(res=condition(lexical_analyzer,point2,value))) return ERROR;
if(lexical_analyzer.getsymtype()!=DOTYPE)
{
this->errorinfo="这里应该有一个do的,没有do,单单一个while有神马意义呢?";
return ERROR;
}
if(!statement(lexical_analyzer)) return ERROR;
this->intermediate_code_deque_vector[this->cur_procedure_index].push_back(
SyntacticAnalyzer::IntermediateCodeNode(JUMP,point1));
if(res!=VALUE || !value) //和生成point的逻辑条件完全一致
{
this->intermediate_code_deque_vector[this->cur_procedure_index].push_back(
SyntacticAnalyzer::IntermediateCodeNode(DECLARE,point2));
}
return RIGHT;
}
SyntacticAnalyzer::SyntacticAnalyzerResult SyntacticAnalyzer::readsym(LexicalAnalyzer& lexical_analyzer)
{
int type1=lexical_analyzer.getsymtype(),type2;
std::deque<SyntacticAnalyzer::IdentStackNode>::const_iterator iter;
SyntacticAnalyzer::IntermediateCodeNode newcodenode(READSYM);
if(type1!=READTYPE)
{
this->errorinfo="这里应该是一个函数";
return ERROR;
}
if(lexical_analyzer.getsymtype()!=LPARENTYPE)
{
this->errorinfo="这是函数诶!他的语法应该是 READ(X); 你貌似漏了左括号";
return ERROR;
}
do{
if(lexical_analyzer.getsymtype()!=IDENTTYPE)
{
this->errorinfo="函数里面要有标识符的啊,他的语法应该是 READ(X,Y,Z);";
return ERROR;
}
else if((iter=this->ident_stack_find(lexical_analyzer.getprevsymword().c_str(),QUERY_ALL_PROC))==this->ident_stack.end())
{
this->errorinfo="找不到这个标识符哦,它确实已经定义了吗?";
return ERROR;
}
else if(iter->flag!=VAR)
{
this->errorinfo="喂喂喂!这不是变量,是常量吧!你怎么能对常量赋值呢??";
return ERROR;
}
else
{
newcodenode.oper1=iter->ident;
this->intermediate_code_deque_vector[this->cur_procedure_index].push_back(newcodenode);
}
}while((type2=lexical_analyzer.getsymtype())==COMMATYPE);
if(type2==RPARENTYPE) return RIGHT;
else
{
this->errorinfo="函数参数要用右括号扩起来的啊,你加右括号了吗";
return ERROR;
}
}
SyntacticAnalyzer::SyntacticAnalyzerResult SyntacticAnalyzer::writesym(LexicalAnalyzer& lexical_analyzer)
{
int value,type1=lexical_analyzer.getsymtype(),type2;
SyntacticAnalyzer::SyntacticAnalyzerResult res;
SyntacticAnalyzer::IntermediateCodeNode newcodenode(WRITESYM);
char buffer[VALUEBUFFERLENGTH];
if(type1!=WRITETYPE)
{
this->errorinfo="这里应该是一个函数";
return ERROR;
}
if(lexical_analyzer.getsymtype()!=LPARENTYPE)
{
this->errorinfo="这是函数诶!他的语法应该是 WRITE(Y); 吧,你貌似漏了左括号";
return ERROR;
}
do{
if((res=expression(lexical_analyzer,value))==ERROR)
{
return ERROR;
}
else
{
if(res==RIGHT) newcodenode.oper1=this->tempvar.prev_temp_varname;
else /*res==VALUE*/
{
sprintf(buffer,"%d",value);
newcodenode.oper1=buffer;
}
this->intermediate_code_deque_vector[this->cur_procedure_index].push_back(newcodenode);
}
}while((type2=lexical_analyzer.getsymtype())==COMMATYPE);
if(type2==RPARENTYPE) return RIGHT;
else
{
this->errorinfo="函数参数要用右括号扩起来的啊,你加右括号了吗";
return ERROR;
}
}