-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cpp
273 lines (221 loc) · 7.03 KB
/
parser.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
269
270
271
272
/**
RIGHT TO USE, DISTRIBUTE AND MODIFY
===================================
Copyright (C) 2015 Ganesh Prasad Sahoo - All Rights Reserved
You may use, distribute and modify this code under the Terms
of GNU GPL V3 License. You should have received a copy of the
GNU GPL v3 License with this file. If not please write to
or visit
http://www.gnu.org/licenses/gpl.txt
*****************************************************************************
*/
#include "parser.hpp"
using namespace std;
bool isAttrTag(KeyWord k){
return k==MULTI || k==KEY || k==DERIVED || k==COMPOSITE;
}
bool isRelnTag(KeyWord k){
return k==PARTIAL || k==COMPLETE || k==ONE || k==MANY || k==WEAK;
}
void Parser::parseEnt(){
Entity ent_;
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
if(t.type_==NAME)strcpy(ent_.name_, t.token_.name_);
else error("Invalid naming of the Entity");
if(entTbl_.table_.count(ent_.name_)!=0)
error("entity with same name already defined");
ent_.weak_ = false;
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
if(t.type_== KEYWORD && t.token_.kw_ == WEAK ){
ent_.weak_ = true;
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
}
if(t.type_ == OPERATOR && t.token_.op_ == LCURL){
parseEntAttr(ent_);
}
else if(t.type_ == OPERATOR && t.token_.op_ == SEMICOLON);
else error("expected SEMICOLON(;) or a list of Attributes");
entTbl_.insert(ent_.name_, ent_);
// entTbl_.table_.insert(make_pair<const char*, Entity>(ent_.name_, ent_));
DEBUG(cout<<"DEBUG::"<<ent_.name_<<" inserted"<<endl;)
return;
}
void Parser::parseEntAttr(Entity &ent){
Attribute a;
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
if(t.type_==KEYWORD && t.token_.kw_==ATTR){
while(t.type_==KEYWORD && t.token_.kw_==ATTR){
parseAttr(a);
ent.attr_.push_back(a);
a.destruct();
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
}
if(t.type_==OPERATOR && t.token_.op_==RCURL) return;
else error("expected RCURL(}) here");
}
else if(t.type_==OPERATOR && t.token_.op_==RCURL) return;
else error("expected RCURL(}) or a list of Attributes");
return;
}
void Parser::parseAttr(Attribute &a){
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
if(t.type_==NAME) strcpy(a.name_, t.token_.name_);
else error("Invalid naming of attribute");
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
bool composite = false;
if(t.type_==KEYWORD && isAttrTag(t.token_.kw_)){
while(t.type_==KEYWORD && isAttrTag(t.token_.kw_)){
switch(t.token_.kw_){
case MULTI:
a.type_.push_back(_MULTI);
break;
case KEY:
a.type_.push_back(_KEY);
break;
case DERIVED:
a.type_.push_back(_DERIVED);
break;
case COMPOSITE:
composite=true;
a.type_.push_back(_COMPOSITE);
break;
default: break;
}
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
}
if(composite){
if(t.type_==OPERATOR && t.token_.op_==LCURL) parseAttrAttr(a);
else error("expected list of Attributes for composite Attribute");
}
else if(t.type_==OPERATOR && t.token_.op_==SEMICOLON) return;
else error("expected SEMICOLON(;) here");
}
else if(t.type_==OPERATOR && t.token_.op_==SEMICOLON) return;
else error("expected SEMICOLON(;) or a list of Attribute tags");
return;
}
void Parser::parseAttrAttr(Attribute &ent){
Attribute a;
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
if(t.type_==KEYWORD && t.token_.kw_==ATTR){
while(t.type_==KEYWORD && t.token_.kw_==ATTR){
parseAttr(a);
ent.attr_.push_back(a);
a.destruct();
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
}
if(t.type_==OPERATOR && t.token_.op_==RCURL) return;
else error("expected RCURL(}) here");
}
else if(t.type_==OPERATOR && t.token_.op_==RCURL) return;
else error("expected RCURL(}) or a list of Attributes");
return;
}
void Parser::parseReln(){
Relation rel;
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
if(t.type_==NAME) strcpy(rel.name_, t.token_.name_);
else error("Invalid naming of Relationship");
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
if(t.type_==KEYWORD && t.token_.kw_==WEAK){
rel.weak_=true;
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
}
if(!(t.type_==KEYWORD && t.token_.kw_==FROM))
error("expect keyword FROM here");
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
if(!(t.type_==OPERATOR && t.token_.op_==LCURL))
error("expected LCURL({) here");
parseEdges(rel, true);
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
if(!(t.type_==KEYWORD && t.token_.kw_==TO))
error("expect keyword FROM here");
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
if(!(t.type_==OPERATOR && t.token_.op_==LCURL))
error("expected LCURL({) here");
parseEdges(rel, false);
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
if(!(t.type_==OPERATOR && t.token_.op_==SEMICOLON))
error("expected SEMICOLON(;) here");
diagram_.graph_.push_back(rel);
return;
}
void Parser::parseEdges(Relation &rel, bool from){
Edge ed;
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
if(t.type_==KEYWORD && t.token_.kw_==CONNECT){
while(t.type_==KEYWORD && t.token_.kw_==CONNECT){
parseEdge(ed);
if(from) rel.from_.push_back(ed);
else rel.to_.push_back(ed);
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
}
if(t.type_==OPERATOR && t.token_.op_==RCURL) return;
else error("expected RCURL(}) here");
}
else error("at least one connection required here");
return;
}
void Parser::parseEdge(Edge &ed){
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
if(t.type_==NAME) strcpy(ed.ent_, t.token_.name_);
else error("Invalid name");
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
ed.normal_=true;
ed.weak_=false;
ed.complete_=false;
ed.many_=false;
if(t.type_==KEYWORD && isRelnTag(t.token_.kw_)){
while(t.type_==KEYWORD && isRelnTag(t.token_.kw_)){
switch(t.token_.kw_){
case WEAK:
ed.weak_=true;
break;
case PARTIAL:
ed.complete_=false;
break;
case COMPLETE:
ed.complete_=true;
break;
case ONE:
ed.normal_=false;
ed.many_=false;
break;
case MANY:
ed.normal_=false;
ed.many_=true;
break;
default: break;
}
check = lex_.next(t);
if(!check) error("incomplete description of ER Diagram");
}
if(t.type_==OPERATOR && t.token_.op_==SEMICOLON) return;
else error("expected SEMICOLON(;) here");
}
else if(t.type_==OPERATOR && t.token_.op_==SEMICOLON) return;
else error("expected SEMICOLON(;) or a list of Attribute tags");
return;
}