Skip to content

Commit

Permalink
(#330) MFA alphabet fix
Browse files Browse the repository at this point in the history
  • Loading branch information
xendalm committed Jun 16, 2024
1 parent 7d04e97 commit 99410d5
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 5 deletions.
4 changes: 4 additions & 0 deletions apps/UnitTestsApp/src/UnitTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ TEST(TestParseString, FromString) {
{"a|(c^)", false, false, 8}, // a | ( c . ^ eps )
{"[b[a]:1&1]:2&2^a", true, true},
{"[b[a]:1&1]:2&2^a", true, false},
{"[a]:1[]:1&1", false, true, 9},
{"[a]:1[(|)]:1&1", true, true},
{"O1aC1R1&1", false, false, 9},
{"O1aC1X1&1", true, false},
};

for (const auto& t : tests) {
Expand Down
3 changes: 2 additions & 1 deletion libs/Interpreter/src/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,8 @@ optional<Interpreter::Expression> Interpreter::scan_expression(const vector<Lexe
if (end > pos && lexems[pos].type == Lexem::regex) {
string str = lexems[pos].value;
// выбор между backref и regex
if (str.find("&") != string::npos || str.find(":") != string::npos) {
// TODO: костыль
if (str.find('&') != string::npos || str.find("]:") != string::npos) {
expr.type = ObjectType::BRefRegex;
expr.value = BackRefRegex(str);
} else {
Expand Down
6 changes: 4 additions & 2 deletions libs/Objects/include/Objects/Symbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <vector>

// Символ, по которому осуществляются переходы в автомате.
// Может быть символом-буквой (и входить в алфавит) или ссылкой (&i)
// Может быть символом-буквой (и входить ТОЛЬКО в алфавит FA) или ссылкой (&i)
class Symbol {
private:
std::vector<int> annote_numbers;
Expand Down Expand Up @@ -90,4 +90,6 @@ class MemorySymbols {
static bool is_open(const Symbol& s);

static int get_cell_number(const Symbol& s);
};
};

bool is_special_symbol(const Symbol& s);
1 change: 1 addition & 0 deletions libs/Objects/src/AlgExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ vector<AlgExpression::Lexeme> AlgExpression::parse_string(string str, bool allow
return {Lexeme::Type::error};

if (allow_ref)
// не будет входить в алфавит, только для обозначения перехода в MFA
lexeme.type = Lexeme::Type::ref;
else
lexeme.type = Lexeme::Type::symb;
Expand Down
6 changes: 5 additions & 1 deletion libs/Objects/src/FiniteAutomaton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2702,11 +2702,15 @@ void FiniteAutomaton::to_mfa_dfs(int state_index, vector<bool>& visited,

MemoryFiniteAutomaton FiniteAutomaton::to_mfa() const {
vector<MFAState> mfa_states;
Alphabet alphabet;
mfa_states.emplace_back(0, states[initial_state].identifier, states[initial_state].is_terminal);
vector<bool> visited(size(), false);
unordered_map<int, int> states_mapping;
to_mfa_dfs(initial_state, visited, mfa_states, states_mapping, {}, 0);
return {initial_state, mfa_states, language->get_alphabet()};
for (const auto& symbol : language->get_alphabet())
if (!is_special_symbol(symbol))
alphabet.insert(symbol);
return {initial_state, mfa_states, alphabet};
}

void FiniteAutomaton::fill_order(int state_index, vector<bool>& visited, stack<int>& order) {
Expand Down
6 changes: 5 additions & 1 deletion libs/Objects/src/MemoryFiniteAutomaton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ MemoryFiniteAutomaton::MemoryFiniteAutomaton(int initial_state, std::vector<MFAS
throw std::logic_error(
"State.index must correspond to its ordinal number in the states vector");
}
for (const auto& symbol : language->get_alphabet())
if (is_special_symbol(symbol))
throw std::logic_error("alphabet of MFA mustn't contain special symbols");
}

template <typename T>
Expand Down Expand Up @@ -1345,7 +1348,8 @@ MemoryFiniteAutomaton MemoryFiniteAutomaton::get_subautomaton(const vector<int>&
for (const auto& [symbol, symbol_transitions] : states[state_index].transitions)
for (const auto& tr : symbol_transitions)
if (indexes.count(tr.to)) {
alphabet.insert(symbol);
if (!is_special_symbol(symbol))
alphabet.insert(symbol);
sub_states[indexes.at(state_index)].add_transition(
MFATransition(indexes.at(tr.to), tr.memory_actions), symbol);
}
Expand Down
4 changes: 4 additions & 0 deletions libs/Objects/src/Symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,7 @@ int MemorySymbols::get_cell_number(const Symbol& s) {
int number = stoi(number_str);
return number;
}

bool is_special_symbol(const Symbol& s) {
return s.is_ref() || MemorySymbols::is_memory_symbol(s);
}

0 comments on commit 99410d5

Please sign in to comment.