-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparserTree.cpp
More file actions
83 lines (76 loc) · 2.16 KB
/
Copy pathparserTree.cpp
File metadata and controls
83 lines (76 loc) · 2.16 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "parserTree.hpp"
#include "helperFunctions_hash.hpp"
#include <stack>
#include <string>
#include <sstream>
#include <iostream>
#include <unordered_map>
using namespace std;
#ifndef dataType
#define dataType int
#define classType int
#define hashConv int
#define pairSet set<pair<classType, dataType>>
#define container unordered_map<classType, dataType>
#define bit16 65535
#endif
parserTree shuntingYard(string exp){
unordered_map<char, char> conversionTable;
conversionTable[')']='(';
conversionTable[']']='[';
conversionTable['}']='{';
stringstream parsing(exp);
string empty = "";
parserTree output;
stack <char> operatorStack;
dataType low;
classType high;
char temp='w';
while(parsing.peek() != -1){
if (parsing.peek()<='9' && parsing.peek()>='0'){
parsing>>high;
parsing.ignore();
if(parsing.peek()=='X' || parsing.peek()=='x'){low=0;parsing.ignore();}
else{parsing>>low;}
output<<convert(make_pair(high, low));
}else{
parsing>>temp;
if( temp!=')' && temp!=']' && temp!='}' ){
operatorStack.push(temp);
}else{
while(operatorStack.top()!=conversionTable[temp]){output<<operatorStack.top();operatorStack.pop();}
operatorStack.pop();
}
}
}while(!operatorStack.empty()){output<<operatorStack.top(); operatorStack.pop();}
return output;
}
bool operatorToken::eval(unordered_map<hashConv, bool> guide){
if (content=='&' || content=='^' || content=='n')return right->eval(guide) && left->eval(guide);
if (content=='|' || content=='v')return right->eval(guide) || left->eval(guide);
return 0;
}
void parserTree::operator <<(hashConv value){
hashConvToken * token = new hashConvToken(value);
tokenStack.push(token);
return;
}
void parserTree::operator <<(char value){
token *right;
token *left;
right = tokenStack.top(); tokenStack.pop();
left = tokenStack.top(); tokenStack.pop();
operatorToken *op = new operatorToken(value);
op->right = right;
op->left = left;
right->parent = op;
left->parent = op;
tokenStack.push(op);
return;
}
bool parserTree::eval(unordered_map<hashConv, bool> input){
if (!tokenStack.size()) return true;
token *base;
base = tokenStack.top();
return base->eval(input);
}