Skip to content

Commit

Permalink
(#290) thompson post-merge fix
Browse files Browse the repository at this point in the history
  • Loading branch information
xendalm committed Nov 23, 2023
1 parent 3349cff commit c393144
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
4 changes: 3 additions & 1 deletion apps/UnitTestsApp/src/UnitTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ TEST(ParseStringTest, Test_regex_lexer) {
}
}

TEST(TestCaseThompsonNegative, Test_thompson_negative) {
TEST(TestNegativeRegex, Test_thompson_negative) {
vector<FiniteAutomaton::State> states;
for (int i = 0; i < 9; i++) {
states.emplace_back(i, set<int>{i}, std::to_string(i), false, map<Symbol, set<int>>());
Expand Down Expand Up @@ -93,6 +93,8 @@ TEST(TestCaseThompsonNegative, Test_thompson_negative) {
ASSERT_TRUE(FiniteAutomaton::equal(fa, Regex("(^a|b)c").to_thompson()));
}

TEST(TestNegativeRegex, Test_antimirov_negative) {}

TEST(TestCaseName, Test_random_regex_parsing) {
RegexGenerator rg(15, 10, 5, 3);
for (int i = 0; i < 30; i++) {
Expand Down
1 change: 1 addition & 0 deletions libs/Objects/include/Objects/AlgExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class AlgExpression : public BaseObject {
memoryWriter,
};

// множество уникальных символов алфавита в дереве
std::set<Symbol> alphabet;
Type type;
Lexeme value;
Expand Down
1 change: 1 addition & 0 deletions libs/Objects/include/Objects/FiniteAutomaton.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class FiniteAutomaton : public AbstractMachine {
polynomially_ambigious
};

// !!! меняешь структуру здесь, поменяй в FAState в Regex.h !!!
struct State : AbstractMachine::State {
// используется для объединения состояний в процессе работы алгоритмов
// преобразования автоматов возможно для визуализации
Expand Down
1 change: 1 addition & 0 deletions libs/Objects/include/Objects/Regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
class Language;
class FiniteAutomaton;

// представление FiniteAutomaton::State
struct FAState {
int index;
std::string identifier;
Expand Down
19 changes: 8 additions & 11 deletions libs/Objects/src/Regex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,8 @@ pair<vector<FAState>, int> Regex::get_thompson(int max_index) const {
pair<vector<FAState>, int> fa_right;
// автомат для отрицания, строится обычный томпсон и берется дополнение
FiniteAutomaton fa_negative;

vector<FAState> negative_states;
vector<FAState> res;
vector<FiniteAutomaton::State> states1;
vector<FiniteAutomaton::State> fa_negative_states;
vector<FAState> negative_states_copy;

switch (type) {
case Type::alt: // |
Expand Down Expand Up @@ -286,11 +284,10 @@ pair<vector<FAState>, int> Regex::get_thompson(int max_index) const {
return {fa_states, max_index + 2};
case Type::negative:
// строим автомат для отрицания
res = Regex::cast(term_l)->get_thompson(-1).first;
for (const auto& i : res) {
states1.emplace_back(i.index, i.identifier, i.is_terminal, i.transitions);
for (const auto& i : Regex::cast(term_l)->get_thompson(-1).first) {
fa_negative_states.emplace_back(i.index, i.identifier, i.is_terminal, i.transitions);
}
fa_negative = FiniteAutomaton(0, states1, Regex::cast(term_l)->alphabet);
fa_negative = FiniteAutomaton(0, fa_negative_states, Regex::cast(term_l)->alphabet);
fa_negative = fa_negative.minimize();
// берем дополнение автомата
fa_negative = fa_negative.complement();
Expand All @@ -309,11 +306,11 @@ pair<vector<FAState>, int> Regex::get_thompson(int max_index) const {
max_index++;
fa_negative.states.emplace_back(int(fa_negative.states.size()), id_str, true);

for (const auto& i : res) {
negative_states.emplace_back(i.index, i.identifier, i.is_terminal, i.transitions);
for (const auto& i : fa_negative.states) {
negative_states_copy.emplace_back(i.index, i.identifier, i.is_terminal, i.transitions);
}
// возвращаем состояния и макс индекс
return {negative_states, max_index};
return {negative_states_copy, max_index};
default:
id_str = "q" + to_string(max_index + 1);
fa_states.emplace_back(0, id_str, false);
Expand Down

0 comments on commit c393144

Please sign in to comment.