Skip to content

Commit

Permalink
(#290) генерация регулярок с отрицанием
Browse files Browse the repository at this point in the history
  • Loading branch information
mathhyyn committed Nov 19, 2023
1 parent 2dd9cbf commit 5351c06
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
7 changes: 4 additions & 3 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 Down
1 change: 1 addition & 0 deletions apps/UnitTestsApp/src/UnitTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ TEST(ParseStringTest, Test_regex_lexer) {
}

TEST(TestCaseName, Test_random_regex_parsing) {
// RegexGenerator rg(2); // для регулярок с отрицанием
RegexGenerator rg(15, 10, 5, 3);
for (int i = 0; i < 30; i++) {
string str = rg.generate_regex();
Expand Down
4 changes: 4 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;
string res_str = "";
void generate_regex_();
Expand All @@ -33,6 +34,8 @@ class RegexGenerator {
8 - максимальная длина, 3 - максимальное кол-во звезд,
2 - максимальная звездная вложенность, 2 - число символов в алфавите*/
RegexGenerator();
// генерация регулярок с отрицанием
RegexGenerator(int neg_chance);
/*генератор регулярных выражений, параметрирозованных длиной, кол-вом
итераций Клини и звездной вложенностью*/
RegexGenerator(int regex_length, int star_num, int star_nesting);
Expand All @@ -47,4 +50,5 @@ class RegexGenerator {
string generate_framed_regex();
/*запись регулярки в файл*/
void write_to_file(string filename);
void set_neg_chance(int new_neg_chance);
};
36 changes: 27 additions & 9 deletions libs/InputGenerator/src/RegexGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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 @@ -95,8 +99,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 @@ -106,11 +109,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 @@ -137,6 +143,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 @@ -148,6 +158,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 @@ -172,14 +186,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> ::= '^'
*/

0 comments on commit 5351c06

Please sign in to comment.