This commit is contained in:
IlyaShurupov 2024-02-07 19:20:29 +03:00 committed by Ilya Shurupov
parent 6279323a84
commit 70b6bf4d77
8 changed files with 120 additions and 117 deletions

View file

@ -92,7 +92,7 @@ namespace tp {
void addAnyTransition(State* from, State* to) { from->mTransitions.append(Transition(Transition::ANY, to)); }
void setStartVertex(State* start) { mStartState = start; }
void setStartState(State* start) { mStartState = start; }
[[nodiscard]] State* getStartState() const { return mStartState; }

View file

@ -0,0 +1,18 @@
#pragma once
#include "Strings.hpp"
#include "Automata.hpp"
#include "Buffer2D.hpp"
namespace tp {
template <typename tAlphabetType, typename tStateType>
class ContextFreeAutomata {
public:
ContextFreeAutomata() = default;
void construct(const FiniteStateAutomation<tAlphabetType, tStateType>& automata) {}
};
}

View file

@ -18,7 +18,7 @@ namespace tp {
struct Item {
Grammar::Rule mRule;
ualni mAdvanceIdx;
ualni mAdvanceIdx = 0;
};
typedef FiniteStateAutomation<Alphabet, Item> NFA;

View file

@ -193,6 +193,7 @@ namespace tp {
explicit ClassNode(const Buffer<Range<tAlphabetType>>& ranges, bool exclude = false) :
Node(Node::CLASS) {
mExclude = exclude;
mRanges = ranges;
}
~ClassNode() override { mRanges.removeAll(); }

View file

@ -2,45 +2,49 @@
#pragma once
#include "AST.hpp"
#include "RegularCompiler.hpp"
#include "RegularAutomata.hpp"
#include "ContextFreeCompiler.hpp"
#include "ContextFreeAutomata.hpp"
namespace tp {
template <typename tAlphabetType, typename TokenType>
template <typename tAlphabetType, typename TokenType, ualni MinSymbol, ualni MaxSymbol>
class Parser {
typedef RegularAutomata<tAlphabetType, TokenType, TokenType::InTransition, TokenType::Failed> RegularTable;
typedef RegularGrammar<tAlphabetType, TokenType> RegularGrammar;
typedef RegularCompiler<tAlphabetType, TokenType, MinSymbol, MaxSymbol> RegularCompiler;
typedef FiniteStateAutomation<tAlphabetType, TokenType> RegularGraph;
typedef RegularCompiler<tAlphabetType, TokenType, TokenType::InTransition, TokenType::Failed> RegularCompiler;
typedef FiniteStateAutomation<tAlphabetType, TokenType> RegularNonDetGraph;
typedef RegularAutomata<tAlphabetType, TokenType, TokenType::InTransition, TokenType::Failed> RegularAutomata;
// ContextFreeCompiler::Alphabet PushDownTable;
typedef FiniteStateAutomation<ContextFreeCompiler::Alphabet, ContextFreeCompiler::Item> ContextFreeDFA;
typedef ContextFreeCompiler::NFA ContextFreeNFA;
// ContextFreeGrammar;
// ContextFreeCompiler;
typedef FiniteStateAutomation<ContextFreeCompiler::Alphabet, ContextFreeCompiler::Item> ContextFreeGraph;
typedef ContextFreeAutomata<ContextFreeCompiler::Alphabet, ContextFreeCompiler::Item> ContextFreeAutomata;
public:
Parser() = default;
public:
void compileTables(const ContextFreeGrammar& cfGrammar, const RegularGrammar<tAlphabetType, TokenType>& reGrammar) {
void compileTables(const ContextFreeGrammar& cfGrammar, const RegularGrammar& reGrammar) {
// Compile Regular Grammar
{
RegularNonDetGraph nfa;
RegularGraph graph;
RegularCompiler compiler;
compiler.compile(nfa, reGrammar);
RegularGraph dfa(nfa);
mRegularTable.construct(dfa);
compiler.compile(graph, reGrammar);
graph.makeDeterministic();
mRegularAutomata.construct(graph);
}
// compile context free grammar
{
ContextFreeNFA nfa;
ContextFreeGraph graph;
ContextFreeCompiler compiler;
compiler.compile(cfGrammar, nfa);
ContextFreeDFA dfa(nfa);
compiler.compile(cfGrammar, graph);
graph.makeDeterministic();
mContextFreeAutomata.construct(graph);
}
}
@ -48,6 +52,7 @@ namespace tp {
public:
// save load compiled tables
RegularTable mRegularTable;
RegularAutomata mRegularAutomata;
ContextFreeAutomata mContextFreeAutomata;
};
}

View file

@ -27,50 +27,6 @@ namespace tp {
auto getTransitions() const { return &mTransitions; }
auto getStart() const { return mStart; }
void construct(const FiniteStateAutomation<tAlphabetType, tStateType>& automata) {
mSymbolRange = { tAlphabetType(automata.getAlphabetRange().mBegin),
tAlphabetType(automata.getAlphabetRange().mEnd) };
auto range_len = ualni(mSymbolRange.mEnd - mSymbolRange.mBegin);
auto sizeX = range_len ? range_len : 1;
auto sizeY = (ualni) (automata.numStates() + 1);
mTransitions.reserve({ sizeX, sizeY });
mTransitions.assign(automata.numStates());
mStates.reserve(sizeY);
ualni idx = 0;
for (auto state : *automata.getStates()) {
auto stateVal = state->isAccepting() ? state->getStateVal() : tNoStateVal;
mStates[idx] = stateVal;
idx++;
}
mStates[automata.numStates()] = tFailedStateVal;
idx = 0;
for (auto state : *automata.getStates()) {
if (&state.data() == automata.getStartState()) {
mStart = mIter = mIterPrev = idx;
}
idx++;
}
ualni stateIdx = 0;
for (auto state : *automata.getStates()) {
for (auto transition : *state->getTransitions()) {
ualni stateIdx2 = 0;
for (auto state2 : *automata.getStates()) {
if (transition->getState() == &state2.data()) break;
stateIdx2++;
}
auto const code = transition->getSymbol();
mTransitions.set({ (ualni) (code - mSymbolRange.mBegin), (ualni) stateIdx }, stateIdx2);
}
stateIdx++;
}
}
bool isTrapped() { return mStates[mIter] == tFailedStateVal; }
tStateType move(tAlphabetType symbol) {
@ -112,5 +68,49 @@ namespace tp {
mIter = mStart;
mIterPrev = mStart;
}
void construct(const FiniteStateAutomation<tAlphabetType, tStateType>& automata) {
const auto range = automata.getAlphabetRange();
mSymbolRange = { tAlphabetType(range.mBegin), tAlphabetType(range.mEnd) };
auto range_len = ualni(mSymbolRange.mEnd - mSymbolRange.mBegin);
auto sizeX = range_len ? range_len : 1;
auto sizeY = (ualni) (automata.numStates() + 1);
mTransitions.reserve({ sizeX, sizeY });
mTransitions.assign(automata.numStates());
mStates.reserve(sizeY);
ualni idx = 0;
for (auto state : *automata.getStates()) {
auto stateVal = state->isAccepting() ? state->getStateVal() : tNoStateVal;
mStates[idx] = stateVal;
idx++;
}
mStates[automata.numStates()] = tFailedStateVal;
idx = 0;
for (auto state : *automata.getStates()) {
if (&state.data() == automata.getStartState()) {
mStart = mIter = mIterPrev = idx;
}
idx++;
}
ualni stateIdx = 0;
for (auto state : *automata.getStates()) {
for (auto transition : *state->getTransitions()) {
ualni stateIdx2 = 0;
for (auto state2 : *automata.getStates()) {
if (transition->getState() == &state2.data()) break;
stateIdx2++;
}
auto const code = transition->getSymbol();
mTransitions.set({ (ualni) (code - mSymbolRange.mBegin), (ualni) stateIdx }, stateIdx2);
}
stateIdx++;
}
}
};
}

View file

@ -2,11 +2,11 @@
#pragma once
#include "Grammar.hpp"
#include "RegularAutomata.hpp"
#include "Automata.hpp"
namespace tp {
template <typename tAlphabetType, typename tStateType, tStateType tNoStateVal, tStateType tFailedStateVal>
template <typename tAlphabetType, typename tStateType, ualni tMinSymbol, ualni tMaxSymbol>
class RegularCompiler {
typedef FiniteStateAutomation<tAlphabetType, tStateType> Graph;
@ -34,7 +34,6 @@ namespace tp {
void compile(Graph& graph, const tAlphabetType* regex, tStateType state) {
mGraph = &graph;
compileUtil(regex, state);
setAlphabet();
}
void compile(Graph& aGraph, const Grammar& grammar) {
@ -59,9 +58,7 @@ namespace tp {
idx++;
}
mGraph->setStartVertex(left);
setAlphabet();
mGraph->setStartState(left);
}
private:
@ -69,9 +66,10 @@ namespace tp {
auto node = compileNode(astNode, nullptr, nullptr);
// mGraph->setVertexState(node.right, state, true);
// mGraph->setStartVertex(node.left);
ASSERT(0);
node.right->setValue(state);
node.right->setAcceptance(true);
mGraph->setStartState(node.left);
return node;
}
@ -129,13 +127,13 @@ namespace tp {
if (node->mRanges.size() == 1) {
auto const& range = node->mRanges.first();
transitionRange(left, right, { range.mBegin, range.mEnd }, node->mExclude);
transitionRange(left, right, { ualni(range.mBegin), ualni(range.mEnd) }, node->mExclude);
return { left, right };
}
for (auto range : node->mRanges) {
auto middle = addVertex();
transitionRange(left, middle, { range.data().mBegin, range.data().mEnd }, node->mExclude);
transitionRange(left, middle, { ualni(range->mBegin), ualni(range->mEnd) }, node->mExclude);
transitionAny(middle, right);
}
return { left, right };
@ -174,56 +172,37 @@ namespace tp {
}
void transitionAny(Vertex* from, Vertex* to, bool consumes = false) {
// mGraph->addTransition(from, to, {}, consumes, true, false);
ASSERT(0);
for (auto symbol : Range<ualni>(tMinSymbol, tMaxSymbol)) {
transitionVal(from, to, symbol);
}
}
void transitionVal(Vertex* from, Vertex* to, tAlphabetType val) {
// mGraph->addTransition(from, to, { val, val }, true, false, false);
ASSERT(0);
}
void transitionVal(Vertex* from, Vertex* to, tAlphabetType val) { mGraph->addTransition(from, to, val); }
void transitionRange(Vertex* from, Vertex* to, Range<tAlphabetType> range, bool exclude) {
// mGraph->addTransition(from, to, range, true, false, exclude);
ASSERT(0);
}
void transitionRange(Vertex* from, Vertex* to, Range<ualni> range, bool exclude) {
if (exclude) {
Range<ualni> first = { tMinSymbol, range.mBegin - 1 };
Range<ualni> second = { range.mEnd + 1, tMaxSymbol };
Vertex* addVertex() {
// return mGraph->addVertex(tNoStateVal, false);
ASSERT(0);
return nullptr;
}
if (first.valid()) {
for (auto symbol : first) {
transitionVal(from, to, symbol);
}
}
private:
Range<tAlphabetType> getAlphabetRange() const {
/*
tAlphabetType start = 0, end = 0;
Range<tAlphabetType> all_range(
std::numeric_limits<tAlphabetType>::min(), std::numeric_limits<tAlphabetType>::max()
);
bool first = true;
if (second.valid()) {
for (auto symbol : second) {
transitionVal(from, to, symbol);
}
}
for (auto vertex : mGraph->mVertices) {
for (auto edge : vertex.data().edges) {
if (!edge.data().mConsumesSymbol) continue;
auto const& tran_range = edge.data().mAcceptingRange;
if (edge.data().mAcceptsAll || edge.data().mExclude) return all_range;
if (tran_range.mBegin < start || first) start = tran_range.mBegin;
if (tran_range.mEnd > end || first) end = tran_range.mEnd;
first = false;
} else {
for (auto symbol : range) {
transitionVal(from, to, symbol);
}
}
return Range<tAlphabetType>(start, end + 1);
*/
ASSERT(0);
return {};
}
void setAlphabet() {
auto range = getAlphabetRange();
for (auto iter : range) {
// mGraph->mAllSymbols.append(iter);
}
}
Vertex* addVertex() { return mGraph->addState(tStateType::InTransition, false); }
};
}

View file

@ -12,7 +12,7 @@ void testAutomation() {
automata.addTransition(start, end, 'a');
automata.setStartVertex(start);
automata.setStartState(start);
automata.makeDeterministic();
}