This commit is contained in:
IlyaShurupov 2024-02-07 18:22:42 +03:00 committed by Ilusha
parent dd6b63acb1
commit 81a2247bf2
4 changed files with 18 additions and 14 deletions

View file

@ -56,12 +56,12 @@ namespace tp {
public:
void setValue(const tStateType& stateValue) { mStateVal = stateValue; }
void setAcceptance(bool isAccepting) { mIsAccepting = isAccepting; }
bool isAccepting() const { return mIsAccepting; }
[[nodiscard]] bool isAccepting() const { return mIsAccepting; }
const tStateType& getStateVal() const { return mStateVal; }
const Buffer<Transition>* getTransitions() const { return &mTransitions; }
[[nodiscard]] const Buffer<Transition>* getTransitions() const { return &mTransitions; }
private:
Buffer<Transition> mTransitions;
Buffer<Transition> mTransitions{};
tStateType mStateVal = tStateType();
bool mIsAccepting = false;
};
@ -69,6 +69,7 @@ namespace tp {
private:
List<State> mStates;
State* mStartState = nullptr;
Range<ualni> mAlphabetRange = { ENV_UALNI_MAX, ENV_UALNI_MIN };
public:
FiniteStateAutomation() = default;
@ -83,6 +84,8 @@ namespace tp {
void addTransition(State* from, State* to, const tAlphabetType& symbol) {
from->mTransitions.append(Transition(Transition::SYMBOL, to, symbol));
if (mAlphabetRange.mBegin < ualni(symbol)) mAlphabetRange.mBegin = ualni(symbol);
if (mAlphabetRange.mEnd > ualni(symbol)) mAlphabetRange.mEnd = ualni(symbol);
}
void addEpsilonTransition(State* from, State* to) { from->mTransitions.append(Transition(Transition::SYMBOL, to)); }
@ -104,11 +107,13 @@ namespace tp {
[[nodiscard]] const List<State>* getStates() const { return &mStates; }
[[nodiscard]] Range<ualni> getAlphabetRange() const { return mAlphabetRange; }
private:
typedef AvlTree<AvlNumericKey<State*>, bool> StatesSet;
// Expands initial set with states that are reachable from initial set with no input consumption (E-transitions)
void expandSet(StatesSet& set) const {
static void expandSet(StatesSet& set) {
List<State*> workingSet;
set.forEach([&](AvlNumericKey<State*>& key, bool) { workingSet.pushBack(key.val); });
@ -128,7 +133,7 @@ namespace tp {
}
// States that are reachable from initial set with symbol transition
void findMoveSet(StatesSet& from, StatesSet& moveSet, tAlphabetType symbol) const {
static void findMoveSet(StatesSet& from, StatesSet& moveSet, tAlphabetType symbol) {
from.forEach([&](AvlNumericKey<State*>& key, bool) {
for (auto transition : key.val->mTransitions) {
if (transition->isEpsilon()) continue;
@ -140,8 +145,7 @@ namespace tp {
}
public:
template <typename tAlphabetIterator>
bool makeDeterministic(const tAlphabetIterator& allSymbols) {
bool makeDeterministic() {
if (!isValid()) return false;
struct GroupKey {
@ -175,7 +179,7 @@ namespace tp {
StatesSet* group = workingSet.first()->data;
GroupInfo* info = &groupInfos.get({ group });
for (auto symbol : allSymbols) {
for (auto symbol : getAlphabetRange()) {
// calculate new possible state
StatesSet potentialGroup;

View file

@ -10,7 +10,7 @@ namespace tp {
template <typename tAlphabetType, typename TokenType>
class Parser {
typedef TransitionMatrix<tAlphabetType, TokenType, TokenType::InTransition, TokenType::Failed> RegularTable;
typedef RegularAutomata<tAlphabetType, TokenType, TokenType::InTransition, TokenType::Failed> RegularTable;
typedef FiniteStateAutomation<tAlphabetType, TokenType> RegularGraph;
typedef RegularCompiler<tAlphabetType, TokenType, TokenType::InTransition, TokenType::Failed> RegularCompiler;
typedef FiniteStateAutomation<tAlphabetType, TokenType> RegularNonDetGraph;

View file

@ -8,7 +8,7 @@
namespace tp {
template <typename tAlphabetType, typename tStateType, tStateType tNoStateVal, tStateType tFailedStateVal>
class TransitionMatrix {
class RegularAutomata {
static_assert(TypeTraits<tAlphabetType>::isIntegral, "tAlphabetType must be enumerable.");
@ -21,15 +21,15 @@ namespace tp {
ualni mStart = 0;
public:
TransitionMatrix() = default;
RegularAutomata() = default;
auto getStates() const { return &mStates; }
auto getTransitions() const { return &mTransitions; }
auto getStart() const { return mStart; }
void construct(const FiniteStateAutomation<tAlphabetType, tStateType>& automata) {
// mSymbolRange = { dfa.getRange().first(), dfa.getRange().last() };
ASSERT(0);
mSymbolRange = { tAlphabetType(automata.getAlphabetRange().mBegin),
tAlphabetType(automata.getAlphabetRange().mEnd) };
auto range_len = ualni(mSymbolRange.mEnd - mSymbolRange.mBegin);
auto sizeX = range_len ? range_len : 1;

View file

@ -14,7 +14,7 @@ void testAutomation() {
automata.setStartVertex(start);
automata.makeDeterministic(Range<char>(10, 30));
automata.makeDeterministic();
}
void test() {