parsing loop closed
This commit is contained in:
parent
63b0560375
commit
3bed74df87
8 changed files with 92 additions and 45 deletions
|
|
@ -1,8 +1,6 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "AST.hpp"
|
||||
|
||||
#include "RegularCompiler.hpp"
|
||||
#include "RegularAutomata.hpp"
|
||||
|
||||
|
|
@ -11,24 +9,34 @@
|
|||
|
||||
namespace tp {
|
||||
|
||||
template <typename tAlphabetType, typename TokenType, ualni MinSymbol, ualni MaxSymbol>
|
||||
template <typename tAlphabetType, typename tTokenType, tTokenType tInTransition, ualni MinSymbol, ualni MaxSymbol>
|
||||
class Parser {
|
||||
|
||||
typedef RegularGrammar<tAlphabetType, TokenType> RegularGrammar;
|
||||
typedef RegularCompiler<tAlphabetType, TokenType, MinSymbol, MaxSymbol> RegularCompiler;
|
||||
typedef FiniteStateAutomation<tAlphabetType, TokenType> RegularGraph;
|
||||
typedef RegularAutomata<tAlphabetType, TokenType> RegularAutomata;
|
||||
typedef RegularGrammar<tAlphabetType, tTokenType> RegularGrammar;
|
||||
typedef RegularCompiler<tAlphabetType, tTokenType, tInTransition, MinSymbol, MaxSymbol> RegularCompiler;
|
||||
typedef FiniteStateAutomation<tAlphabetType, tTokenType> RegularGraph;
|
||||
typedef RegularAutomata<tAlphabetType, tTokenType> RegularAutomata;
|
||||
|
||||
// ContextFreeGrammar;
|
||||
// ContextFreeCompiler;
|
||||
typedef FiniteStateAutomation<ContextFreeCompiler::SymbolID, ContextFreeCompiler::Item> ContextFreeGraph;
|
||||
typedef ContextFreeAutomata<ContextFreeCompiler::SymbolID, ContextFreeCompiler::Item> ContextFreeAutomata;
|
||||
typedef FiniteStateAutomation<ContextFreeCompiler::SymbolId, ContextFreeCompiler::Item> ContextFreeGraph;
|
||||
typedef ContextFreeAutomata<ContextFreeCompiler::SymbolId, ContextFreeCompiler::Item> ContextFreeAutomata;
|
||||
|
||||
public:
|
||||
struct ParseResult {
|
||||
bool accepted = false;
|
||||
const ContextFreeAutomata::StackItem* ast = nullptr;
|
||||
};
|
||||
|
||||
public:
|
||||
Parser() = default;
|
||||
|
||||
public:
|
||||
bool compileTables(const ContextFreeGrammar& cfGrammar, const RegularGrammar& reGrammar) {
|
||||
bool compileTables(
|
||||
const ContextFreeGrammar& cfGrammar,
|
||||
const RegularGrammar& reGrammar,
|
||||
const Map<String, tTokenType>& contextFreeToRegular
|
||||
) {
|
||||
// Compile Regular Grammar
|
||||
{
|
||||
RegularGraph graph;
|
||||
|
|
@ -45,16 +53,53 @@ namespace tp {
|
|||
compiler.compile(cfGrammar, graph);
|
||||
graph.makeDeterministic();
|
||||
mContextFreeAutomata.construct(graph);
|
||||
|
||||
// make glue
|
||||
for (auto symbol : *compiler.getSymbols()) {
|
||||
auto symbolId = compiler.getSymbolId(symbol->mId);
|
||||
if (symbol->mIsTerminal) {
|
||||
auto iter = contextFreeToRegular.presents(symbol->mId);
|
||||
if (!iter) return false;
|
||||
mGrammarGlue.put(contextFreeToRegular.getSlotVal(iter), symbolId);
|
||||
} else {
|
||||
mAstNames.put(symbolId, symbol->mId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void parse(const tAlphabetType* sentence, ualni sentenceLength, AST& out) {}
|
||||
ParseResult parse(const tAlphabetType* sentence, ualni sentenceLength) {
|
||||
// get tokens stream
|
||||
Buffer<ContextFreeCompiler::SymbolId> tokens;
|
||||
|
||||
const tAlphabetType* sentenceIter = sentence;
|
||||
ualni lengthIter = sentenceLength;
|
||||
|
||||
while (lengthIter) {
|
||||
auto result = mRegularAutomata.accept(sentenceIter, lengthIter);
|
||||
|
||||
if (!result.accepted) {
|
||||
return { false, nullptr };
|
||||
}
|
||||
|
||||
sentenceIter += result.advancedIdx;
|
||||
lengthIter -= result.advancedIdx;
|
||||
|
||||
tokens.append(mGrammarGlue.get(result.state));
|
||||
}
|
||||
|
||||
ContextFreeAutomata::AcceptResult result = mContextFreeAutomata.accept(tokens.getBuff(), tokens.size());
|
||||
return { result.accepted, result.ast };
|
||||
}
|
||||
|
||||
public:
|
||||
// save load compiled tables
|
||||
RegularAutomata mRegularAutomata;
|
||||
ContextFreeAutomata mContextFreeAutomata;
|
||||
|
||||
Map<tTokenType, ContextFreeCompiler::SymbolId> mGrammarGlue;
|
||||
Map<ContextFreeCompiler::SymbolId, String> mAstNames;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue