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
|
#include <cctype>
#include <cstdarg>
#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
template <typename T2,typename T>
T2 ato(const std::basic_string<T> &str){
std::basic_stringstream<T> stream(str);
T2 res;
return !(stream >>res)?0:res;
}
template <typename T> inline long atol(const std::basic_string<T> &str){ return ato<long>(str); }
template <typename T> inline double atof(const std::basic_string<T> &str){ return ato<double>(str); }
struct Accumulator;
enum TokenType{
null=0,
END=1,
INTEGER='0',
POINT='.',
PLUS='+',
MINUS='-',
MUL='*',
DIV='/',
POW='^',
LPAREN='(',
RPAREN=')',
expr=128
};
struct Token{
TokenType type;
double f_value;
std::string literal;
Token(TokenType type=END):type(type),f_value(0){}
Token(const std::string &val):type(INTEGER),literal(val){}
Token(char character){
this->type=(TokenType)character;
}
Accumulator operator<<(const Token &);
};
struct Rule;
typedef Token (*handler_f)(const Rule &,const Token *);
struct Accumulator{
int state;
Token reduces_to,
lookahead;
std::vector<Token> constraints;
handler_f handler;
Accumulator(const Token &token):state(0),handler(0){
this->reduces_to=token;
}
Accumulator &operator<<=(const Token &token){
if (!this->state){
this->lookahead=token;
state++;
}else
this->constraints.push_back(token);
return *this;
}
Accumulator operator<<(const Token &token){
Accumulator r=*this;
r<<=token;
return r;
}
Accumulator &operator<<=(const handler_f f){
this->handler=f;
return *this;
}
Accumulator operator<<(const handler_f f){
Accumulator r=*this;
r<<=f;
return r;
}
};
Accumulator Token::operator<<(const Token &token){
return Accumulator(*this)<<token;
}
struct Rule{
Token reduces_to;
std::vector<Token> constraints;
Token lookahead;
handler_f handler;
Rule(const Accumulator &accum):
reduces_to(accum.reduces_to),
lookahead(accum.lookahead),
constraints(accum.constraints),
handler(accum.handler){}
bool matches(const std::vector<Token> &stack,const Token &lookahead){
if (stack.size()<this->constraints.size() ||
this->lookahead.type!=null && this->lookahead.type!=lookahead.type)
return 0;
const Token *array=&stack[stack.size()-this->constraints.size()];
for (unsigned a=0,size=this->constraints.size();a<size;a++)
if (array[a].type!=this->constraints[a].type)
return 0;
return 1;
}
};
double make_fractional_part(const std::string &str){
double r=0;
for (size_t a=0;a<str.size();a++){
r+=(str[str.size()-a-1]-'0');
r/=10;
}
return r;
}
#define DEFINE_HANDLER(x,y) \
Token x(const Rule &rule,const Token *redex){ \
Token new_token(rule.reduces_to); \
y; \
return new_token; \
}
DEFINE_HANDLER(
handle_int_p_int,
new_token.f_value=atof(redex[0].literal)+make_fractional_part(redex[2].literal)
)
DEFINE_HANDLER(
handle_p_int,
new_token.f_value=make_fractional_part(redex[1].literal)
)
DEFINE_HANDLER(
handle_int,
new_token.f_value=atof(redex[0].literal)
)
DEFINE_HANDLER(
handle_nop,
new_token.f_value=redex[1].f_value
)
DEFINE_HANDLER(
handle_minus_int,
new_token.f_value=-redex[1].f_value
)
DEFINE_HANDLER(
handle_pow,
new_token.f_value=pow((double)redex[0].f_value,(double)redex[2].f_value)
)
DEFINE_HANDLER(
handle_mul,
new_token.f_value=redex[0].f_value*redex[2].f_value
)
DEFINE_HANDLER(
handle_div,
new_token.f_value=redex[0].f_value/redex[2].f_value
)
DEFINE_HANDLER(
handle_add,
new_token.f_value=redex[0].f_value+redex[2].f_value
)
DEFINE_HANDLER(
handle_sub,
new_token.f_value=redex[0].f_value-redex[2].f_value
)
class Parser{
std::stringstream stream;
std::vector<Token> stack;
bool result;
std::vector<Rule> rules;
Token read(){
char character;
while (!this->stream.eof() && isspace(character=this->stream.peek()))
this->stream.get();
if (this->stream.eof())
return END;
character=this->stream.peek();
if (isdigit(character)){
std::string str;
str.push_back(this->stream.get());
while (isdigit(this->stream.peek()))
str.push_back(this->stream.get());
return str;
}
return (char)this->stream.get();
}
bool reduce(const Token &lookahead){
long rule_index=-1;
unsigned max=0;
for (unsigned a=0;a<this->rules.size();a++){
if (this->rules[a].matches(this->stack,lookahead) && this->rules[a].constraints.size()>max){
rule_index=a;
max=this->rules[a].constraints.size();
}
}
if (rule_index<0 || this->rules[rule_index].reduces_to.type==null)
return 0;
Rule &rule=this->rules[rule_index];
Token new_token=rule.handler(rule,&this->stack[this->stack.size()-rule.constraints.size()]);
for (unsigned a=0;a<rule.constraints.size();a++)
this->stack.pop_back();
this->stack.push_back(new_token);
return 1;
}
bool run(){
Token next_token=this->read();
while (this->reduce(next_token));
switch (next_token.type){
case END:
this->result=(this->stack.size()==1);
return 0;
case INTEGER:
case POINT:
case PLUS:
case MINUS:
case MUL:
case DIV:
case RPAREN:
case LPAREN:
case POW:
this->stack.push_back(next_token);
return 1;
default:
this->result=0;
return 0;
}
}
void initialize_rules(){
this->rules.clear();
#define DEFINE_RULE(x,y,z) this->rules.push_back(Rule(Token(x) << y << z))
DEFINE_RULE(null,POINT,INTEGER);
DEFINE_RULE(null,INTEGER,INTEGER << POINT);
DEFINE_RULE(expr,null,INTEGER << POINT << INTEGER << handle_int_p_int);
DEFINE_RULE(expr,null,INTEGER << POINT << handle_int);
DEFINE_RULE(expr,null,POINT << INTEGER << handle_p_int);
DEFINE_RULE(expr,null,INTEGER << handle_int);
DEFINE_RULE(expr,null,LPAREN << expr << RPAREN << handle_nop);
DEFINE_RULE(expr,null,PLUS << expr << handle_nop);
DEFINE_RULE(expr,null,MINUS << expr << handle_minus_int);
DEFINE_RULE(null,POW,expr << POW << expr);
DEFINE_RULE(expr,null,expr << POW << expr << handle_pow);
DEFINE_RULE(null,POW,expr << MUL << expr);
DEFINE_RULE(expr,null,expr << MUL << expr << handle_mul);
DEFINE_RULE(null,POW,expr << DIV << expr);
DEFINE_RULE(expr,null,expr << DIV << expr << handle_div);
DEFINE_RULE(null,POW,expr << PLUS << expr);
DEFINE_RULE(null,MUL,expr << PLUS << expr);
DEFINE_RULE(null,DIV,expr << PLUS << expr);
DEFINE_RULE(expr,null,expr << PLUS << expr << handle_add);
DEFINE_RULE(null,POW,expr << MINUS << expr);
DEFINE_RULE(null,MUL,expr << MINUS << expr);
DEFINE_RULE(null,DIV,expr << MINUS << expr);
DEFINE_RULE(expr,null,expr << MINUS << expr << handle_sub);
}
public:
Parser(){
this->initialize_rules();
}
bool eval(double &res,const std::string &str){
this->stream.str(str);
this->stream.clear();
this->stack.clear();
this->result=0;
while (this->run());
if (this->result)
res=this->stack.front().f_value;
else
this->stack.clear();
return this->result;
}
};
int main(){
Parser evaluator;
while (1){
double res=0;
std::string input;
std::getline(std::cin,input);
if (!input.size())
break;
if (evaluator.eval(res,input))
std::cout <<"ACCEPT\n="<<res<<std::endl;
else
std::cout <<"REJECT\n";
}
return 0;
}
|