-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.cpp
More file actions
490 lines (454 loc) · 12 KB
/
create.cpp
File metadata and controls
490 lines (454 loc) · 12 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#include "create.h"
using namespace std;
extern Token lookahead; //the ahead token
extern std::vector<Token> tokens;//all tokens of each sentence
int tcount; //current index of tokens
string tableName; //name of a table
string tempColName; //name of a column
string primaryKey; // one of the primary keys
vector<string> pKeys; //all primary keys in a table
bool hasPrimaryKey; //judge if the table has any primary key
bool hasDefault; // if a column has default:true, else false
int defaultV; // default value, if a column has no default, defaultV = 0
vector<Column> cols; // all columns in a table
vector<string> expr;// expression in default value in order
bool errorDeteced; //judge if there is an error
bool isNegative; //judge if the num is a negative
Create::Create(vector<Token> token){
tokens = token;
tcount = 0;
hasDefault = false;
defaultV = 0;
hasPrimaryKey = false;
errorDeteced = false;
isNegative = false;
tokens.clear();
pKeys.clear();
cols.clear();
expr.clear();
lookahead = getNextToken();
}
Table Create::create() {
if (create_stmt()) {
Table table(tableName, cols, pKeys);
if (detectError(table)) {//error exists
Table t("", cols, pKeys);
return t;
} else {
return table;
}
}
Table t("", cols, pKeys);
return t;
}
bool detectError(Table table) {//true indicates error found
if (cols.size() > 100) {
errorDeteced = true;
cout << "error: number of columns > 100" << endl;
return true;
}
if (pKeys.size() > 100) {
errorDeteced = true;
cout << "error: number of primary key declarations > 100" << endl;
return true;
}
vector<Column> tmp1(cols);
for (int i = 0; i < cols.size(); i++) {
//judge if there are duplicate column names.
for (int j = 0; j < cols.size(); j++) {
if (j != i) {
if (tmp1[i].getName() == tmp1[j].getName()) {
errorDeteced = true;
cout << "error: duplicate column name: \"" << tmp1[i].getName() << "\"" << endl;
return true;
}
}
}
}
vector<string> tmp2(pKeys);
sort(tmp2.begin(), tmp2.end());
//judge if primary key id is in the columns.
for (int i = 0; i < pKeys.size(); i++) {
if (!table.isColumnExist(pKeys[i])) {
errorDeteced = true;
cout << "error: primary key not found: \"" << pKeys[i] << "\"" << endl;
return true;
}
//judge if there are duplicate primary keys.
if (i == pKeys.size() - 1) { break;}
if (tmp2[i] == tmp2[i + 1]) {
errorDeteced = true;
cout << "error: primary keys duplicate: \"" << tmp2[i] << "\"" << endl;
return true;
}
}
return false; //indicate no error found
}
//print result
void Create::printTest(bool exist) {
if (errorDeteced) {
cout << "failed to create table \"" << tableName << "\"" << endl << endl;
return;
}
if (exist) {
cout << "failed to create table \"" << tableName << "\"" << endl;
cout << "error: table \"" << tableName << "\" already exist" << endl;
return;
}
if (!errorDeteced) {
cout << "SUCCESS: create table \"" << tableName << "\"" << endl;
//test example for create statment
cout << "table: " << tableName << endl;
cout << "primary keys: ";
for (int k = 0; k < pKeys.size(); k++) {
cout << pKeys[k];
if (pKeys.size() > 1 && k != pKeys.size() - 1) {
cout << " | ";
}
}
cout << endl;
cout << "|";
for (int i = 0; i < cols.size(); i++) {
cout << "------------------|";
}
cout << endl;
for (int j = 0; j < cols.size(); j++) {
int length = cols[j].getName().size() + (cols[j].getHasDefault() ? (5 + intToStr(cols[j].getDefaultValue()).size()) : 0);
cout << "|";
for (int m = 0; m < (19 - length) / 2; m++) {
cout << " ";
}
cout << cols[j].getName();
if (cols[j].getHasDefault()) {
cout << " def=" << cols[j].getDefaultValue();
}
for (int m = 0; m < (19 - (length + 1)) / 2; m++) {
cout << " ";
}
}
cout << "|" << endl;
cout << "|";
for (int i = 0; i < cols.size(); i++) {
cout << "------------------|";
}
cout << endl << endl;
} else {
cout << "failed to create table \"" << tableName << "\"" << endl << endl;
}
}
bool create_stmt() {
if (transLow(lookahead.getName()) == "create") {
match("keyword", "create");
match("keyword", "table");
tableName = lookahead.getName(); match("id", "");
match("operator", "(");
decl_list();
match("operator", ")");
match("operator", ";");
if (errorDeteced) return false;
else return true;
} else {
cout << "syntax error: can't find keyword [create]" << endl;
errorDeteced = true;
return false;
}
}
void decl_list() {
if (errorDeteced) return;
decl();
decl_list2();
}
void decl_list2() {
if (errorDeteced) return;
if (lookahead.getName() == ",") {
match("operator", ",");
decl();
decl_list2();
}
}
void decl() {
if (errorDeteced) return;
if (lookahead.getType() == "id") {
tempColName = lookahead.getName();
match("id", "");
match("keyword", "int");
default_spec();
} else if (transLow(lookahead.getName()) == "primary") {
match("keyword", "primary");
match("keyword", "key");
if (hasPrimaryKey) {
cout << "error: duplicate primary key declarations" << endl;
errorDeteced = true;
return;
}
hasPrimaryKey = true;
match("operator", "(");
column_list();
match("operator", ")");
} else {
cout << "syntax error: can't find type [id] or keyword [primary] in columns." << endl;
errorDeteced = true;
}
}
void default_spec() {
if (errorDeteced) return;
if (transLow(lookahead.getName()) == "default") {
hasDefault = true;
match("keyword", "default");
match("operator", "=");
Csimple_expr();
//set default value
defaultV = calculateRPN(RPN(expr));
expr.clear();
} else {
defaultV = 0;
hasDefault = false;
}
vector<int> value;
//create a column
Column column(tempColName, value, hasDefault, defaultV);
cols.push_back(column);
}
void Csimple_expr() {
if (errorDeteced) return;
Csimple_term();
Csimple_expr2();
}
void Csimple_expr2() {
if (errorDeteced) return;
if (lookahead.getName() == "+") {
expr.push_back("+");
match("aop", "+");
Csimple_term();
Csimple_expr2();
} else if (lookahead.getName() == "-") {
expr.push_back("-");
match("aop", "-");
Csimple_term();
Csimple_expr2();
}
}
void Csimple_term() {
if (errorDeteced) return;
Csimple_unary();
Csimple_term2();
}
void Csimple_term2() {
if (errorDeteced) return;
if (lookahead.getName() == "*") {
expr.push_back("*");
match("aop", "*");
Csimple_unary();
Csimple_term2();
} else if (lookahead.getName() == "/") {
expr.push_back("/");
match("aop", "/");
Csimple_unary();
Csimple_term2();
}
}
void Csimple_unary() {
if (errorDeteced) return;
if (lookahead.getName() == "(") {
expr.push_back("(");
match("operator", "(");
Csimple_expr();
expr.push_back(")");
match("operator", ")");
} else if (lookahead.getName() == "-") {
isNegative = true;
match("aop", "-");
Csimple_unary();
} else if (lookahead.getName() == "+") {
isNegative = false;
match("aop", "+");
Csimple_unary();
} else if (lookahead.getType() == "num") {
defaultV = lookahead.getNum() * (isNegative ? -1 : 1);
expr.push_back(intToStr(defaultV));
match("num", "");
isNegative = false;
} else {
errorDeteced = true;
cout << "syntax error: expression error" << endl;
}
}
void column_list() {
if (errorDeteced) return;
if (lookahead.getType() == "id") {
primaryKey = lookahead.getName();
match("id", "");
column_list2();
} else {
cout << "syntax error: can't find type [id] in primary keys" << endl;
errorDeteced = true;
}
}
void column_list2() {
if (errorDeteced) return;
if (lookahead.getName() == ",") {
match("operator", ",");
pKeys.push_back(lookahead.getName());
match("id", "");
column_list2();
} else {
pKeys.push_back(primaryKey);
}
}
//match the predicted token and lookahead
void match(string type, string name) {
if (errorDeteced) return;
if (type == "id") {
if (lookahead.getType() == "id") lookahead = getNextToken();
else {
cout << "syntax error: \"" << lookahead.getName() << "\" can't match type [id]" << endl;
errorDeteced = true;
}
} else if (type == "num") {
if (lookahead.getType() == "num") lookahead = getNextToken();
else {
cout << "syntax error: \"" << lookahead.getNum() << "\" can't match type [num]" << endl;
errorDeteced = true;
}
} else if (type == "keyword") {
if (lookahead.getType() == "keyword") {
if (transLow(lookahead.getName()) == name) {
lookahead = getNextToken();
} else {
cout << "syntax error: \"" << lookahead.getName() << "\" can't match keyword: \"" << name << "\"" << endl;
errorDeteced = true;
}
} else {
cout << "syntax error: \"" << lookahead.getType() << "\" can't match type [keyword]" << endl;
errorDeteced = true;
}
} else if (type == "operator") {
if (lookahead.getType() == "operator") {
if (lookahead.getName() == name) {
if (name != ";") {//indicate the last token of each line
lookahead = getNextToken();
}
} else {
cout << "syntax error: \"" << lookahead.getName() << "\" can't match operator: \"" << name << "\"" << endl;
errorDeteced = true;
}
} else {
cout << "syntax error: \"" << lookahead.getType() << "\" can't match type [operator]" << endl;
errorDeteced = true;
}
} else if (type == "aop") {
if (lookahead.getType() == "aop") {
if (lookahead.getName() == name) {
lookahead = getNextToken();
} else {
errorDeteced = true;
cout << "syntax error: \"" << lookahead.getName() << "\" can't match operator: \"" << name << "\"" << endl;
}
} else {
cout << "syntax error: \"" << lookahead.getType() << "\" can't match type [aop]" << endl;
errorDeteced = true;
}
} else if (type == "error") {
cout << "syntax error: " << "type error" << endl;
errorDeteced = true;
} else {
cout << "unfound syntax error" << endl;
errorDeteced = true;
}
}
Token getNextToken() {
return tokens[tcount++];
}
//convert infix expression to suffix expression(Reverse Polish Notation)
stack<string> RPN(vector<string> exp) {
stack<string> temp;
temp.push("#");
stack<string> rpn;//stack restoring the RPN
//scan the input expression
for (int i = 0; i < exp.size(); i++) {
if (exp[i] == "+" || exp[i] == "-") {
if (temp.top() == "(" || temp.top() == "#") {
temp.push(exp[i]);
} else {
string op = temp.top();
temp.pop();
rpn.push(op);
temp.push(exp[i]);
}
} else if (exp[i] == "*" || exp[i] == "/") {
if (temp.top() == "(" || temp.top() == "#"
|| temp.top() == "+" || temp.top() == "-") {
temp.push(exp[i]);
} else {
string op = temp.top();
temp.pop();
rpn.push(op);
temp.push(exp[i]);
}
} else if (exp[i] == "(") {
temp.push(exp[i]);
} else if (exp[i] == ")") {
while(temp.top() != "(") {
string op = temp.top();
temp.pop();
rpn.push(op);
}
temp.pop();//pop out "("
} else {//num
rpn.push(exp[i]);
}
}
while(temp.top() != "#") {
string op = temp.top();
temp.pop();
rpn.push(op);
}
return rpn;
}
//calculate rpn to get the result
int calculateRPN(stack<string> rpn) {
stack<string> rrpn;
while (!rpn.empty()) {
string op = rpn.top();
rrpn.push(op);
rpn.pop();
}
stack<string> temp;
while (!rrpn.empty()) {
if (rrpn.top() == "+" || rrpn.top() == "-" || rrpn.top() == "*" || rrpn.top() == "/") {//operator
int num1 = atoi(temp.top().c_str());
temp.pop();
int num2 = atoi(temp.top().c_str());
temp.pop();
int result = 0;
if (rrpn.top() == "+") {
result = num2 + num1;
} else if (rrpn.top() == "-") {
result = num2 - num1;
} else if (rrpn.top() == "*") {
result = num2 * num1;
} else if (rrpn.top() == "/") {
if (num1 == 0) {
cout << "error: division by 0" << endl;
errorDeteced = true;
return 0;
}
result = num2 / num1;
}
temp.push(intToStr(result));
} else {//number
temp.push(rrpn.top());
}
rrpn.pop();
}
int res = atoi(temp.top().c_str());
return res;
}
//convert type int to type string
string intToStr(int num) {
stringstream ss;
string str;
ss << num;
ss >> str;
return str;
}