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
|
#include <cctype>
#include <cstdarg>
#include <cmath>
#include <iostream>
#include <sstream>
#include <stack>
#include <vector>
struct Token{
enum TokenType{
null=0,
END=1,
INTEGER='0',
PLUS='+',
MINUS='-',
MUL='*',
DIV='/',
POW='^',
LPAREN='(',
RPAREN=')',
expr=128
} type;
double intValue;
Token(TokenType type=END):type(type),intValue(0){}
Token(long val):type(INTEGER),intValue(val){}
Token(char character){
this->type=(TokenType)character;
}
};
struct Rule{
Token reduces_to;
std::vector<Token> constraints;
Token lookahead;
Rule(const Token &to,const Token &la,unsigned constraints,...){
this->reduces_to=to;
this->lookahead=la;
va_list list;
va_start(list,constraints);
this->constraints.reserve(constraints);
for (unsigned a=0;a<constraints;a++)
this->constraints.push_back(va_arg(list,Token::TokenType));
}
bool matches(const std::vector<Token> &stack,const Token &lookahead){
if (stack.size()<this->constraints.size() ||
this->lookahead.type!=Token::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;
}
};
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 Token::END;
character=this->stream.peek();
if (isdigit(character)){
std::string str;
long temp=atol(str.c_str());
return temp;
}
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==Token::null)
return 0;
Rule &rule=this->rules[rule_index];
Token new_token(rule.reduces_to);
Token *redex=&this->stack[this->stack.size()-rule.constraints.size()];
switch (rule_index){
case 0: //expr <- INTEGER
new_token.intValue=redex[0].intValue;
break;
case 1: //expr <- '(' expr ')'
case 2: //expr <- '+' expr
new_token.intValue=redex[1].intValue;
break;
case 3: //expr <- '-' expr
new_token.intValue=-redex[1].intValue;
break;
case 4: //impossible
case 5: //expr <- expr '^' expr
new_token.intValue=pow((double)redex[0].intValue,(double)redex[2].intValue);
break;
case 6: //expr <- expr '*' expr
new_token.intValue=redex[0].intValue*redex[2].intValue;
break;
case 7: //expr <- expr '/' expr
new_token.intValue=redex[0].intValue/redex[2].intValue;
break;
case 8: //impossible
case 9: //impossible
case 10: //expr <- expr '+' expr
new_token.intValue=redex[0].intValue+redex[2].intValue;
break;
case 11: //impossible
case 12: //impossible
case 13: //expr <- expr '-' expr
new_token.intValue=redex[0].intValue-redex[2].intValue;
break;
}
for (unsigned b=0;b<rule.constraints.size();b++)
this->stack.pop_back();
this->stack.push_back(new_token);
return 1;
}
bool run_state(){
Token next_token=this->read();
while (this->reduce(next_token));
switch (next_token.type){
case Token::END:
this->result=(this->stack.size()==1);
return 0;
case Token::INTEGER:
case Token::PLUS:
case Token::MINUS:
case Token::MUL:
case Token::DIV:
case Token::RPAREN:
case Token::LPAREN:
case Token::POW:
this->stack.push_back(next_token);
return 1;
default:
this->result=0;
return 0;
}
}
void initializeRules(){
this->rules.clear();
/*rule 0*/ this->rules.push_back(Rule( Token::expr, Token::null, 1, Token::INTEGER ));
/*rule 1*/ this->rules.push_back(Rule( Token::expr, Token::null, 3, Token::LPAREN, Token::expr, Token::RPAREN ));
/*rule 2*/ this->rules.push_back(Rule( Token::expr, Token::null, 2, Token::PLUS, Token::expr ));
/*rule 3*/ this->rules.push_back(Rule( Token::expr, Token::null, 2, Token::MINUS, Token::expr ));
/*rule 4*/ this->rules.push_back(Rule( Token::null, Token::POW, 3, Token::expr, Token::POW, Token::expr ));
/*rule 5*/ this->rules.push_back(Rule( Token::expr, Token::null, 3, Token::expr, Token::POW, Token::expr ));
/*rule 6*/ this->rules.push_back(Rule( Token::expr, Token::null, 3, Token::expr, Token::MUL, Token::expr ));
/*rule 7*/ this->rules.push_back(Rule( Token::expr, Token::null, 3, Token::expr, Token::DIV, Token::expr ));
/*rule 8*/ this->rules.push_back(Rule( Token::null, Token::MUL, 3, Token::expr, Token::PLUS, Token::expr ));
/*rule 9*/ this->rules.push_back(Rule( Token::null, Token::DIV, 3, Token::expr, Token::PLUS, Token::expr ));
/*rule 10*/ this->rules.push_back(Rule( Token::expr, Token::null, 3, Token::expr, Token::PLUS, Token::expr ));
/*rule 11*/ this->rules.push_back(Rule( Token::null, Token::MUL, 3, Token::expr, Token::MINUS, Token::expr ));
/*rule 12*/ this->rules.push_back(Rule( Token::null, Token::DIV, 3, Token::expr, Token::MINUS, Token::expr ));
/*rule 13*/ this->rules.push_back(Rule( Token::expr, Token::null, 3, Token::expr, Token::MINUS, Token::expr ));
}
public:
Parser(const std::string &str)
:stream(str){
this->result=0;
this->initializeRules();
}
bool eval(double &res){
while (this->run_state());
if (this->result)
res=this->stack.front().intValue;
else
this->stack.clear();
return this->result;
}
};
int main(){
//Parser evaluator("2^2^2");// Original - Failed
Parser evaluator("2+2+2"); //Failed
double res=0;
system("pause"); //Block code
std::cout <<(evaluator.eval(res)?"ACCEPT":"ABORT")<<std::endl; //The loop goes here...
std::cout <<res<<std::endl; // Is never reached
return 0;
}
| |