-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cpp
More file actions
539 lines (447 loc) · 11.2 KB
/
Parser.cpp
File metadata and controls
539 lines (447 loc) · 11.2 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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#include <iostream>
#include "Parser.h"
#include "syscatalog.h"
using namespace lexer;
const Schema * SymbolTable::m_symbols = NULL;
Parser::Parser()
{
}
IRelationalOperator * Parser::parse(const std::string & query)
{
m_scanner.tokenize(query.c_str());
m_token = m_scanner.next();
if (m_token != EOQ)
{
return query_op();
}
}
IRelationalOperator * Parser::query_op()
{
switch (m_token)
{
case PROJECTION:
case SELECTION:
case SORT_MERGE_JOIN:
case NESTED_BLOCK_JOIN:
return relop();
case SEQUENTIAL_SCAN:
return scan();
default:
std::cerr << "Unexpected token: "
<< Scanner::description(m_token) << ". Expected scan-op | rel-op";
break;
}
}
IRelationalOperator * Parser::relop()
{
if (consume(PROJECTION))
{
if (consume(LPAREN))
{
IRelationalOperator * op = projection();
if (consume(RPAREN))
{
return op;
}
delete op;
std::cerr << "Unexpected token: '" << Scanner::description(m_token)
<< "'; Expected: '" << Scanner::description(RPAREN) << "'";
}
else
{
std::cerr << "Unexpected token: '" << Scanner::description(m_token)
<< "'; Expected: '" << Scanner::description(LPAREN) << "'";
}
}
else if (consume(SORT_MERGE_JOIN))
{
if (consume(LPAREN))
{
IRelationalOperator * op = merge_join();
if (consume(RPAREN))
return op;
delete op;
}
}
else if (consume(NESTED_BLOCK_JOIN))
{
}
else
{
std::cerr << "Unexpected token. Expected projection | selection | m-join | n-join";
}
return NULL;
}
Projection * Parser::projection()
{
IRelationalOperator * child = query_op();
if (consume(SEMI_COLON))
{
ProjectionList * list = projection_list();
Projection * p = new Projection(child, *list);
delete list;
return p;
}
else
{
delete child;
std::cerr << "Unexpected token: '" << Scanner::description(m_token)
<< "'; Expected: '" << Scanner::description(SEMI_COLON) << "'";
}
}
/*
* @syntax = merge-join(l-rel-op, r-rel-op; equi-join-clause; [projection-list])
*/
MergeJoin * Parser::merge_join()
{
IRelationalOperator * lChild = NULL;
IRelationalOperator * rChild = NULL;
lChild = query_op();
if (!consume(COMMA))
{
std::cerr << "Unexpected token: '" << Scanner::description(m_token)
<< "'; Expected: '" << Scanner::description(COMMA) << "'";
delete lChild;
return NULL;
}
rChild = query_op();
if (!consume(SEMI_COLON))
{
delete lChild;
delete rChild;
return NULL;
}
Columns * joinCols = equi_join_clause();
if (!consume(SEMI_COLON))
{
std::cerr << "Unexpected token: '" << Scanner::description(m_token)
<< "'; Expected: '" << Scanner::description(SEMI_COLON) << "'";
delete lChild;
delete rChild;
delete joinCols;
return NULL;
}
if (!match(RPAREN))
{
ProjectionList * list = projection_list();
MergeJoin * join = NULL; // TODO: uncomment new MergeJoin(lChild, rChild, m_columns, *list);
delete list;
delete joinCols;
return join;
}
else
{
MergeJoin * join = new MergeJoin(lChild, rChild, *joinCols);
delete joinCols;
return join;
}
}
SequentialScan * Parser::scan()
{
if (consume(SEQUENTIAL_SCAN) && consume(LPAREN))
{
SequentialScan * scan = sscan();
if (consume(RPAREN))
{
return scan;
}
delete scan;
std::cerr << "Unexpected token: '" << Scanner::description(m_token)
<< "'. Expected: '" << Scanner::description(RPAREN) << "'";
}
else
{
std::cerr << "Unexpected token. Expected '('";
}
}
/*
* @syntax = sscan(table[:alias]; [where-clause]; projection-list)
*/
SequentialScan * Parser::sscan()
{
if (!match(IDENTIFIER))
{
std::cerr << "Unexpected token. Expected '<table> : alias;'";
}
const Table * t = SystemCatalog::table(m_scanner.value());
std::string alias = t->name();
// consume table.
consume(m_token);
if (consume(COLON))
{
if (match(IDENTIFIER))
{
alias = m_scanner.value();
consume(m_token); // consume table-alias.
}
else
std::cerr << "Unexpected token: '" << Scanner::description(m_token)
<< "'; Expected: '" << Scanner::description(IDENTIFIER) << "'";
}
if (consume(SEMI_COLON))
{
Columns * list = projection_list();
if (consume(SEMI_COLON))
{
SequentialScan * s = new SequentialScan(t, alias, *list);
// update the symbol table
SymbolTable::scope(s->schema());
if (!match(RPAREN))
{
s->filter(where_clause(), m_columns);
}
delete list;
return s;
}
else
{
delete list;
std::cerr << "Unexected token: '" << Scanner::description(m_token)
<< "'. Expected '" << Scanner::description(IDENTIFIER) << "' or '"
<< Scanner::description(SEMI_COLON) << "'";
}
}
}
Columns * Parser::projection_list()
{
Columns * list = new Columns();
while (match(ATTRIBUTE))
{
std::string column = m_scanner.value();
int offset = column.find(".");
std::string name = column.substr(1+offset);
std::string table = column.substr(0, offset);
list->add(name, table);
consume(m_token);
if (!consume(COMMA))
{
return list;
}
}
delete list;
std::cerr << "Unexpected token: '" << Scanner::description(m_token)
<< "'; Expected '" << Scanner::description(ATTRIBUTE) << "'";
}
BooleanExpression * Parser::where_clause()
{
return boolean_expression();
}
Columns * Parser::equi_join_clause()
{
Columns * columns = new Columns();
do
{
if (!match(ATTRIBUTE))
{
std::cerr << "Unpexted token: '" << Scanner::description(m_token)
<< "'; Expected: '" << Scanner::description(ATTRIBUTE) << "'";
delete columns;
return NULL;
}
if (!columns->contains(m_scanner.value()))
{
std::string column = m_scanner.value();
int offset = column.find(".");
std::string name = column.substr(1+offset);
std::string table = column.substr(0, offset);
columns->add(name, table);
}
consume(m_token);
if (!consume(EQ))
{
std::cerr << "Unpexted token: '" << Scanner::description(m_token)
<< "'; Expected: '" << Scanner::description(EQ) << "'";
delete columns;
return NULL;
}
if (!match(ATTRIBUTE))
{
std::cerr << "Unpexted token: '" << Scanner::description(m_token)
<< "'; Expected: '" << Scanner::description(ATTRIBUTE) << "'";
delete columns;
return NULL;
}
if (!columns->contains(m_scanner.value()))
{
std::string column = m_scanner.value();
int offset = column.find(".");
std::string name = column.substr(1+offset);
std::string table = column.substr(0, offset);
columns->add(name, table);
}
consume(m_token);
}
while (consume(AND));
return columns;
}
BooleanExpression * Parser::boolean_expression()
{
std::vector<BooleanTerm *> terms;
m_columns.clear();
terms.push_back(boolean_term());
while (consume(OR))
{
terms.push_back(boolean_term());
}
BooleanExpression * e = new BooleanExpression(terms.size());
for (int i = 0; i < terms.size(); i++)
e->term(i, terms[i]);
return e;
}
BooleanTerm * Parser::boolean_term()
{
BooleanTerm * term = new BooleanTerm();
term->factor(boolean_factor());
while (consume(AND))
{
term->factor(boolean_factor());
}
return term;
}
IBooleanFactor * Parser::boolean_factor()
{
std::string l;
std::string r;
if (match(ATTRIBUTE))
{
l = m_scanner.value();
consume(m_token);
if (!logical_op())
{
return NULL;
}
IBooleanFactor::LogicalOperator op = logical_op(m_token);
consume(m_token); // consume logical operator;
token t = m_token;
std::string r = m_scanner.value();
if (consume(ATTRIBUTE) || consume(NUMBER) || consume(STRING_LITERAL))
{
field_type_t type = SymbolTable::type(l);
size_t len = 0; //
m_columns.push_back(l);
//consume(t);
if (t == ATTRIBUTE)
{
m_columns.push_back(r);
}
return boolean_factor(l, VARIABLE, r, t == ATTRIBUTE ? VARIABLE : CONSTANT,
op, type, len);
}
else
{
std::cerr << "Unexpected token: '" << Scanner::description(m_token)
<< "'; Expected: '" << Scanner::description(ATTRIBUTE)
<< "' or '" << Scanner::description(NUMBER) << "'";
}
}
else if (match(NUMBER) || match(STRING_LITERAL))
{
field_type_t type = match(NUMBER) ? INTEGER : STRING;
size_t len = 0;
consume(m_token);
if (!logical_op())
{
return NULL;
}
IBooleanFactor::LogicalOperator op = logical_op(m_token);
consume(m_token);
token t = m_token;
std::string r = m_scanner.value();
if (consume(ATTRIBUTE))
{
m_columns.push_back(r);
return boolean_factor(l, CONSTANT, r, VARIABLE, op, type, len);
}
else
{
std::cerr << "Unexpected token: '" << Scanner::description(m_token)
<< "'; Expected: '" << Scanner::description(ATTRIBUTE) << "'";
}
}
else
{
std::cerr << "Unexpected token: '" << Scanner::description(m_token)
<< "'; Expected: '" << Scanner::description(ATTRIBUTE) << "'";
}
return NULL;
}
IBooleanFactor * Parser::boolean_factor(std::string & l, Type lType,
std::string & r, Type rType,
IBooleanFactor::LogicalOperator op,
field_type_t value_type, size_t field_len)
{
if (value_type == INTEGER)
{
Operand<int> * lOperand = INT_OPERAND(l, lType);
Operand<int> * rOperand = INT_OPERAND(r, rType);
return new BooleanFactor<int>(lOperand, op, rOperand);
}
if (value_type == CHAR)
{
Operand<char> * lOperand = CHAR_OPERAND(l, lType);
Operand<char> * rOperand = CHAR_OPERAND(r, rType);
return new BooleanFactor<char>(lOperand, op, rOperand);
}
if (value_type == STRING)
{
Operand<const char *> * lOperand = new StringVariable(l, STRING, field_len);
Operand<const char *> * rOperand = STRING_OPERAND(r, rType, field_len);
return new BooleanFactor<const char *>(lOperand, op, rOperand);
}
return NULL;
}
bool Parser::logical_op()
{
switch (m_token)
{
case EQ:
case NE:
case LT:
case LE:
case GT:
case GE:
return true;
default:
std::cerr << "Unexpected token: '" << Scanner::description(m_token)
<< "'; Expected: '"
<< Scanner::description(EQ) << "' or '"
<< Scanner::description(NE) << "' or '"
<< Scanner::description(LT) << "' or '"
<< Scanner::description(LE) << "' or '"
<< Scanner::description(GT) << "' or '"
<< Scanner::description(GE) << "'";
return false;
}
}
IBooleanFactor::LogicalOperator Parser::logical_op(token t)
{
switch (t)
{
case NE:
return IBooleanFactor::NE;
case LT:
return IBooleanFactor::LT;
case LE:
return IBooleanFactor::LE;
case GT:
return IBooleanFactor::GT;
case GE:
return IBooleanFactor::GE;
case EQ:
default:
return IBooleanFactor::EQ;
}
}
bool Parser::match(token t)
{
return m_token == t;
}
bool Parser::consume(token t)
{
if (match(t))
{
m_token = m_scanner.next();
return true;
}
return false;
}