-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cs
More file actions
313 lines (291 loc) · 12 KB
/
Parser.cs
File metadata and controls
313 lines (291 loc) · 12 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* SAT solver using Bruteforce
* Expression rule : PEMDAS | written in reverse polish notation
* Parser
* -> Postfix // Shunting-yard algorithm
* -> EvalExpression // Reverse polish reader
*
* @author : Princy Rasolonjatovo
* @email : princy.m.rasolonjatovo@gmail.com
* @github : princy-rasolonjatovo
**/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace SATBruteforce
{
public class Parser
{
public string Input { get; init; }
private List<Operator> Operators;
public int TokenCount { get; set; }
public List<IExpressionType> Postfixed { get; init; }
public Parser(string s, ref List<Operator> operators)
{
// Clean Tokens
TokenTracker.RemoveAll();
this.Input = s;
this.Operators = operators;
this.Postfixed = this.Postfix(s);
}
private List<string> OperationToList(string s)
{
List<string> operatorsSymbols = new List<string>();
this.Operators.ForEach(op => operatorsSymbols.Add(op.Symbol));
string pattern = $@"(\(|\)|{String.Join('|', operatorsSymbols)})";
string[] raw_splited = Regex.Split(s, pattern);
List<string> splited = new List<string>();
foreach (string raw in raw_splited)
{
string _raw = raw.Replace(" ", "").Trim();
if (_raw.Length > 0)
{
splited.Add(_raw);
}
}
return splited;
}
private List<IExpressionType> Postfix(string s)
{
Stack<IExpressionType> stack = new Stack<IExpressionType>();
Dictionary<string, Operator> operators = new Dictionary<string, Operator>();
foreach (Operator op in this.Operators)
{
operators.Add(op.Symbol, op);
}
// Parenthesis markers
Operator OPENPARENTHESIS = new Operator("(", "(", null, -1, false);
Operator CLOSEPARENTHESIS = new Operator(")", ")", null, -1, false); ;
List<IExpressionType> operations = new List<IExpressionType>();
List<string> splited = this.OperationToList(s);
string val;
while (splited.Count > 0 || stack.Count > 0)
{
// Left to Right (LR)
if (splited.Count > 0)
{
// Get the current element from input(Token | Operator | parenthesis)
val = splited.First();
splited.RemoveAt(0);
}
else
{
// No more tokens to process in input
// Add remaining operations(in the stack) to the main queue
while (stack.Count > 0)
{
operations.Add(stack.Pop());
}
continue;
}
// Check if current element is an operator
if (operators.ContainsKey(val))
{
Operator op = operators[val];
// is the stack (containing operator) empty?
if (stack.Count > 0)
{
// the stack is not empty
// get the top of the stack
IExpressionType top = stack.Pop();
// Is the topest element a openparenthesis ?
if (top.GetSymbol() != OPENPARENTHESIS.GetSymbol())
{
// The top element in operator_stack is not a parenthesis
// priority is DESC (rmin has higher priority)
if ((top as Operator)?.Priority <= op.Priority)
{
// add higher priority element into the queue
operations.Add(top);
stack.Push(op);
continue;
}
else
{
stack.Push(top);
stack.Push(op);
continue;
}
}
else
{
// Is parenthesis
stack.Push(top);
stack.Push(op);
continue;
}
}
else
{
// The stack is empty
stack.Push(op);
continue;
}
}
else if (val == OPENPARENTHESIS.GetSymbol())
{
stack.Push(OPENPARENTHESIS);
continue;
}
else if (val == CLOSEPARENTHESIS.GetSymbol())
{
// Remove all element from the stack until OPENPARENTHESIS
while (stack.Count > 0)
{
IExpressionType top = stack.Pop();
if (top.GetSymbol() == OPENPARENTHESIS.GetSymbol())
{
break;
}
else
{
operations.Add(top);
}
}
continue;
}
else
{
// The current element is a variable | constant
operations.Add(TokenTracker.CreateToken(val, false));
}
}
this.TokenCount = TokenTracker.GetInstancesCount();
return operations;
}
private bool EvalExpression(List<bool> values)
{
if(this.TokenCount != values.Count)
{
throw new Exception(
$"[NotEnoughValuesError] numbers of variables in expression: {this.TokenCount} number of variable on input: {values.Count}"
);
}
int k = 0;
TokenTracker.GetTokens().ForEach(t => t.Value = values.ElementAt(k++));
Func<IExpressionType, int, IExpressionType> fn_cloner = (el, i) => el.RevealType() == ExpressionType.OPERATOR ? (el as Operator)?.Clone() : (el as Token)?.Clone();
List<IExpressionType> expression = this.Postfixed.Select(fn_cloner).ToList();
Stack<bool> stack = new Stack<bool>();
while (expression.Count > 0)
{
IExpressionType val = expression.First();
expression.RemoveAt(0);
// Check if its a Token
if (val.RevealType() == ExpressionType.TOKEN)
{
stack.Push((val as Token).Value);
}
else
{
// Three address code
try
{
Operator op = (val as Operator);
if (op.IsUnary)
{
// Is The stack empty ?
if (stack.Count == 0)
{
Stack<IExpressionType> temp_stack = new Stack<IExpressionType>();
// '(' is just a marker
temp_stack.Push(new Operator("(", "(", null, -1, false));
IExpressionType _val = expression.First();
expression.RemoveAt(0);
// POP all unary operators till getting a Token
while (_val.RevealType() != ExpressionType.TOKEN)
{
temp_stack.Push(_val);
_val = expression.First();
expression.RemoveAt(0);
}
IExpressionType _ = temp_stack.Pop();
bool _val_value = (_val as Token).Value;
while (_.GetSymbol() != "(")
{
_val_value = (_ as Operator).Apply(_val_value, false);
_ = temp_stack.Pop();
}
stack.Push(op.Apply(_val_value, false));
}
else
{
stack.Push(op.Apply(stack.Pop(), false));
}
continue;
}
// it must be an operator here if the poped elements(left=operator,right=operator) is an operator(like in ----1-----3)
// for example
// what are the operand of the current operator
bool right;
bool left;
right = stack.TryPop(out right) ? right : false;
left = stack.TryPop(out left) ? left : false;
stack.Push(op.Apply(left, right));
continue;
}
catch (ArgumentOutOfRangeException ex)
{
// Solved using stack.TryPop
}
catch (Exception ex)
{
Console.Error.WriteLine($"[compileStringError] unknown operator: {val} Error: {ex.Message}");
throw new Exception("[OperationAborted]");
}
}
}
return stack.Pop();
}
/// <summary>
/// Used to generate bool values for Token variables
/// </summary>
/// <param name="count">dimension of the boolean vector</param>
/// <returns>List{true, false, ...(count)}</returns>
private IEnumerable<List<bool>> GenerateTokenValues(int count)
{
Func<int, int, List<bool>> divide_mod = (int number, int autosize) => {
List<bool> result = new List<bool>();
while (number > 0)
{
int mod = number % 2;
result.Add(mod == 1);
number = number / 2;
}
while (result.Count < autosize)
{
result.Add(false);
}
return result;
};
for (int i = 0; i < Math.Pow(2, count); i++)
{
yield return divide_mod(i, count);
}
}
public List<Dictionary<string, bool>> Bruteforce()
{
List<Dictionary<string, bool>> solutions = new List<Dictionary<string, bool>>();
foreach (List<bool> values in GenerateTokenValues(this.TokenCount))
{
bool ret = this.EvalExpression(values);
if (ret)
{
Dictionary<string, bool> expression = new Dictionary<string, bool>();
//Func<Token, int, Token> feed_expression= (Token token, int i) => { expression.Add(token.Name, values[i]); return token; };
//TokenTracker.GetTokens().Select(feed_expression);
int i = 0;
foreach (Token token in TokenTracker.GetTokens())
{
expression.Add(token.Name, values[i]);
i++;
}
solutions.Add(expression);
}
}
return solutions;
}
}
}