-
Notifications
You must be signed in to change notification settings - Fork 0
/
expParse.h
177 lines (163 loc) · 5.46 KB
/
expParse.h
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
#pragma once
#include "rule.hpp"
#include <iostream>
#include <stack>
class bracket {
public:
string val;
int left_index;
int right_index;
bool isNot;
ruleExp* bracket_rule=nullptr;
bracket(string val,int l_i,int r_i,bool isNot){
this->val=val;
this->left_index=l_i;
this->right_index=r_i;
this->isNot=isNot;
}
void clear(){
val="";
left_index=-1;
right_index=-1;
}
};
class left_bracket{
public:
int index;
bool isNot;
left_bracket(int index,bool isNot=false){
this->index=index;
this->isNot=isNot;
}
};
class expParse {
public:
static ruleExp* total_strrule_parse(const string& strrule){
if(strrule==""){
return nullptr;
}
vector<bracket> bracket_vec;
if(bracketCheck(strrule,bracket_vec)==false){
throw string("Wrong expression");
}
for(int i=0;i<bracket_vec.size();++i){
if(insert_rule(bracket_vec,bracket_vec[i],strrule)==false){
inter_call(bracket_vec[i]);
}
}
return bracket_vec[bracket_vec.size()-1].bracket_rule;
}
static bool insert_rule(vector<bracket>& bracket_vec,bracket& tmp,const string& strrule){
vector<int> insert_vec;
for(int i=0;i<bracket_vec.size();++i){
if(tmp.left_index<bracket_vec[i].left_index&&bracket_vec[i].right_index<tmp.right_index){
insert_vec.push_back(i);
}
}
if(insert_vec.empty()){
return false;
}
do_insert_rule(bracket_vec[insert_vec[0]],bracket_vec[insert_vec[1]],tmp,strrule);
if(tmp.isNot==true){
tmp.bracket_rule=new logExp (NOT,nullptr,tmp.bracket_rule);
}
for(int i=0;i<insert_vec.size();++i){
bracket_vec[insert_vec[i]].clear();
}
return true;
}
static void do_insert_rule(const bracket& left_bk,const bracket& right_bk,bracket& tmp,const string& strrule){
string symbol=strrule.substr(left_bk.right_index+1,2);
if(symbol=="||"){
tmp.bracket_rule=new logExp(OR,left_bk.bracket_rule,right_bk.bracket_rule);
}
else if(symbol=="&&"){
tmp.bracket_rule=new logExp(AND,left_bk.bracket_rule,right_bk.bracket_rule);
}
else{
throw string("Wrong expression");
}
}
static bool bracketCheck(const string& strrule,vector<bracket>& bracket_val){
stack<left_bracket> left_brackets;
for(int i=0;i<strrule.size();++i){
char c=strrule[i];
if(c=='('){
if(i-1>=0&&strrule[i-1]=='!'){
left_brackets.push(left_bracket(i,true));
}
else{
left_brackets.push(left_bracket(i,false));
}
}
else if(c==')'){
if(left_brackets.empty()){
return false;
}
left_bracket tmp_lb=left_brackets.top();
left_brackets.pop();
bracket_val.push_back(bracket(strrule.substr(tmp_lb.index+1,i-(tmp_lb.index+1)),tmp_lb.index,i,tmp_lb.isNot));
}
}
if(left_brackets.empty()==false){
return false;
}
return true;
}
static void inter_call(bracket& bk){
if(bk.isNot==true){
bk.bracket_rule=new logExp(NOT,nullptr,part_strrule_parse(bk.val));
}
else{
bk.bracket_rule=part_strrule_parse(bk.val);
}
}
static ruleExp* part_strrule_parse(const string& strrule){
string temp="";
for(int i=0;i<strrule.size();++i){
char c=strrule[i];
if(c=='='||c=='>'||c=='<'||c=='!'){
temp=strrule.substr(i);
break;
}
}
if(temp==""){
throw string("Wrong expression");
}
char c=temp[1];
if(c=='='){
return binary_strrule_parse(temp);
}
return single_strrule_parse(temp);
}
static ruleExp* single_strrule_parse(const string& strrule){
string val=strrule.substr(1);
if(strrule[0]=='<'){
return new numExp(SMAL,typeHelper::typehelper->strToBasic(QString::fromStdString(val)));
}
else if(strrule[0]=='>'){
return new numExp(GRAT,typeHelper::typehelper->strToBasic(QString::fromStdString(val)));
}
else{
throw string("Wrong expression");
}
}
static ruleExp* binary_strrule_parse(const string& strrule){
string val=strrule.substr(2);
if(strrule[0]=='='){
return new ruleExp(EQU,typeHelper::typehelper->strToBasic(QString::fromStdString(val)));
}
else if(strrule[0]=='>'){
return new logExp(OR,new ruleExp(EQU,typeHelper::typehelper->strToBasic(QString::fromStdString(val))),new numExp(GRAT,typeHelper::typehelper->strToBasic(QString::fromStdString(val))));
}
else if (strrule[0]=='<') {
return new logExp(OR,new ruleExp(EQU,typeHelper::typehelper->strToBasic(QString::fromStdString(val))),new numExp(SMAL,typeHelper::typehelper->strToBasic(QString::fromStdString(val))));
}
else if(strrule[0]=='!'){
return new logExp(NOT,nullptr,new ruleExp(EQU,typeHelper::typehelper->strToBasic(QString::fromStdString(val))));
}
else{
throw string("Wrong expression");
}
}
};