small fix

This commit is contained in:
IlyaShurupov 2024-02-12 16:39:12 +03:00 committed by Ilusha
parent 3bed74df87
commit 3ba4bef93b
4 changed files with 37 additions and 16 deletions

View file

@ -184,7 +184,7 @@ namespace tp {
// calculate new possible state // calculate new possible state
StatesSet potentialGroup; StatesSet potentialGroup;
findMoveSet(*group, potentialGroup, symbol); findMoveSet(*group, potentialGroup, tAlphabetType(symbol));
expandSet(potentialGroup); expandSet(potentialGroup);
if (!potentialGroup.size()) continue; if (!potentialGroup.size()) continue;
@ -201,7 +201,7 @@ namespace tp {
} }
// assert transition is added // assert transition is added
info->transitions.insert(targetGroup, symbol); info->transitions.insert(targetGroup, tAlphabetType(symbol));
} }
workingSet.popFront(); workingSet.popFront();

View file

@ -132,6 +132,6 @@ namespace tp {
ualni mStartState = 0; ualni mStartState = 0;
ualni mCurrentState = 0; ualni mCurrentState = 0;
Range<tAlphabetType> mRange = { 0, 0 }; Range<ualni> mRange;
}; };
} }

View file

@ -8,7 +8,26 @@ namespace tp {
class ContextFreeCompiler { class ContextFreeCompiler {
public: 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 { struct Item {
const ContextFreeGrammar::Rule* mRule = nullptr; const ContextFreeGrammar::Rule* mRule = nullptr;
@ -48,13 +67,13 @@ namespace tp {
}; };
public: public:
bool compile(const ContextFreeGrammar& grammar, FiniteStateAutomation<SymbolId, Item>& automata) { bool compile(const ContextFreeGrammar& grammar, FiniteStateAutomation<SymbolVal, Item>& automata) {
if (!init(grammar)) return false; if (!init(grammar)) return false;
return true; return true;
} }
[[nodiscard]] const Buffer<Symbol>* getSymbols() const { return &mSymbols; } [[nodiscard]] const Buffer<Symbol>* 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: private:
bool init(const ContextFreeGrammar& grammar) { bool init(const ContextFreeGrammar& grammar) {
@ -138,7 +157,7 @@ namespace tp {
void initSymbols(const ContextFreeGrammar& grammar) { void initSymbols(const ContextFreeGrammar& grammar) {
for (auto nonTerminal : mNonTerminals) { for (auto nonTerminal : mNonTerminals) {
mSymbols.append({ nonTerminal->key, false }); 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 rule : nonTerminal->val.rules) {
for (auto arg : *rule->getArgs()) { for (auto arg : *rule->getArgs()) {
@ -147,7 +166,7 @@ namespace tp {
mTerminals.put(arg->getId(), {}); mTerminals.put(arg->getId(), {});
mSymbols.append({ nonTerminal->key, true }); 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<String, NonTerminal> mNonTerminals; Map<String, NonTerminal> mNonTerminals;
Map<String, bool> mTerminals; Map<String, bool> mTerminals;
Buffer<Symbol> mSymbols; Buffer<Symbol> mSymbols;
Map<String, SymbolId> mSymbolLookup; Map<String, SymbolVal> mSymbolLookup;
}; };
} }

View file

@ -19,8 +19,8 @@ namespace tp {
// ContextFreeGrammar; // ContextFreeGrammar;
// ContextFreeCompiler; // ContextFreeCompiler;
typedef FiniteStateAutomation<ContextFreeCompiler::SymbolId, ContextFreeCompiler::Item> ContextFreeGraph; typedef FiniteStateAutomation<ContextFreeCompiler::SymbolVal, ContextFreeCompiler::Item> ContextFreeGraph;
typedef ContextFreeAutomata<ContextFreeCompiler::SymbolId, ContextFreeCompiler::Item> ContextFreeAutomata; typedef ContextFreeAutomata<ContextFreeCompiler::SymbolVal, ContextFreeCompiler::Item> ContextFreeAutomata;
public: public:
struct ParseResult { struct ParseResult {
@ -72,7 +72,7 @@ namespace tp {
ParseResult parse(const tAlphabetType* sentence, ualni sentenceLength) { ParseResult parse(const tAlphabetType* sentence, ualni sentenceLength) {
// get tokens stream // get tokens stream
Buffer<ContextFreeCompiler::SymbolId> tokens; Buffer<ContextFreeCompiler::SymbolVal> tokens;
const tAlphabetType* sentenceIter = sentence; const tAlphabetType* sentenceIter = sentence;
ualni lengthIter = sentenceLength; ualni lengthIter = sentenceLength;
@ -84,10 +84,12 @@ namespace tp {
return { false, nullptr }; return { false, nullptr };
} }
tokens.append(ContextFreeCompiler::SymbolVal(
mGrammarGlue.get(result.state), ualni(sentenceIter - sentence), result.advancedIdx
));
sentenceIter += result.advancedIdx; sentenceIter += result.advancedIdx;
lengthIter -= result.advancedIdx; lengthIter -= result.advancedIdx;
tokens.append(mGrammarGlue.get(result.state));
} }
ContextFreeAutomata::AcceptResult result = mContextFreeAutomata.accept(tokens.getBuff(), tokens.size()); ContextFreeAutomata::AcceptResult result = mContextFreeAutomata.accept(tokens.getBuff(), tokens.size());
@ -99,7 +101,7 @@ namespace tp {
RegularAutomata mRegularAutomata; RegularAutomata mRegularAutomata;
ContextFreeAutomata mContextFreeAutomata; ContextFreeAutomata mContextFreeAutomata;
Map<tTokenType, ContextFreeCompiler::SymbolId> mGrammarGlue; Map<tTokenType, ContextFreeCompiler::SymbolVal> mGrammarGlue;
Map<ContextFreeCompiler::SymbolId, String> mAstNames; Map<ContextFreeCompiler::SymbolVal, String> mAstNames;
}; };
} }