Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#279) Рефакторинг метода to-regex класса FiniteAutomaton #282

Merged
merged 21 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
928fe1c
Refactored FiniteAutomaton::to_regex #279
AngelicHedgehog Oct 23, 2023
0be5062
Removed artefact #279
AngelicHedgehog Oct 23, 2023
6d8a85d
Merge branch 'main' into vilenskii/refacts
AngelicHedgehog Nov 12, 2023
58b8a27
(#279) удалено старое содержимое метода
AngelicHedgehog Nov 12, 2023
f0fe215
(#279) Первая реализация FiniteAutomaton::to_regex
AngelicHedgehog Nov 22, 2023
7903085
(#279) Добавлено логирование
AngelicHedgehog Nov 22, 2023
7340be4
Merge branch 'main' into vilenskii/refacts
AngelicHedgehog Nov 22, 2023
b5ca6d5
(#279) Устранена ошибка ссылки на rvalue
AngelicHedgehog Nov 22, 2023
e893cb4
(#279) Удалены лишние заголовки
AngelicHedgehog Nov 22, 2023
b4d46ae
(#279) Исправлена Segmentation fault
AngelicHedgehog Nov 22, 2023
ea1e4c3
(#279) Фикс бага формата регулярки возврата
AngelicHedgehog Nov 22, 2023
a4d2e8c
Merge branch 'main' into vilenskii/refacts
AngelicHedgehog Nov 23, 2023
3fe5878
(#279) Устранено лишнее преобразование финальной регулярки
AngelicHedgehog Nov 23, 2023
13c0de8
(#279) Добавлено определение номера начального состояния
AngelicHedgehog Nov 23, 2023
ce9370f
(#279) Излишние выкладки
AngelicHedgehog Nov 23, 2023
e1b2860
(#279) Добавлена генерация афловита с языком
AngelicHedgehog Nov 23, 2023
012327f
(#279) Исправлен метод генерации языка финальной регулярки
AngelicHedgehog Nov 23, 2023
f9e69c0
(#279) Изменён способ задания языка на явный
AngelicHedgehog Nov 23, 2023
bec1d3c
Merge branch 'hotfix' into vilenskii/refacts
xendalm Nov 23, 2023
4bd2149
(#279) fixed arden test, added auto alphabet generating (new AlgExpre…
xendalm Nov 24, 2023
d4d0207
(#302) тест на генерируемых регулярках
mathhyyn Nov 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/MetamorphicTestsApp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ target_link_libraries(${PROJECT_NAME}
)

enable_testing()
add_test(NAME IntegrationTests
add_test(NAME MetamorphicTests
COMMAND ${PROJECT_NAME})
28 changes: 27 additions & 1 deletion apps/MetamorphicTestsApp/src/MetamorphicTests.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
#include "gtest/gtest.h"
#include "gtest/gtest.h"

#include "InputGenerator/RegexGenerator.h"
#include "Objects/FiniteAutomaton.h"
#include "Objects/Regex.h"

using std::string;

TEST(TestCaseName, Test_random_regex_parsing) {
RegexGenerator rg(15, 10, 5, 3);
for (int i = 0; i < 30; i++) {
string str = rg.generate_regex();
Regex r1(str);
string r1_str = r1.to_txt();
Regex r2(r1_str);
ASSERT_EQ(true, Regex::equivalent(r1, r2));
}
}

TEST(TestArden, Test_arden_on_random_regex) {
RegexGenerator rg;
for (int i = 0; i < 30; i++) {
string rgx_str = rg.generate_regex();
Regex r1(rgx_str), r2(rgx_str);
ASSERT_TRUE(Regex::equivalent(r1, r2.to_ilieyu().to_regex()));
}
}
1 change: 0 additions & 1 deletion apps/UnitTestsApp/include/UnitTestsApp/UnitTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <vector>

#include "AutomatonToImage/AutomatonToImage.h"
#include "InputGenerator/RegexGenerator.h"
#include "Interpreter/Interpreter.h"
#include "Objects/AlgExpression.h"
#include "Objects/FiniteAutomaton.h"
Expand Down
23 changes: 6 additions & 17 deletions apps/UnitTestsApp/src/UnitTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,6 @@ TEST(TestNegativeRegex, Test_thompson_negative) {

TEST(TestNegativeRegex, Test_antimirov_negative) {}

TEST(TestCaseName, Test_random_regex_parsing) {
RegexGenerator rg(15, 10, 5, 3);
for (int i = 0; i < 30; i++) {
string str = rg.generate_regex();
Regex r1(str);
string r1_str = r1.to_txt();
Regex r2(r1_str);
ASSERT_EQ(true, Regex::equivalent(r1, r2));
}
}

TEST(TestCaseName, Test_fa_equal) {
vector<FiniteAutomaton::FiniteAutomaton::State> states1;
for (int i = 0; i < 6; i++) {
Expand Down Expand Up @@ -363,13 +352,13 @@ TEST(TestCaseName, Test_remove_trap) {
ASSERT_EQ(fa3.size(), fa3.remove_trap_states().size()); // Кейс, когда ловушек нет.
}

TEST(TestCaseName, Test_arden) {
TEST(TestArden, Test_to_regex_equivalence) {
auto test_equivalence = [](const string& rgx_str) {
Regex reg(rgx_str);
ASSERT_TRUE(Regex::equivalent(reg, reg.to_thompson().to_regex()));
ASSERT_TRUE(Regex::equivalent(reg, reg.to_glushkov().to_regex()));
ASSERT_TRUE(Regex::equivalent(reg, reg.to_ilieyu().to_regex()));
ASSERT_TRUE(Regex::equivalent(reg, reg.to_antimirov().to_regex()));
Regex r1(rgx_str), r2(rgx_str);
ASSERT_TRUE(Regex::equivalent(r1, r2.to_thompson().to_regex()));
ASSERT_TRUE(Regex::equivalent(r1, r2.to_glushkov().to_regex()));
ASSERT_TRUE(Regex::equivalent(r1, r2.to_ilieyu().to_regex()));
ASSERT_TRUE(Regex::equivalent(r1, r2.to_antimirov().to_regex()));
};

test_equivalence("a");
Expand Down
17 changes: 8 additions & 9 deletions libs/Objects/include/Objects/AlgExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,18 @@ class AlgExpression : public BaseObject {

// копирует объект (!!! не чистит память !!!)
virtual void copy(const AlgExpression*) = 0; // NOLINT(build/include_what_you_use)
// возвращает указатель на 'new' объект соответствующего типа
// возвращает указатель на 'new' объект своего типа
virtual AlgExpression* make() const = 0;

void clear();

// Создает новый язык с алфавитом
void set_language(const std::set<Symbol>& _alphabet);
// Рекурсивная генерация алфавита
void generate_alphabet(std::set<Symbol>& _alphabet); // NOLINT(runtime/references)
// Генерация языка из алфавита
// Устанавливает язык
void set_language(const std::shared_ptr<Language>& _language);
// Рекурсивная генерация алфавита во всех нодах
void generate_alphabet();
// Генерация алфавита и создание нового языка
void make_language();

// для print_tree
Expand Down Expand Up @@ -115,15 +117,12 @@ class AlgExpression : public BaseObject {
std::vector<AlgExpression*> get_last_nodes();
std::unordered_map<int, std::vector<int>> pairs() const;

void regex_union(AlgExpression* a, AlgExpression* b);
void regex_alt(AlgExpression* a, AlgExpression* b);
void regex_star(AlgExpression* a);
void regex_eps();

public:
AlgExpression();
AlgExpression(std::shared_ptr<Language>, Type, const Lexeme&, const std::set<Symbol>&);
AlgExpression(std::set<Symbol>); // NOLINT(runtime/explicit)
AlgExpression(Type type, AlgExpression* = nullptr,
AlgExpression* = nullptr); // NOLINT(runtime/explicit)

virtual ~AlgExpression();

Expand Down
2 changes: 2 additions & 0 deletions libs/Objects/include/Objects/BackRefRegex.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class BackRefRegex : public AlgExpression {
public:
BackRefRegex() = default;
BackRefRegex(const std::string&); // NOLINT(runtime/explicit)
BackRefRegex(Type type, AlgExpression* = nullptr,
AlgExpression* = nullptr); // NOLINT(runtime/explicit)

BackRefRegex* make_copy() const override;
BackRefRegex(const BackRefRegex&);
Expand Down
3 changes: 0 additions & 3 deletions libs/Objects/include/Objects/FiniteAutomaton.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ class FiniteAutomaton : public AbstractMachine {
// функция проверки на семантическую детерминированность
bool semdet_entry(bool annoted = false, iLogTemplate* log = nullptr) const;

static std::vector<expression_arden> arden(const std::vector<expression_arden>& in, int index);
static std::vector<expression_arden> arden_minimize(const std::vector<expression_arden>& in);

public:
FiniteAutomaton();
FiniteAutomaton(int initial_state, std::vector<State> states,
Expand Down
2 changes: 2 additions & 0 deletions libs/Objects/include/Objects/Regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class Regex : public AlgExpression {
Regex() = default;
Regex(const std::string&); // NOLINT(runtime/explicit)
Regex(const std::string&, const std::shared_ptr<Language>&);
Regex(Type type, AlgExpression* = nullptr,
AlgExpression* = nullptr); // NOLINT(runtime/explicit)

Regex* make_copy() const override;
Regex(const Regex&) = default;
Expand Down
60 changes: 29 additions & 31 deletions libs/Objects/src/AlgExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ AlgExpression::AlgExpression(std::shared_ptr<Language> language, Type type, cons

AlgExpression::AlgExpression(set<Symbol> alphabet) : BaseObject(std::move(alphabet)) {}

AlgExpression::AlgExpression(Type type, AlgExpression* _term_l, AlgExpression* _term_r)
: type(type) {
if (_term_l) {
term_l = _term_l->make_copy();
alphabet = term_l->alphabet;
}
if (_term_r) {
term_r = _term_r->make_copy();
alphabet.insert(term_r->alphabet.begin(), term_r->alphabet.end());
}
}

AlgExpression::Lexeme::Lexeme(Type type, const Symbol& symbol, int number)
: type(type), symbol(symbol), number(number) {}

Expand Down Expand Up @@ -68,20 +80,28 @@ void AlgExpression::set_language(const set<Symbol>& _alphabet) {
language = make_shared<Language>(alphabet);
}

void AlgExpression::generate_alphabet(set<Symbol>& _alphabet) {
void AlgExpression::set_language(const std::shared_ptr<Language>& _language) {
language = _language;
}

void AlgExpression::generate_alphabet() {
if (type == AlgExpression::symb) {
alphabet = {value.symbol};
return;
}
alphabet.clear();
if (term_l != nullptr) {
term_l->generate_alphabet(alphabet);
term_l->generate_alphabet();
alphabet = term_l->alphabet;
}
if (term_r != nullptr) {
term_r->generate_alphabet(alphabet);
}
for (const auto& symb : alphabet) {
_alphabet.insert(symb);
term_r->generate_alphabet();
alphabet.insert(term_r->alphabet.begin(), term_r->alphabet.end());
}
}

void AlgExpression::make_language() {
generate_alphabet(alphabet);
generate_alphabet();
language = make_shared<Language>(alphabet);
}

Expand Down Expand Up @@ -480,7 +500,7 @@ AlgExpression* AlgExpression::scan_conc(const vector<AlgExpression::Lexeme>& lex
p->term_l = l;
p->term_r = r;
p->value = lexemes[i];
p->type = conc;
p->type = Type::conc;

p->alphabet = l->alphabet;
p->alphabet.insert(r->alphabet.begin(), r->alphabet.end());
Expand Down Expand Up @@ -533,7 +553,6 @@ AlgExpression* AlgExpression::scan_alt(const vector<AlgExpression::Lexeme>& lexe
p = make();
p->term_l = l;
p->term_r = r;

p->value = lexemes[i];
p->type = Type::alt;

Expand Down Expand Up @@ -607,7 +626,7 @@ bool AlgExpression::equality_checker(const AlgExpression* expr1, const AlgExpres
return true;
if (expr1 == nullptr || expr2 == nullptr)
return false;
if (expr1->value.type != expr2->value.type)
if (expr1->type != expr2->type)
return false;

if (expr1->value.type == Lexeme::Type::symb) {
Expand Down Expand Up @@ -750,25 +769,4 @@ unordered_map<int, vector<int>> AlgExpression::pairs() const {
break;
}
return {};
}

void AlgExpression::regex_union(AlgExpression* a, AlgExpression* b) {
type = Type::conc;
term_l = a->make_copy();
term_r = b->make_copy();
}

void AlgExpression::regex_alt(AlgExpression* a, AlgExpression* b) {
type = Type::alt;
term_l = a->make_copy();
term_r = b->make_copy();
}

void AlgExpression::regex_star(AlgExpression* a) {
type = Type::star;
term_l = a->make_copy();
}

void AlgExpression::regex_eps() {
type = Type::eps;
}
3 changes: 3 additions & 0 deletions libs/Objects/src/BackRefRegex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ BackRefRegex::BackRefRegex(const BackRefRegex& other) : AlgExpression(other) {
cell_number = other.cell_number;
}

BackRefRegex::BackRefRegex(Type type, AlgExpression* term_l, AlgExpression* term_r)
: AlgExpression(type, term_l, term_r) {}

void BackRefRegex::copy(const AlgExpression* other) {
auto* tmp = cast(other);
alphabet = tmp->alphabet;
Expand Down
Loading