forked from movchan74/LightDBMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax_analyser.cpp
84 lines (72 loc) · 2.03 KB
/
syntax_analyser.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
#include "syntax_analyser.h"
#include "syntax_tree.h"
#include "lexem.h"
#include <string>
#include <iostream>
#include <list>
#include <stack>
using namespace std;
// void printTree(SyntaxTree* cur_node, int level) {
// for (int i = 0; i < level; ++i)
// {
// cout << "\t";
// }
// cout << cur_node->node <<endl;
// for (auto it=cur_node->childs.begin(); it != cur_node->childs.end(); ++it) {
// printTree(*it, level+1);
// }
// }
bool SyntaxAnalyser::analyse(std::list<Lexem::Lexem> lexemChain) {
int state = 0;
int new_state;
SyntaxTree root;
root.setType(SyntaxTreeType::ROOT);
stack<SyntaxTree*> s;
SyntaxTree* new_node;
s.push(&root);
for (auto it=lexemChain.begin(); it != lexemChain.end(); ++it) {
if (state == ERROR)
break;
new_state = automaton[state][(int)it->getType()];
switch (state) {
case 0:
new_node = s.top()->addNewChild(SyntaxTreeType::QUERY);
s.push(new_node);
new_node = s.top()->addNewChild(SyntaxTreeType::SFW);
s.push(new_node);
new_node = s.top()->addNewChild(SyntaxTreeType::SELECT);
break;
case 1:
new_node = s.top()->addNewChild(SyntaxTreeType::SELECTLIST);
s.push(new_node);
new_node = s.top()->addNewChild(SyntaxTreeType::ATTRIBUTE);
s.push(new_node);
new_node = s.top()->addNewChild(SyntaxTreeType::PATTERN, it->getValue());
s.pop();
break;
case 2:
if (new_state == 4) {
s.pop();
new_node = s.top()->addNewChild(SyntaxTreeType::FROM);
s.push(new_node);
}
break;
case 3:
new_node = s.top()->addNewChild(SyntaxTreeType::ATTRIBUTE);
s.push(new_node);
new_node = s.top()->addNewChild(SyntaxTreeType::PATTERN, it->getValue());
s.pop();
break;
case 4:
new_node = s.top()->addNewChild(SyntaxTreeType::FROMLIST);
s.push(new_node);
new_node = s.top()->addNewChild(SyntaxTreeType::RELATION);
s.push(new_node);
new_node = s.top()->addNewChild(SyntaxTreeType::PATTERN, it->getValue());
}
state = new_state;
cout << state << endl;
}
root.printTree();
return state == SUCCESS;
}