-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.cpp
executable file
·380 lines (357 loc) · 9.6 KB
/
parse.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
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
/**
* \file parse.cpp
*
* Implementation of a parser that analyzes a string containing an
* s-expression, and determines its tree structure.
*/
#include "parse.hpp"
bool inparsecar;
// check whether chr is white space
bool iswhitespace(char ch)
{
if ((' ' == ch)||('\n' == ch)||('\t' == ch)||('\r' == ch)) {
return true;
} else {
return false;
}
}
/**
* \brief Check whether numericstr is an legal numericstr string
* \param str The string to be checked
* \return ture if numericstr is an legal numericstr string, false otherwise
*/
bool is_legalnumeric(string str)
{
int dotnum = 0;
int length = str.length();
int i;
if ('.' == str[0]) {
dotnum ++;
} else if ( !((str[0] >= '0') && (str[0] <= '9')) && ('+'!=str[0]) && ('-'!=str[0])) {
return false;
}
for (i = 1; i < length; i ++) {
if ('.' == str[i]) {
dotnum ++;
} else if ((str[i] < '0') || (str[i] > '9')) {
return false;
}
}
if (dotnum>1) {
return false;
}
return true;
}
/**
* \brief Check whether str is a legal operator
*
*/
bool is_legaloperator(string str)
{
return true;
}
void readsinglesymbol(string& substring, string& sexpr)
{
char currentchar;
int i = 0;
int length = substring.length();
// get the first character
currentchar = substring[i];
if (currentchar == '\"') {
// read a string literal
sexpr += currentchar;
do {
++i;
currentchar = substring[i];
sexpr += currentchar;
} while (currentchar != '\"' && i < length-1);
if ('\"' != currentchar) {
cout << "error: illegal string" << endl;
exit(1);
}
} else {
// read a numeric literal or operator
do {
sexpr += currentchar;
++i;
if (i >= static_cast<int>(substring.size())) {
break;
}
currentchar = substring[i];
} while ((!iswhitespace(currentchar)) && (currentchar != '(') && currentchar != '\"');
--i;
}
substring = substring.substr(i+1, length - i - 1);
}
/**
* \brief Clear the whitespace at the begining and end of string sexpr.
* \param sexpr The string.
*/
void clearwhitespace(string& sexpr)
{
if (sexpr.size() <= 0) {
return;
}
int leftvalidpos;
int rightvalidpos;
int length = sexpr.size();
int i;
// most left non-whitespace position
for (i = 0; i < length; ++i) {
if (iswhitespace(sexpr[i])) {
continue;
} else {
leftvalidpos = i;
break;
}
}
// most right non-whitespace position
for (i = length - 1; i >= 0; i --) {
if (iswhitespace(sexpr[i])) {
continue;
} else {
rightvalidpos = i;
break;
}
}
// delete the white space at the beginning and end
if ( i == -1 ) sexpr = "";
else sexpr = sexpr.substr(leftvalidpos, rightvalidpos - leftvalidpos + 1);
}
/**
* \brief Check whether the s-expression legal
*/
bool is_legalexpr(string sexpr)
{
clearwhitespace(sexpr);
if (sexpr.length()==0) {
cout << "blank string " << endl;
return false;
}
if (')' == sexpr[0]) {
cout << "error: illegal s-expression" << endl;
return false;
}
if ('(' == sexpr[0]) {
// it is expression
int length = sexpr.length();
int inumleftparenthesis = 1;
int i;
int quotationmark = 0;
for (i = 1; i < length; i ++ ) {
if ('\"' == sexpr[i]) {
quotationmark ++;
quotationmark = quotationmark%2;
} else if ('(' == sexpr[i] && 0 == quotationmark) {
inumleftparenthesis ++;
} else if (')' == sexpr[i] && 0 == quotationmark) {
inumleftparenthesis --;
}
if (0 == inumleftparenthesis) {
break;
}
}
if ((i < length - 1) || (i == length) || (inumleftparenthesis > 0) || 0 != quotationmark) {
cout << "error: illegal s-expression " << endl;
return false;
}
} else if ('\"' != sexpr[0]) {
// single element
if (string::npos != sexpr.find('(') || string::npos != sexpr.find(')') || string::npos != sexpr.find(' ') || string::npos != sexpr.find('\"')) {
cout << "error: illegal s-expression " << endl;
return false;
}
// check whether str is illegal numeric literal or illegal operator
if ((false == is_legalnumeric(sexpr)) && (false ==is_legaloperator(sexpr))) {
cout << "error: illegal numeric literal or illegal operator" << endl;
return false;
}
} else {
int length = sexpr.length();
int inumleft = 1;
int i;
for (i = 1; i < length; i ++) {
if ('\"' == sexpr[i]) {
inumleft ++;
}
if (2 == inumleft) {
break;
}
}
if ((i < length-1) || (inumleft != 2)) {
cout << "error: illegal s-expression " << endl;
return false;
}
}
return true;
}
/**
* \brief Make the cell.
* \param str The string to represent the symbol, int or double.
*/
Cell* makecell(string str)
{
Cell* root;
if (((str[0] >= '0') && (str[0] <= '9')) || (str[0] == '.')
|| ((('+'==str[0]) || ('-'==str[0]))&&(str.length()>1))) {
if (false == is_legalnumeric(str)) {
cout << "error: illegal numeric literal" << endl;
exit(1);
}
// this is a numeric literal
if (string::npos == str.find('.')) {
// int number
char* fchar = const_cast<char*>(str.c_str());
int value = atoi(fchar);
root = make_int(value);
} else {
// this is a double
char* fchar = const_cast<char*>(str.c_str());
double value = atof(fchar);
root = make_double(value);
}
}
// we don't deal with literal strings right now, so they are commented out
// else if (str[0] == '\"') {
// // this is a string literal
// string strval = str.substr(1, str.size() - 2);
// root = make_string(const_cast<char*>(strval.data()));
// }
else {
// this is a symbol
if (false == is_legaloperator(str)) {
cout << "error: illegal operator" << endl;
exit(1);
}
root = make_symbol(const_cast<char*>(str.data()));
}
return root;
}
Cell* separate_parse(string& sexpr);
Cell* parse(string sexpr)
{
// is_legal(sexpr);
// delete the whitesapce at the begining and end
// such that the first and last character are not white space
clearwhitespace(sexpr);
// if (sexpr.length() == 0) {
// return nil;
// }
if (sexpr.length() == 0) {
return nil;
}
if ( !is_legalexpr(sexpr)) {
return nil;
}
// check whether is single symbol
// i.e. leaf cell
// if (string::npos == sexpr.find('(')) {
if ('(' != sexpr[0]) {
// this is leaf cell
// bulid this leaf cell
Cell* root = makecell( sexpr );
return root;
}
// the first and last character are '(' and ')', respectively
// delete the two characters
int length = sexpr.size();
sexpr = sexpr.substr(1, length-2);
clearwhitespace(sexpr);
length = sexpr.size();
// if ( inparsecar ) {
// if (sexpr == "") {
// Cell* ec = new Cell("()");
// return ec;
// }
// }
// separate the s-expression into two left and right subsexps
Cell* root = separate_parse(sexpr);
return root;
}
/**
* \brief Separately parse the sexpr and build the tree.
* \param instr The string which consists of s-expressions.
* \return A pointer to the conspair cell at the root of the parse tree.
*/
Cell* separate_parse(string& instr)
{
string sexp;
bool isstartsexp = false;
int inumleftparenthesis = 0;
// check whether to read the end
clearwhitespace(instr);
int length = instr.size();
// check whether it is a "()" sexpr
while (instr.size() > 0) {
// read char by char
char currentchar = instr[0];
// skip some white space before new s-expression occurs
if ((true == iswhitespace(currentchar))&&(false == isstartsexp)) {
continue;
}
// run accross a new s-expression
if ((false == isstartsexp)&&(false == iswhitespace(currentchar))) {
// check whether single symbol
if ('(' != currentchar) {
// read single a single symbol
readsinglesymbol(instr, sexp);
clearwhitespace(instr);
inparsecar = true;
Cell* car = parse(sexp);
inparsecar = false;
Cell* cdr = parse("(" + instr + ")");
Cell* root = cons(car, cdr);
sexp.clear();
return root;
} else {
// start new expression
isstartsexp = true;
// read left parenthesiss
sexp += currentchar;
instr = instr.substr(1, instr.size() -1);
inumleftparenthesis = 1;
}
} else {
// in the process of reading the current s-expression
if (true == isstartsexp) {
if (true == iswhitespace(currentchar)) {
// append a blankspace
sexp += ' ';
instr = instr.substr(1, instr.size() -1);
} else {
// append current character
sexp += currentchar;
instr = instr.substr(1, instr.size() -1);
// count left parenthesiss
if ('(' == currentchar) {
inumleftparenthesis ++;
}
if (')' == currentchar) {
inumleftparenthesis --;
// check whether current s-expression ends
if (0 == inumleftparenthesis) {
// current s-expression ends
isstartsexp = false;
clearwhitespace(instr);
inparsecar = true;
Cell* car = parse(sexp);
inparsecar = false;
int length = instr.length();
Cell* cdr;
Cell* root;
if (length <= 0) {
cdr = nil;
} else {
cdr = parse("(" + instr + ")");
}
root = cons(car, cdr);
sexp.clear();
return root;
}
}
}
}
}
}
return nil;
}