-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
249 lines (231 loc) · 4.74 KB
/
parser.cpp
File metadata and controls
249 lines (231 loc) · 4.74 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
/*
* Rule: Expr
* | ^Expr
* | Expr$
*
* Expr: Expr '|' Cat_Expr
* | Cat_Expr
*
* Cat_Expr: Cat_Expr Factor
* | Factor
*
* Factor: |Term*
* |Term+
* |Term?
* |Term{n,m}
* |Term{n,}
* |Term{n}
* |Term
*
* Term: [String]
* |[^string]
* |.
* |Char
* |(Expr)
*/
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include "parser.h"
using namespace std;
AstNode *parser(const string &s);
AstNode *getExpr(istringstream &stream);
AstNode *getCatExpr(istringstream &stream);
AstNode *getFactor(istringstream &stream);
AstNode *getTerm(istringstream &stream);
AstNode::~AstNode()
{
for (auto &x : childs)
delete x;
}
// 暂时不处理 \^ 和 \s
AstNode *parser(const string &s)
{
istringstream stream(s);
char c;
AstNode *result = getExpr(stream);
if (!stream.eof() || result == NULL) {
string error;
stream >> error;
delete result;
throw runtime_error("some error happend in " + error);
}
if (result->type == Error) {
string error = result->sValue;
delete result;
throw runtime_error(error);
}
return result;
}
AstNode *getExpr(istringstream &stream)
{
AstNode *result;
char c;
result = getCatExpr(stream);
if (result == NULL || result->type == Error || !(stream >> c))
return result;
// 创建 |
if (c == '|') {
result = new AstNode(Op, '|', result);
AstNode *child = getCatExpr(stream);
if (child == NULL) {
delete result;
return new AstNode(Error, "synatx error : |");
}
if (child->type == Error) {
delete result;
return child;
}
result->addChild(child);
}
else {
stream.putback(c);
return result;
}
while (stream >> c) {
if (c == '|') {
AstNode *otherChild = getCatExpr(stream);
if (otherChild == NULL) {
delete result;
return new AstNode(Error, "synatx error : |");
}
if (otherChild->type == Error) {
delete result;
return otherChild;
}
result->addChild(otherChild);
}
else {
stream.putback(c);
return result;
}
}
return result;
}
AstNode *getCatExpr(istringstream &stream)
{
AstNode *result = getFactor(stream);
if (result == NULL || result->type == Error)
return result;
AstNode *child = getFactor(stream);
if (child == NULL)
return result;
if (child->type == Error) {
delete result;
return child;
}
result = new AstNode(Op, '@', result);
result->addChild(child);
while (true) {
AstNode *child = getFactor(stream);
if (child == NULL)
break;
if (child->type == Error) {
delete result;
return child;
}
result->addChild(child);
}
return result;
}
bool isDigit(char c) { return ('0' <= c && c <= '9'); }
AstNode *getOpRep(istringstream &stream)
{
char c;
stream >> c;
string start, end;
while (stream >> c && isDigit(c))
start += c;
if (start.size() == 0)
return new AstNode(Error, "synatx error: {}");
if (c == '}')
end = "";
else if(c == ',') {
stream >> c;
if (c == '}')
end = "$";
else {
if (!isDigit(c))
return new AstNode(Error, "synatx error: {}");
end += c;
while (stream >> c && isDigit(c))
end += c;
if (c != '}')
return new AstNode(Error, "synatx error: missing '}'");
}
}
else
return new AstNode(Error, "synatx error: {}");
string sVaule = start + ":" + end;
return new AstNode(Op, '{', sVaule);
}
AstNode *getFactor(istringstream &stream)
{
char c;
AstNode *result = getTerm(stream);
if (result == NULL || result->type == Error || !(stream >> c))
return result;
if (c == '*' || c == '+' || c == '?')
return new AstNode(Op, c, result);
if (c == '{') {
stream.putback(c);
AstNode *op = getOpRep(stream);
if (op->type == Error) {
delete result;
return op;
}
op->addChild(result);
return op;
}
stream.putback(c);
return result;
}
AstNode *getOpRange(istringstream &stream)
{
char c;
string val;
while (stream >> c && c != ']') {
val += c;
}
if (c != ']')
return new AstNode(Error, "synatx error: missing ']'");
return new AstNode(Op, '[', val);
}
AstNode *getTerm(istringstream &stream)
{
char c;
if(!(stream >> c))
return NULL;
switch (c) {
case '[':
return getOpRange(stream);
case '(': {
AstNode *expr = getExpr(stream);
if ((stream >> c) && (c == ')'))
return expr;
delete expr;
return new AstNode(Error, "synatx error: missing ')'");
}
case '\\':
if ((stream >> c)) {
AstNode *symbol = new AstNode(Char, c);
AstNode *result = new AstNode(Op, '\\', symbol);
return result;
}
return new AstNode(Error, "synatx error: \\");
case '.':
return new AstNode(Op, c);
case '+':
case '*':
case ']':
case '}':
case '?':
case ')':
case '|':
stream.putback(c);
return NULL;
default:
return new AstNode(Char, c);
}
}