Skip to content

Commit

Permalink
Merge pull request #304 from OnionGrief/generator-refact
Browse files Browse the repository at this point in the history
generator refact (mini)
  • Loading branch information
mathhyyn authored Nov 26, 2023
2 parents 5818e3f + 8d9eb63 commit 9c0bbd6
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 157 deletions.
9 changes: 5 additions & 4 deletions apps/InputGeneratorApp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ int main() {
// alphabet_size)
RegexGenerator RG(100, 20, 4, 4);
ofstream out;
out.open("test.txt", ofstream::trunc);
std::ofstream out;
out.open("test_regexex.txt", std::ofstream::trunc);
for (int i = 0; i < 10; i++) {
if (out.is_open()) out << RG.generate_regex() << "\n";
if (out.is_open())
out << RG.generate_regex() << "\n";
}
out.close();
*/
Expand All @@ -27,7 +28,7 @@ int main() {

// Используем сгенерированный тест
TasksGenerator TG;
TG.generate_task(10, 10, false, false);
TG.generate_task(3, 5, false, false);
TG.write_to_file("test.txt");

// Загружаем в интерпретатор файл с коммандами
Expand Down
1 change: 1 addition & 0 deletions apps/MetamorphicTestsApp/src/MetamorphicTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using std::string;

TEST(TestCaseName, Test_random_regex_parsing) {
RegexGenerator rg(15, 10, 5, 3);
//rg.set_neg_chance(2); // для отрицания
for (int i = 0; i < 30; i++) {
string str = rg.generate_regex();
Regex r1(str);
Expand Down
5 changes: 5 additions & 0 deletions libs/InputGenerator/include/InputGenerator/RegexGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class RegexGenerator {
int cur_star_num = 0;
int star_nesting = 0; // вложенность
int cur_nesting = 0;
int neg_chance = 0;
bool all_alts_are_eps = true;
std::string res_str = "";
void generate_regex_();
Expand All @@ -33,6 +34,8 @@ class RegexGenerator {
8 - максимальная длина, 3 - максимальное кол-во звезд,
2 - максимальная звездная вложенность, 2 - число символов в алфавите*/
RegexGenerator();
// генерация регулярок с отрицанием
explicit RegexGenerator(int neg_chance);
/*генератор регулярных выражений, параметрирозованных длиной, кол-вом
итераций Клини и звездной вложенностью*/
RegexGenerator(int regex_length, int star_num, int star_nesting);
Expand All @@ -47,4 +50,6 @@ class RegexGenerator {
std::string generate_framed_regex();
/*запись регулярки в файл*/
void write_to_file(std::string filename);
/*установить шанс появления отрицания - чем больше значение, тем реже шанс*/
void set_neg_chance(int new_neg_chance);
};
2 changes: 2 additions & 0 deletions libs/InputGenerator/include/InputGenerator/TasksGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ class TasksGenerator {

void distribute_functions();
Function generate_next_func(std::string, int);
std::string generate_arguments(Function first_func);
std::string get_random_id_by_type(std::string type);
Function rand_func();
Function rand_pred();
void change_seed();
Expand Down
36 changes: 27 additions & 9 deletions libs/InputGenerator/src/RegexGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ using std::string;

RegexGenerator::RegexGenerator() : RegexGenerator::RegexGenerator(8, 3, 2, 2) {}

RegexGenerator::RegexGenerator(int neg_chance_) : RegexGenerator::RegexGenerator() {
neg_chance = neg_chance_;
}

RegexGenerator::RegexGenerator(int regex_length, int star_num, int star_nesting, int alphabet_size)
: regex_length(regex_length), star_num(star_num), star_nesting(star_nesting) {

Expand Down Expand Up @@ -98,8 +102,7 @@ void RegexGenerator::generate_n_alt_regex() { // <n-alt-regex> ::= <conc-regex>

void RegexGenerator::generate_conc_regex() { // <conc-regex> ::= <simple-regex>
// | <simple-regex><conc-regex>
int v = rand() % 2;
if (v == 0) {
if (rand() % 2 == 0) {
generate_simple_regex();
} else {
generate_simple_regex();
Expand All @@ -109,11 +112,14 @@ void RegexGenerator::generate_conc_regex() { // <conc-regex> ::= <simple-regex>
}
}

void RegexGenerator::generate_simple_regex() { // <simple-regex> ::=
// <lbr><regex-without-eps><rbr><unary>?
// | буква <unary>?
int v = rand() % 2;
if (v == 0) {
// рандомное число в диапозоне [0; n]
int rand_num(int n) {
return n > 0 ? rand() % n : -1;
}

void RegexGenerator::generate_simple_regex() { // <simple-regex> ::= <neg>? буква <star>? |
// <neg>? <lbr><regex><rbr> <star>?
if (rand() % 2 == 0) {
bool prev_eps_counter = all_alts_are_eps;
all_alts_are_eps = true; // новый контроллер эпсилонов

Expand All @@ -140,6 +146,10 @@ void RegexGenerator::generate_simple_regex() { // <simple-regex> ::=
} else {
v2 = 1;
}

if (rand_num(neg_chance) == 0)
res_str += '^';

res_str += '(';
generate_regex_();
res_str += ')';
Expand All @@ -151,6 +161,10 @@ void RegexGenerator::generate_simple_regex() { // <simple-regex> ::=
}
} else {
all_alts_are_eps = false;

if (rand_num(neg_chance) == 0)
res_str += '^';

res_str += rand_symb();

int v2;
Expand All @@ -175,14 +189,18 @@ char RegexGenerator::rand_symb() {
return alphabet[rand() % alphabet.size()];
}

void RegexGenerator::set_neg_chance(int new_neg_chance) {
neg_chance = new_neg_chance;
}
/*
GRAMMAR:
<regex> ::= <n-alt-regex> <alt> <regex> | <conc-regex> | пусто
<n-alt-regex> ::= <conc-regex> | пусто
<conc-regex> ::= <simple-regex> | <simple-regex><conc-regex>
<simple-regex> ::= <lbr><regex><rbr><unary>? | буква <unary>?
<simple-regex> ::= <neg>? <lbr><regex><rbr> <star>? | <neg>? буква <star>?
<alt> ::= '|'
<lbr> ::= '('
<rbr> ::= ')'
<unary> ::= '*'
<star> ::= '*'
<neg> ::= '^'
*/
Loading

0 comments on commit 9c0bbd6

Please sign in to comment.