-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1400.cc
149 lines (129 loc) · 3.42 KB
/
1400.cc
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
//Name: Complicated Expressions
//Level: 3
//Category: 構文解析
//Note: TLE厳しい
/* 解法:
* とりあえず構文解析してから()の位置を考える.
* 優先順位の逆転があるときと,-や/が絡んで左結合されてはまずいときのみに()がつく.
*/
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
using namespace std;
enum AST_Type {
TYPE_OPERATOR,
TYPE_IDENTIFIER,
};
struct AST {
AST_Type type;
string name;
char op;
AST *left, *right;
AST() {}
AST(char op) : type(TYPE_OPERATOR), op(op) {}
AST(const string &name) : type(TYPE_IDENTIFIER), name(name) {}
} buf[250];
typedef string::iterator Iterator;
int bufpos = 0;
AST *fetch(char op) {
return &(buf[bufpos++] = AST(op));
}
AST *fetch(const string &name) {
return &(buf[bufpos++] = AST(name));
}
void skip(const string &str, int &pos, char c) {
if(str[pos] != c) {
cerr << "expected " << c << " but " << str[pos] << " near " << str.substr(pos) << endl;
}
++pos;
}
string identifier(const string &str, int &pos) {
int start = pos;
while(islower(str[pos])) ++pos;
return str.substr(start, pos-start);
}
AST* expr(const string &str, int &pos);
AST* term(const string &str, int &pos) {
AST *cur;
if(str[pos] == '(') {
++pos;
cur = expr(str, pos);
skip(str, pos, ')');
}
else {
cur = fetch(identifier(str, pos));
}
return cur;
}
AST* factor(const string &str, int &pos) {
AST *cur = term(str, pos);
while(str[pos] == '*' || str[pos] == '/') {
AST *opnode = fetch(str[pos]);
++pos;
opnode->left = cur;
opnode->right = term(str, pos);
cur = opnode;
}
return cur;
}
AST* expr(const string &str, int &pos) {
AST *cur = factor(str, pos);
while(str[pos] == '+' || str[pos] == '-') {
AST *opnode = fetch(str[pos]);
++pos;
opnode->left = cur;
opnode->right = factor(str, pos);
cur = opnode;
}
return cur;
}
void print_ast(AST *cur, int depth) {
for(int i = 0; i < depth; ++i) cout << ' ';
if(cur->type == TYPE_OPERATOR) cout << cur->op << endl;
if(cur->type == TYPE_IDENTIFIER) cout << cur->name << endl;
if(cur->type == TYPE_OPERATOR) {
print_ast(cur->left, depth+2);
print_ast(cur->right, depth+2);
}
}
string visit(AST *cur, char prev, bool rhs) {
if(cur->type == TYPE_IDENTIFIER) return cur->name;
if(cur->type == TYPE_OPERATOR) {
bool need_paren;
if(cur->op == '+' || cur->op == '-') {
need_paren = prev == '*' || prev == '/';
if(rhs && prev == '-') need_paren = true;
}
else {
need_paren = (rhs && prev == '/');
}
string lhs = visit(cur->left, cur->op, false);
string rhs = visit(cur->right, cur->op, true);
if(need_paren) return string("(") + lhs + cur->op + rhs + ")";
else return lhs + cur->op + rhs;
}
return "";
}
void free_ast(AST *cur) {
if(cur->type == TYPE_IDENTIFIER) delete cur;
else {
free_ast(cur->left);
free_ast(cur->right);
delete cur;
}
}
int main() {
int N;
cin >> N;
while(N--) {
bufpos = 0;
string str;
cin >> str;
int pos = 0;
AST *ast = expr(str, pos);
string expr = visit(ast, '\0', false);
cout << expr << endl;
}
return 0;
}