Skip to content

Commit

Permalink
(#301) изменил вспомогательный метод set_initial_state_zero
Browse files Browse the repository at this point in the history
  • Loading branch information
dak151449 committed Dec 28, 2023
1 parent afd1941 commit d8b896c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 29 deletions.
2 changes: 1 addition & 1 deletion apps/MetamorphicTestsApp/src/MetamorphicTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ TEST(TestNFA, Test_equivalent_nfa_negative) {
for (int i = 0; i < 30; i++) {
string str = rg.generate_regex();
Regex r1(str), r2(str);
ASSERT_TRUE(FiniteAutomaton::equivalent(r1.to_thompson().minimize(), r2.to_antimirov().minimize()));
ASSERT_TRUE(FiniteAutomaton::equivalent(r1.to_thompson(), r2.to_antimirov()));
}
}
36 changes: 18 additions & 18 deletions apps/UnitTestsApp/src/UnitTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,37 +64,37 @@ TEST(ParseStringTest, Test_regex_lexer) {

TEST(TestNegativeRegex, Test_thompson_negative) {
vector<FiniteAutomaton::State> states;
for (int i = 0; i < 10; i++) {
for (int i = 0; i < 9; i++) {
states.emplace_back(i, set<int>{i}, std::to_string(i), false, map<Symbol, set<int>>());
}

states[0].set_transition(1, Symbol::epsilon());
states[0].set_transition(6, Symbol::epsilon());
states[0].set_transition(5, Symbol::epsilon());

states[1].set_transition(2, Symbol::epsilon());
states[1].set_transition(4, Symbol::epsilon());
states[1].set_transition(2, "a");
states[1].set_transition(3, "b");
states[1].set_transition(3, "c");
states[1].set_transition(4, Symbol::epsilon());

states[2].set_transition(3, "a");
states[2].set_transition(4, "b");
states[2].set_transition(4, "c");
states[2].set_transition(5, Symbol::epsilon());
states[2].set_transition(3, "b");
states[2].set_transition(3, "c");

states[3].set_transition(4, "a");
states[3].set_transition(4, "b");
states[3].set_transition(4, "c");
states[3].set_transition(3, "a");
states[3].set_transition(3, "b");
states[3].set_transition(3, "c");
states[3].set_transition(4, Symbol::epsilon());

states[4].set_transition(4, "a");
states[4].set_transition(4, "b");
states[4].set_transition(4, "c");
states[4].set_transition(5, Symbol::epsilon());
states[4].set_transition(7, Symbol::epsilon());

states[7].set_transition(8, Symbol::epsilon());
states[7].set_transition(8, "c");

states[5].set_transition(8, Symbol::epsilon());
states[5].set_transition(6, "b");

states[6].set_transition(7, "b");
states[6].set_transition(7, Symbol::epsilon());

states[8].set_transition(9, "c");
states[9].is_terminal = true;
states[8].is_terminal = true;
FiniteAutomaton fa(0, states, {"a", "b", "c"});

ASSERT_TRUE(FiniteAutomaton::equal(fa, Regex("(^a|b)c").to_thompson()));
Expand Down
2 changes: 1 addition & 1 deletion libs/Objects/include/Objects/Regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Regex : public AlgExpression {
bool derivative_with_respect_to_str(std::string str, const Regex* reg_e,
Regex& result) const; // NOLINT(runtime/references)
// применение ACI правил
Regex* to_aci(std::vector<Regex>& res) const; // NOLINT(runtime/explicit)
Regex* to_aci(std::vector<Regex>& res) const; // NOLINT(runtime/references)
Regex* add_alt(std::vector<Regex> res, Regex* root) const;

std::vector<FAState> get_thompson(const std::set<Symbol>& root_alphabet_symbol) const;
Expand Down
25 changes: 16 additions & 9 deletions libs/Objects/src/FiniteAutomaton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2538,19 +2538,26 @@ Regex FiniteAutomaton::to_regex(iLogTemplate* log) const {

void FiniteAutomaton::set_initial_state_zero()
{
int init = get_initial();
for (int i = 0; i < states.size(); i++) {
states[i].index++;
for (auto elem: states[i].transitions) {
auto& s = states[i].transitions.at(elem.first);
std::set<int> new_trans;
for(const auto& index: s) {
new_trans.insert(index + 1);
for (auto elem: states[i].transitions) {
auto& s = states[i].transitions.at(elem.first);
std::set<int> new_trans;
for(const auto& index: s) {
if (index == init) {
new_trans.insert(0);
continue;
}
if (index == 0) {
new_trans.insert(init);
continue;
}
states[i].transitions[elem.first] = new_trans;
new_trans.insert(index);
}
states[i].transitions[elem.first] = new_trans;
}
}

states.emplace(states.begin(), 0, "q", false);
states[0].set_transition(get_initial() + 1, Symbol::epsilon());
std::swap(states[0], states[init]);
initial_state = 0;
}

0 comments on commit d8b896c

Please sign in to comment.