tmp
This commit is contained in:
parent
226c62f40b
commit
6279323a84
4 changed files with 18 additions and 14 deletions
|
|
@ -56,12 +56,12 @@ namespace tp {
|
||||||
public:
|
public:
|
||||||
void setValue(const tStateType& stateValue) { mStateVal = stateValue; }
|
void setValue(const tStateType& stateValue) { mStateVal = stateValue; }
|
||||||
void setAcceptance(bool isAccepting) { mIsAccepting = isAccepting; }
|
void setAcceptance(bool isAccepting) { mIsAccepting = isAccepting; }
|
||||||
bool isAccepting() const { return mIsAccepting; }
|
[[nodiscard]] bool isAccepting() const { return mIsAccepting; }
|
||||||
const tStateType& getStateVal() const { return mStateVal; }
|
const tStateType& getStateVal() const { return mStateVal; }
|
||||||
const Buffer<Transition>* getTransitions() const { return &mTransitions; }
|
[[nodiscard]] const Buffer<Transition>* getTransitions() const { return &mTransitions; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Buffer<Transition> mTransitions;
|
Buffer<Transition> mTransitions{};
|
||||||
tStateType mStateVal = tStateType();
|
tStateType mStateVal = tStateType();
|
||||||
bool mIsAccepting = false;
|
bool mIsAccepting = false;
|
||||||
};
|
};
|
||||||
|
|
@ -69,6 +69,7 @@ namespace tp {
|
||||||
private:
|
private:
|
||||||
List<State> mStates;
|
List<State> mStates;
|
||||||
State* mStartState = nullptr;
|
State* mStartState = nullptr;
|
||||||
|
Range<ualni> mAlphabetRange = { ENV_UALNI_MAX, ENV_UALNI_MIN };
|
||||||
|
|
||||||
public:
|
public:
|
||||||
FiniteStateAutomation() = default;
|
FiniteStateAutomation() = default;
|
||||||
|
|
@ -83,6 +84,8 @@ namespace tp {
|
||||||
|
|
||||||
void addTransition(State* from, State* to, const tAlphabetType& symbol) {
|
void addTransition(State* from, State* to, const tAlphabetType& symbol) {
|
||||||
from->mTransitions.append(Transition(Transition::SYMBOL, to, 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)); }
|
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]] const List<State>* getStates() const { return &mStates; }
|
||||||
|
|
||||||
|
[[nodiscard]] Range<ualni> getAlphabetRange() const { return mAlphabetRange; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
typedef AvlTree<AvlNumericKey<State*>, bool> StatesSet;
|
typedef AvlTree<AvlNumericKey<State*>, bool> StatesSet;
|
||||||
|
|
||||||
// Expands initial set with states that are reachable from initial set with no input consumption (E-transitions)
|
// 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;
|
List<State*> workingSet;
|
||||||
|
|
||||||
set.forEach([&](AvlNumericKey<State*>& key, bool) { workingSet.pushBack(key.val); });
|
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
|
// 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) {
|
from.forEach([&](AvlNumericKey<State*>& key, bool) {
|
||||||
for (auto transition : key.val->mTransitions) {
|
for (auto transition : key.val->mTransitions) {
|
||||||
if (transition->isEpsilon()) continue;
|
if (transition->isEpsilon()) continue;
|
||||||
|
|
@ -140,8 +145,7 @@ namespace tp {
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
template <typename tAlphabetIterator>
|
bool makeDeterministic() {
|
||||||
bool makeDeterministic(const tAlphabetIterator& allSymbols) {
|
|
||||||
if (!isValid()) return false;
|
if (!isValid()) return false;
|
||||||
|
|
||||||
struct GroupKey {
|
struct GroupKey {
|
||||||
|
|
@ -175,7 +179,7 @@ namespace tp {
|
||||||
StatesSet* group = workingSet.first()->data;
|
StatesSet* group = workingSet.first()->data;
|
||||||
GroupInfo* info = &groupInfos.get({ group });
|
GroupInfo* info = &groupInfos.get({ group });
|
||||||
|
|
||||||
for (auto symbol : allSymbols) {
|
for (auto symbol : getAlphabetRange()) {
|
||||||
|
|
||||||
// calculate new possible state
|
// calculate new possible state
|
||||||
StatesSet potentialGroup;
|
StatesSet potentialGroup;
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ namespace tp {
|
||||||
template <typename tAlphabetType, typename TokenType>
|
template <typename tAlphabetType, typename TokenType>
|
||||||
class Parser {
|
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 FiniteStateAutomation<tAlphabetType, TokenType> RegularGraph;
|
||||||
typedef RegularCompiler<tAlphabetType, TokenType, TokenType::InTransition, TokenType::Failed> RegularCompiler;
|
typedef RegularCompiler<tAlphabetType, TokenType, TokenType::InTransition, TokenType::Failed> RegularCompiler;
|
||||||
typedef FiniteStateAutomation<tAlphabetType, TokenType> RegularNonDetGraph;
|
typedef FiniteStateAutomation<tAlphabetType, TokenType> RegularNonDetGraph;
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@
|
||||||
namespace tp {
|
namespace tp {
|
||||||
|
|
||||||
template <typename tAlphabetType, typename tStateType, tStateType tNoStateVal, tStateType tFailedStateVal>
|
template <typename tAlphabetType, typename tStateType, tStateType tNoStateVal, tStateType tFailedStateVal>
|
||||||
class TransitionMatrix {
|
class RegularAutomata {
|
||||||
|
|
||||||
static_assert(TypeTraits<tAlphabetType>::isIntegral, "tAlphabetType must be enumerable.");
|
static_assert(TypeTraits<tAlphabetType>::isIntegral, "tAlphabetType must be enumerable.");
|
||||||
|
|
||||||
|
|
@ -21,15 +21,15 @@ namespace tp {
|
||||||
ualni mStart = 0;
|
ualni mStart = 0;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TransitionMatrix() = default;
|
RegularAutomata() = default;
|
||||||
|
|
||||||
auto getStates() const { return &mStates; }
|
auto getStates() const { return &mStates; }
|
||||||
auto getTransitions() const { return &mTransitions; }
|
auto getTransitions() const { return &mTransitions; }
|
||||||
auto getStart() const { return mStart; }
|
auto getStart() const { return mStart; }
|
||||||
|
|
||||||
void construct(const FiniteStateAutomation<tAlphabetType, tStateType>& automata) {
|
void construct(const FiniteStateAutomation<tAlphabetType, tStateType>& automata) {
|
||||||
// mSymbolRange = { dfa.getRange().first(), dfa.getRange().last() };
|
mSymbolRange = { tAlphabetType(automata.getAlphabetRange().mBegin),
|
||||||
ASSERT(0);
|
tAlphabetType(automata.getAlphabetRange().mEnd) };
|
||||||
|
|
||||||
auto range_len = ualni(mSymbolRange.mEnd - mSymbolRange.mBegin);
|
auto range_len = ualni(mSymbolRange.mEnd - mSymbolRange.mBegin);
|
||||||
auto sizeX = range_len ? range_len : 1;
|
auto sizeX = range_len ? range_len : 1;
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ void testAutomation() {
|
||||||
|
|
||||||
automata.setStartVertex(start);
|
automata.setStartVertex(start);
|
||||||
|
|
||||||
automata.makeDeterministic(Range<char>(10, 30));
|
automata.makeDeterministic();
|
||||||
}
|
}
|
||||||
|
|
||||||
void test() {
|
void test() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue