-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1106.cpp
More file actions
158 lines (128 loc) · 3.83 KB
/
1106.cpp
File metadata and controls
158 lines (128 loc) · 3.83 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
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
#include<iostream>
#include <iterator>
#include<vector>
#include<memory>
enum class TokenType {
Illegal,
Lparen,
Rparen,
Comma,
And,
Or,
Not,
True,
False,
Eof,
};
struct Token {
TokenType Type;
char Literal;
};
// we don't even need Lexer here, yeah.. true
class Lexer {
public:
std::string input;
size_t position;
char ch;
Lexer(const std::string& input) : input(input), position(0), ch(0) {
readChar();
};
Token NextToken() {
Token tok;
switch (ch) {
case '(': tok = Token{ TokenType::Lparen, '(' }; break;
case ')': tok = Token{ TokenType::Rparen, ')' }; break;
case ',': tok = Token{ TokenType::Comma, ',' }; break;
case '&': tok = Token{ TokenType::And, '&' }; break;
case '|': tok = Token{ TokenType::Or, '|' }; break;
case '!': tok = Token{ TokenType::Not, '!' }; break;
case 't': tok = Token{ TokenType::True, 't' }; break;
case 'f': tok = Token{ TokenType::False, 'f' }; break;
case 0 : tok = Token{ TokenType::Eof, 0}; break;
default: throw std::runtime_error("illegal character");
}
readChar();
return tok;
}
void readChar() {
ch = position >= input.size() ? 0 : input[position];
++position;
}
};
class Parser {
public:
std::unique_ptr<Lexer> l;
Token curToken;
Token peekToken;
Parser(std::unique_ptr<Lexer> lexer) : l(std::move(lexer)) {
peekToken = l->NextToken();
nextToken();
}
void nextToken() {
curToken = peekToken;
peekToken = l->NextToken();
}
bool parseExpr() {
bool result;
switch (curToken.Type) {
case TokenType::And: result = parseAndRes(); break;
case TokenType::Not: result = parseNotRes(); break;
case TokenType::Or : result = parseOrRes(); break;
case TokenType::False: nextToken(); return false;
case TokenType::True: nextToken(); return true;
default: std::cout << "cutToken: " << curToken.Literal; throw std::runtime_error("illegal character");
};
return result;
}
bool parseAndRes() {
nextToken(); // |(&(t,f,t),t)
nextToken();
bool tRes = parseExpr();
// std::cout << "and curToken: " << curToken.Literal;
while (curToken.Type == TokenType::Comma) {
nextToken();
bool nextResult = parseExpr();
tRes = tRes && nextResult;
// std::cout << "before curToken: " << curToken.Literal << "\n";
}
// std::cout << "tres: " << tRes << "\n";
nextToken();
// std::cout << "before peekToken: " << peekToken.Literal << "\n";
return tRes;
}
bool parseNotRes() { // |(f,&(t,t))
nextToken(); // !(&(f,t))
nextToken();
bool tRes = !parseExpr();
nextToken();
return tRes;
}
bool parseOrRes() {
nextToken(); // |(&(t,f,t),t)
nextToken();
bool tRes = parseExpr();
// std::cout << "curToken: " << curToken.Literal;
while (curToken.Type == TokenType::Comma) {
nextToken();
bool nextResult = parseExpr();
tRes = tRes || nextResult;}
nextToken();
return tRes;
}
};
bool parseBoolExpr(std::string expression) {
auto lexer = std::make_unique<Lexer>(expression);
Parser parse(std::move(lexer));
return parse.parseExpr();
}
int main() {
std::string expression = "!(&(&(!(&(f)),&(t),|(f,f,t)),&(t),&(t,t,f)))";
std::cout << parseBoolExpr(expression);
// Lexer l(expression);
//
// for (int i = 0; i < expression.size(); ++i) {
// Token t = l.NextToken();
// std::cout << t.Literal;
// }
std::cin.get();
}