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 059adc8
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 4 deletions.
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
3 changes: 3 additions & 0 deletions 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
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 059adc8

Please sign in to comment.