-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscriptparser.cpp
109 lines (101 loc) · 2.68 KB
/
scriptparser.cpp
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
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <regex>
#include <sstream>
#include <iterator>
using namespace std;
/*
* Check if the current string is a token
* @param the current string
* @param regex to match the input variables
* @return true if a token
*/
bool isTokenp(string s, regex rx){
return s.find("and") != string::npos || s.find("or") != string::npos || s.find("xor") != string::npos ||
s.find("not") != string::npos || regex_match(s, rx);
}
/*
* For the formula from the given set of tokens by filtering out irrelevant ones
* @param initial set of tokens
* @param regex to match the input variables
* @return the extracted boolean formula
*/
string getTokens(vector<string> tokens , regex pattern){
string formula1 = "";
for(vector<string>::const_iterator i = tokens.begin(); i != tokens.end(); ++i){
string s = *i;
if(isTokenp(s, pattern)){
formula1 += s + " ";
}
}
return formula1;
}
/*
* Form a token vector from the given phrase
* @param the phrase
* @return the vector of tokens in this phrase
*/
vector<string> formVector(string s){
istringstream iss(s);
vector<string> tokens;
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
back_inserter(tokens));
return tokens;
}
/*
* Write the generated formula to an output file
* @param the generated formula
*/
void writeFormula(string formula){
cout << formula << '\n';
ofstream logic1("formula1.txt");
logic1 << formula;
logic1.close();
}
/*
* Extract the relevant phrase from the given file
* @param the path to the script file
* @param the regex representing input variable patterns
* @return the phrase containing the formula
*/
string getPhrase(string path, string pattern){
ifstream file(path);
string str;
string out = "";
bool counto = false;
size_t open = 0;
size_t close = 0;
regex rgx("\\((.*?)\\)");
smatch match;
while (getline(file, str))
{
if(str.find(pattern) == 0){
counto = true;
}
if(counto){
open += count(str.begin(), str.end(), '(');
close += count(str.begin(), str.end(), ')');
out += str;
}
if(open == close){
counto = false;
}
}
file.close();
return out;
}
int main(int argc, char *argv[]){
if(argc < 2){
cout << "Please provide the path to the script" << '\n';
return 0;
}
regex rx("[a-z][0-9][0-9]*\\)*");
string out = getPhrase(argv[1],"(define-fun");
vector<string> tokens = formVector(out);
string formula1 = getTokens(tokens, rx);
writeFormula(formula1);
return 0;
}