-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlispparser.cpp
More file actions
344 lines (321 loc) · 8.33 KB
/
lispparser.cpp
File metadata and controls
344 lines (321 loc) · 8.33 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
#include "lispparser.h"
LispParser::LispParser():hasLetter(false),hasMinusSign(false),isAllList(true)
{
}
LispParser::~LispParser()
{
}
void LispParser::resetStatus()
{
hasLetter = false;
hasMinusSign = false;
isAllList = true;
}
string LispParser::getNextToken(string textline,size_t & curIdx)
{
string strToken;
size_t strLen = textline.length();
size_t subStrLen = 0;
size_t beginIdx = curIdx;
hasLetter = false;
hasMinusSign = false;
while(curIdx < strLen)
{
// handle white space
if(isspace(textline[curIdx]))
{
if(subStrLen > 0) // if the content before white space has numalpha, current token is ended
break;
else
beginIdx = ++curIdx;
}
else if(textline[curIdx] == '(' ||
textline[curIdx] == ')' ||
textline[curIdx] == '.' )
{
if(subStrLen == 0)
{
strToken = string(1,textline[curIdx]);
++ curIdx;
}
break;
}
else if(isalnum(textline[curIdx]))
{
//ERROR: Unacceptable Symbol---letter in lower case.
if(islower(textline[curIdx]))
throw runtime_error("US_LL");
if(!hasLetter && isalpha(textline[curIdx]))
hasLetter = true;
++ curIdx;
++ subStrLen;
}
else if(textline[curIdx] == '-')
{
if(hasMinusSign)
{
throw runtime_error("IM");
}
else
{
if(beginIdx == curIdx)
hasMinusSign = true;
else
throw runtime_error("IM");
++ curIdx;
++ subStrLen;
}
}
else
{ //ERROR: Unacceptable Symbol!
throw runtime_error("US");
}
}
if(subStrLen > 0)
{
strToken = textline.substr(beginIdx,subStrLen);
// ERROR: Invalid atom which contains letter beginning with digit.
if(hasLetter && isdigit(textline[beginIdx]))
throw runtime_error("ID");
if(hasMinusSign && subStrLen == 1)
throw runtime_error("IM");
}
return strToken;
}
void LispParser::buildBinaryTree(string textline, size_t &curIdx, TreeNode *node)
{
string tk = getNextToken(textline,curIdx);
if(tk == string(".") || tk == string(")"))
{
//ERROR: missing atom or openparenthesis.
throw runtime_error("MAO");
}
else
{
if(tk == string("("))
{
TreeNode *lchild = new TreeNode();
TreeNode *rchild = new TreeNode();
node->left = lchild;
node->right = rchild;
buildBinaryTree(textline,curIdx,lchild);
//ERROR: missing dot symbol
if(getNextToken(textline,curIdx) != string("."))
throw runtime_error("MDOT");
buildBinaryTree(textline,curIdx,rchild);
if(rchild->isList)
node->isList = true;
//ERROR: missing closeparenthesis
if(getNextToken(textline,curIdx) != string(")"))
throw runtime_error("MCLO");
}
else
{
node->expr = tk;
if (tk == string("NIL"))
{
node->isList = true;
node->nodeValue.vType = BOOL_TYPE;
node->nodeValue.boolValue = false;
}
else if (tk == string("T"))
{
node->nodeValue.vType = BOOL_TYPE;
node->nodeValue.boolValue = true;
}
else if (tk.find_first_not_of("-0123456789") == string::npos)
{
node->nodeValue.vType = INT_TYPE;
node->nodeValue.intValue = atoi(tk.c_str());
}
return;
}
}
}
void LispParser::deleteBinaryTree(TreeNode *node)
{
if (node != NULL)
{
if (node->left != NULL)
{
deleteBinaryTree(node->left);
node->left = NULL;
}
if (node->right != NULL)
{
deleteBinaryTree(node->right);
node->right = NULL;
}
if (node->left == NULL && node->right == NULL)
{
delete node;
node = NULL; // this will not actually assign node to NULL !!!
}
}
}
void LispParser::checkInnerNodesList(TreeNode *node)
{
if(checkIsInnerNode(node))
{
if(!node->isList)
isAllList = false;
else
{
if(isAllList)
checkInnerNodesList(node->left);
if(isAllList)
checkInnerNodesList(node->right);
}
}
}
void LispParser::printExpr(TreeNode *node)
{
// vector<int> flags;
// testPrint(node,flags);
// cout << endl;
if(node==NULL)
return;
checkInnerNodesList(node);
if(getIsAllList())
printListExpr(node);
else
printNodeExpr(node);
}
void LispParser::printNodeExpr(TreeNode *node)
{
if(checkIsInnerNode(node))
{
cout << "(";
printNodeExpr(node->left);
cout << " . ";
printNodeExpr(node->right);
cout << ")";
}
else
{
cout << node->expr;
}
}
void LispParser::printListExpr(TreeNode *node)
{
if(checkIsInnerNode(node))
{
cout << "(";
printListExpr(node->left);
while(checkIsInnerNode(node->right))
{
node = node->right;
cout << " ";
printListExpr(node->left);
}
cout << ")";
}
else
{
cout << node->expr;
}
}
void LispParser::testPrint(TreeNode *node, vector<int> orders)
{
if(node!=NULL)
{
if(!checkIsInnerNode(node))
{
std::copy(orders.begin(),orders.end(),std::ostream_iterator<int>(std::cout<< "->" ));
cout << ":" << node->expr << " "<<endl;
}
else
{
if(node->left!=NULL)
{
orders.push_back(0);
testPrint(node->left,orders);
orders.pop_back();
}
if(node->right!=NULL)
{
orders.push_back(1);
testPrint(node->right,orders);
orders.pop_back();
}
}
}
}
void LispParser::updateIsList(TreeNode *node)
{
if(node == NULL)
return;
if(node->right != NULL)
{
if(!checkIsInnerNode(node->right) )
{
if(node->right->expr == string("NIL"))
node->right->isList = true;
else
node->right->isList = false;
}
else
{
updateIsList(node->right);
}
node->isList = node->right->isList;
}
if(node->left !=NULL)
updateIsList(node->left);
}
string LispParser::int2str(int num)
{
ostringstream oss;
oss << num;
return oss.str();
}
void LispParser::testPrintDList()
{
cout <<"Size of dlist: "<<dlistMap.size()<<endl;
for(map<string,TreeNode*>::iterator iter= dlistMap.begin(); iter != dlistMap.end(); ++iter)
{
updateIsList((*iter).second);
printExpr((*iter).second);
cout << endl;
}
}
void LispParser::testPrintAList(map<string, TreeNode*>& alistMap)
{
if(alistMap.size()==0)
return;
else
cout << "Size of alist: "<<alistMap.size()<<endl;
for(map<string,TreeNode*>::iterator iter= alistMap.begin(); iter != alistMap.end(); ++iter)
{
cout << ((*iter).first) << " ";
updateIsList((*iter).second);
printExpr((*iter).second);
cout << endl;
}
}
void LispParser::copyTree(TreeNode *node, TreeNode *node_cpy)
{
node_cpy->expr = node->expr;
node_cpy->nodeValue = node->nodeValue;
if(node->left != NULL)
{
TreeNode *lt = new TreeNode();
copyTree(node->left,lt);
node_cpy->left = lt;
}
if(node->right != NULL)
{
TreeNode *rt = new TreeNode();
copyTree(node->right,rt);
node_cpy->right = rt;
}
}
void LispParser::clearListMap(map<string, TreeNode*>& listmap)
{
for(map<string,TreeNode*>::iterator iter= listmap.begin(); iter != listmap.end(); ++iter)
{
deleteBinaryTree((*iter).second);
(*iter).second = NULL;
}
listmap.clear();
}