Skip to content

Commit

Permalink
(#301) избавился от костыля в to_txt
Browse files Browse the repository at this point in the history
  • Loading branch information
dak151449 committed Jan 3, 2024
1 parent 9f2796a commit 73ca3af
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 25 deletions.
2 changes: 1 addition & 1 deletion libs/Objects/include/Objects/AlgExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class AlgExpression : public BaseObject {
AlgExpression(const AlgExpression&);
AlgExpression& operator=(const AlgExpression& other);

std::string to_txt(bool eps_is_empty = true) const override;
std::string to_txt() const override;
// вывод дерева для дебага
void print_tree() const;
void print_dot() const;
Expand Down
2 changes: 1 addition & 1 deletion libs/Objects/include/Objects/BackRefRegex.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class BackRefRegex : public AlgExpression {
template <typename T>
static std::vector<BackRefRegex*> cast(std::vector<T*> ptr, bool not_null_ptr = true);

std::string to_txt(bool eps_is_empty = true) const override;
std::string to_txt() const override;

MemoryFiniteAutomaton to_mfa(iLogTemplate* log = nullptr) const;
};
2 changes: 1 addition & 1 deletion libs/Objects/include/Objects/BaseObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BaseObject {
BaseObject();
explicit BaseObject(std::shared_ptr<Language>);
explicit BaseObject(std::set<Symbol>);
virtual std::string to_txt(bool eps_is_empty = true) const = 0;
virtual std::string to_txt() const = 0;

std::shared_ptr<Language> get_language() const;
};
2 changes: 1 addition & 1 deletion libs/Objects/include/Objects/FiniteAutomaton.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class FiniteAutomaton : public AbstractMachine {
// dynamic_cast unique_ptr к типу FiniteAutomaton*
template <typename T> static FiniteAutomaton* cast(std::unique_ptr<T>&& uptr);
// визуализация автомата
std::string to_txt(bool eps_is_empty = true) const override;
std::string to_txt() const override;

std::vector<FAState> get_states() const;
// детерминизация ДКА
Expand Down
2 changes: 1 addition & 1 deletion libs/Objects/include/Objects/MemoryFiniteAutomaton.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class MemoryFiniteAutomaton : public AbstractMachine {
// dynamic_cast unique_ptr к типу MemoryFiniteAutomaton*
template <typename T> MemoryFiniteAutomaton* cast(std::unique_ptr<T>&& uptr);
// визуализация автомата
std::string to_txt(bool eps_is_empty = true) const override;
std::string to_txt() const override;

// возвращает количество состояний (метод States)
size_t size(iLogTemplate* log = nullptr) const;
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 @@ -45,6 +45,8 @@ class Regex : public AlgExpression {
// применение ACI правил
static Regex* to_aci(std::vector<Regex>& res); // NOLINT(runtime/references)
static Regex* add_alt(std::vector<Regex> res, Regex* root);
// печатание регулярного выражения с _eps_ и _empty_
std::string _antimirov_to_txt() const;

// возвращает вектор сотсояний нового автомата, построенного из регулярного выражения
std::vector<FAState> _to_thompson(const std::set<Symbol>& root_alphabet_symbol) const;
Expand Down
10 changes: 4 additions & 6 deletions libs/Objects/src/AlgExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ bool AlgExpression::is_terminal_type(Type t) {
return t == Type::symb || t == Type::memoryWriter || t == Type::ref;
}

string AlgExpression::to_txt(bool eps_is_empty) const {
string AlgExpression::to_txt() const {
string str1, str2;
if (term_l) {
str1 = term_l->to_txt(eps_is_empty);
str1 = term_l->to_txt();
}
if (term_r) {
str2 = term_r->to_txt(eps_is_empty);
str2 = term_r->to_txt();
}
string symb;
switch (type) {
Expand All @@ -131,9 +131,7 @@ string AlgExpression::to_txt(bool eps_is_empty) const {
symb = symbol;
break;
case Type::eps:
if (!eps_is_empty) {
symb = Symbol::Epsilon;
}
symb = "";
break;
case Type::alt:
symb = '|';
Expand Down
2 changes: 1 addition & 1 deletion libs/Objects/src/BackRefRegex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ template <typename T> vector<BackRefRegex*> BackRefRegex::cast(vector<T*> ptrs,
return regexPointers;
}

string BackRefRegex::to_txt(bool eps_is_empty) const {
string BackRefRegex::to_txt() const {
BackRefRegex *br_term_l, *br_term_r;
string str1, str2;
if (term_l) {
Expand Down
2 changes: 1 addition & 1 deletion libs/Objects/src/FiniteAutomaton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ template <typename T> FiniteAutomaton* FiniteAutomaton::cast(std::unique_ptr<T>&
return fa;
}

string FiniteAutomaton::to_txt(bool eps_is_empty) const {
string FiniteAutomaton::to_txt() const {
std::stringstream ss;
ss << "digraph {\n\trankdir = LR\n\tdummy [label = \"\", shape = none]\n\t";
for (int i = 0; i < states.size(); i++) {
Expand Down
2 changes: 1 addition & 1 deletion libs/Objects/src/MemoryFiniteAutomaton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ MemoryFiniteAutomaton* MemoryFiniteAutomaton::cast(std::unique_ptr<T>&& uptr) {
return mfa;
}

string MemoryFiniteAutomaton::to_txt(bool eps_is_empty) const {
string MemoryFiniteAutomaton::to_txt() const {
stringstream ss;
ss << "digraph {\n\trankdir = LR\n\tdummy [label = \"\", shape = none]\n\t";
for (int i = 0; i < states.size(); i++) {
Expand Down
67 changes: 56 additions & 11 deletions libs/Objects/src/Regex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ vector<FAState> Regex::_to_thompson(const set<Symbol>& root_alphabet) const {
}
}

fa_negative.states.emplace_back(int(fa_negative.size()), "q", true);
fa_negative.states.emplace_back(int(fa_negative.size()), true);

// возвращаем состояния и макс индекс
return fa_negative.states;
Expand Down Expand Up @@ -714,7 +714,7 @@ Regex* Regex::add_alt(std::vector<Regex> res, Regex* root) {
Regex* Regex::to_aci(std::vector<Regex>& res) {
// отсортировали вектор регулярок
std::sort(res.begin(), res.end(), [](const Regex& i, const Regex& j) {
return i.to_txt(false) < j.to_txt(false);
return i._antimirov_to_txt() < j._antimirov_to_txt();
});

// rule w | w = w
Expand All @@ -725,7 +725,7 @@ Regex* Regex::to_aci(std::vector<Regex>& res) {
if (i == j)
continue;

if (res[i].to_txt() == res[j].to_txt()) {
if (res[i]._antimirov_to_txt() == res[j]._antimirov_to_txt()) {
res.erase(res.begin() + j);
}
}
Expand All @@ -744,6 +744,51 @@ Regex* Regex::to_aci(std::vector<Regex>& res) {
res_alt->term_l = add_alt(res, cast(res_alt->term_l, false));
return res_alt;
}
std::string Regex::_antimirov_to_txt() const {
string str1, str2;
if (term_l) {
str1 = Regex::cast(term_l)->_antimirov_to_txt();
}
if (term_r) {
str2 = Regex::cast(term_r)->_antimirov_to_txt();
}
string symb;
switch (type) {
case Type::conc:
if (term_l && Regex::cast(term_l)->type == Type::alt) {
str1 = "(" + str1 + ")";
}
if (term_r && Regex::cast(term_r)->type == Type::alt) {
str2 = "(" + str2 + ")";
}
break;
case Type::symb:
symb = symbol;
break;
case Type::eps:
symb = Symbol::Epsilon;
break;
case Type::alt:
symb = '|';
break;
case Type::star:
symb = '*';
if (!is_terminal_type(Regex::cast(term_l)->type))
// ставим скобки при итерации, если символов > 1
str1 = "(" + str1 + ")";
break;
case Type::negative:
symb = '^';
if (!is_terminal_type(Regex::cast(term_l)->type)) {
return symb + "(" + str1 + ")";
}
return symb + str1;
default:
break;
}

return str1 + symb + str2;
}

bool Regex::partial_derivative_with_respect_to_sym(Regex* respected_sym, const Regex* reg_e,
vector<Regex>& result) const {
Expand Down Expand Up @@ -1011,7 +1056,7 @@ FiniteAutomaton Regex::to_antimirov(iLogTemplate* log) const {
}

fa_states.push_back(*this);
check.insert(to_txt(false));
check.insert(_antimirov_to_txt());
for (size_t i = 0; i < fa_states.size(); i++) {
Regex regex_state = fa_states[i];
for (const auto& s : symbols) {
Expand All @@ -1021,7 +1066,7 @@ FiniteAutomaton Regex::to_antimirov(iLogTemplate* log) const {
for (const auto& reg_der : regs_der) {
partial_derivatives_by_regex.push_back({regex_state, reg_der, s});
size_t old_checks = check.size();
check.insert(reg_der.to_txt(false));
check.insert(reg_der._antimirov_to_txt());
if (old_checks != check.size()){
fa_states.push_back(reg_der);
}
Expand All @@ -1032,7 +1077,7 @@ FiniteAutomaton Regex::to_antimirov(iLogTemplate* log) const {
vector<string> name_states;

for (auto& state : fa_states) {
name_states.push_back(state.to_txt(false));
name_states.push_back(state._antimirov_to_txt());
}

vector<FAState> automat_state;
Expand All @@ -1046,18 +1091,18 @@ FiniteAutomaton Regex::to_antimirov(iLogTemplate* log) const {
// cout << partial_derivativ[0].to_txt() << " ";
// cout << partial_derivativ[1].to_txt() << " ";
// cout << partial_derivativ[2].to_txt() << endl;
deriv_log += partial_derivativ[2].to_txt(false) + "(" + partial_derivativ[0].to_txt(false) + ")" + "\\ =\\ ";
deriv_log += partial_derivativ[2]._antimirov_to_txt() + "(" + partial_derivativ[0]._antimirov_to_txt() + ")" + "\\ =\\ ";
if (partial_derivativ[1].to_txt() == "") {
deriv_log += "eps\\\\";
} else {
deriv_log += partial_derivativ[1].to_txt(false) + "\\\\";
deriv_log += partial_derivativ[1]._antimirov_to_txt() + "\\\\";
}

if (partial_derivativ[0].to_txt(false) == state) {
if (partial_derivativ[0]._antimirov_to_txt() == state) {
// поиск индекс состояния в которое переходим по символу из state
auto elem_iter = find(name_states.begin(), name_states.end(), partial_derivativ[1].to_txt(false));
auto elem_iter = find(name_states.begin(), name_states.end(), partial_derivativ[1]._antimirov_to_txt());
// записываем расстояние между begin и итератором, который указывает на состояние
transit[partial_derivativ[2].to_txt(false)].insert(std::distance(name_states.begin(), elem_iter));
transit[partial_derivativ[2]._antimirov_to_txt()].insert(std::distance(name_states.begin(), elem_iter));
}
}

Expand Down

0 comments on commit 73ca3af

Please sign in to comment.