-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parser.cpp
More file actions
62 lines (59 loc) · 1.09 KB
/
test_parser.cpp
File metadata and controls
62 lines (59 loc) · 1.09 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
#include "parser.h"
#include <iostream>
#include <queue>
using namespace std;
void printNode(AstNode *node) {
int type = node->type;
switch (type) {
case 0:
cout << "( Char, " << char(node->value) << " ) ";
break;
case 1:
if (node->value == '[')
cout << "( Op, range: " << node->sValue << " ) ";
else if (node->value == '{')
cout << "( Op, Repe: " << node->sValue << " ) ";
else if (node->value == '@')
cout << "( Op, and ) ";
else
cout <<"( Op, " << char(node->value) << " )";
break;
default:
break;
}
}
void PrintAst(AstNode *root)
{
if (root == NULL)
return;
queue<AstNode *> Queue;
Queue.push(root);
int num_son = 1, temp = 0;
while (!Queue.empty()) {
while (num_son-- != 0) {
AstNode *node = Queue.front();
printNode(node);
for (auto &x : node->childs) {
Queue.push(x);
++temp;
}
Queue.pop();
}
cout << endl;
num_son = temp;
temp = 0;
}
}
int main()
{
string s;
while (getline(cin, s)) {
try{
PrintAst(parser(s));
} catch (runtime_error err) {
cout << err.what() << endl;
continue;
}
}
return 0;
}