From 3ba4bef93b48775a01676dacd7dcd9ceefda71dc Mon Sep 17 00:00:00 2001 From: IlyaShurupov Date: Mon, 12 Feb 2024 16:39:12 +0300 Subject: [PATCH] small fix --- Language/public/Automata.hpp | 4 ++-- Language/public/ContextFreeAutomata.hpp | 2 +- Language/public/ContextFreeCompiler.hpp | 31 ++++++++++++++++++++----- Language/public/Parser.hpp | 16 +++++++------ 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/Language/public/Automata.hpp b/Language/public/Automata.hpp index e89ff06..0e059b9 100644 --- a/Language/public/Automata.hpp +++ b/Language/public/Automata.hpp @@ -184,7 +184,7 @@ namespace tp { // calculate new possible state StatesSet potentialGroup; - findMoveSet(*group, potentialGroup, symbol); + findMoveSet(*group, potentialGroup, tAlphabetType(symbol)); expandSet(potentialGroup); if (!potentialGroup.size()) continue; @@ -201,7 +201,7 @@ namespace tp { } // assert transition is added - info->transitions.insert(targetGroup, symbol); + info->transitions.insert(targetGroup, tAlphabetType(symbol)); } workingSet.popFront(); diff --git a/Language/public/ContextFreeAutomata.hpp b/Language/public/ContextFreeAutomata.hpp index 42c783f..9c4f698 100644 --- a/Language/public/ContextFreeAutomata.hpp +++ b/Language/public/ContextFreeAutomata.hpp @@ -132,6 +132,6 @@ namespace tp { ualni mStartState = 0; ualni mCurrentState = 0; - Range mRange = { 0, 0 }; + Range mRange; }; } diff --git a/Language/public/ContextFreeCompiler.hpp b/Language/public/ContextFreeCompiler.hpp index 8434e0d..db05b61 100644 --- a/Language/public/ContextFreeCompiler.hpp +++ b/Language/public/ContextFreeCompiler.hpp @@ -8,7 +8,26 @@ namespace tp { class ContextFreeCompiler { public: - typedef ualni SymbolId; + struct SymbolVal { + SymbolVal() = default; + + SymbolVal(ualni aId, ualni aStart, ualni aLen) { + id = aId; + start = aStart; + len = aLen; + }; + + SymbolVal(ualni val) { id = val; } + + operator ualni() const { return id; } + + bool operator==(const SymbolVal& in) const { return in.id == id; } + SymbolVal& operator=(const SymbolVal& in) = default; + + ualni id = 0; + ualni start = 0; + ualni len = 0; + }; struct Item { const ContextFreeGrammar::Rule* mRule = nullptr; @@ -48,13 +67,13 @@ namespace tp { }; public: - bool compile(const ContextFreeGrammar& grammar, FiniteStateAutomation& automata) { + bool compile(const ContextFreeGrammar& grammar, FiniteStateAutomation& automata) { if (!init(grammar)) return false; return true; } [[nodiscard]] const Buffer* getSymbols() const { return &mSymbols; } - [[nodiscard]] SymbolId getSymbolId(const String& name) const { return mSymbolLookup.get(name); } + [[nodiscard]] SymbolVal getSymbolId(const String& name) const { return mSymbolLookup.get(name); } private: bool init(const ContextFreeGrammar& grammar) { @@ -138,7 +157,7 @@ namespace tp { void initSymbols(const ContextFreeGrammar& grammar) { for (auto nonTerminal : mNonTerminals) { mSymbols.append({ nonTerminal->key, false }); - mSymbolLookup.put(nonTerminal->key, SymbolId(mSymbols.size() - 1)); + mSymbolLookup.put(nonTerminal->key, SymbolVal(mSymbols.size() - 1)); for (auto rule : nonTerminal->val.rules) { for (auto arg : *rule->getArgs()) { @@ -147,7 +166,7 @@ namespace tp { mTerminals.put(arg->getId(), {}); mSymbols.append({ nonTerminal->key, true }); - mSymbolLookup.put(nonTerminal->key, SymbolId(mSymbols.size() - 1)); + mSymbolLookup.put(nonTerminal->key, SymbolVal(mSymbols.size() - 1)); } } } @@ -157,6 +176,6 @@ namespace tp { Map mNonTerminals; Map mTerminals; Buffer mSymbols; - Map mSymbolLookup; + Map mSymbolLookup; }; } diff --git a/Language/public/Parser.hpp b/Language/public/Parser.hpp index 59d0767..10985ae 100644 --- a/Language/public/Parser.hpp +++ b/Language/public/Parser.hpp @@ -19,8 +19,8 @@ namespace tp { // ContextFreeGrammar; // ContextFreeCompiler; - typedef FiniteStateAutomation ContextFreeGraph; - typedef ContextFreeAutomata ContextFreeAutomata; + typedef FiniteStateAutomation ContextFreeGraph; + typedef ContextFreeAutomata ContextFreeAutomata; public: struct ParseResult { @@ -72,7 +72,7 @@ namespace tp { ParseResult parse(const tAlphabetType* sentence, ualni sentenceLength) { // get tokens stream - Buffer tokens; + Buffer tokens; const tAlphabetType* sentenceIter = sentence; ualni lengthIter = sentenceLength; @@ -84,10 +84,12 @@ namespace tp { return { false, nullptr }; } + tokens.append(ContextFreeCompiler::SymbolVal( + mGrammarGlue.get(result.state), ualni(sentenceIter - sentence), result.advancedIdx + )); + sentenceIter += result.advancedIdx; lengthIter -= result.advancedIdx; - - tokens.append(mGrammarGlue.get(result.state)); } ContextFreeAutomata::AcceptResult result = mContextFreeAutomata.accept(tokens.getBuff(), tokens.size()); @@ -99,7 +101,7 @@ namespace tp { RegularAutomata mRegularAutomata; ContextFreeAutomata mContextFreeAutomata; - Map mGrammarGlue; - Map mAstNames; + Map mGrammarGlue; + Map mAstNames; }; }