Make parser template class
This commit is contained in:
parent
419ebb43d1
commit
b3e5bf0941
9 changed files with 50 additions and 94 deletions
|
|
@ -1,4 +0,0 @@
|
||||||
|
|
||||||
#include "AST.hpp"
|
|
||||||
|
|
||||||
using namespace tp;
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
|
|
||||||
#include "GrammarCompiler.hpp"
|
|
||||||
|
|
||||||
using namespace tp;
|
|
||||||
|
|
@ -1,8 +0,0 @@
|
||||||
|
|
||||||
#include "Parser.hpp"
|
|
||||||
|
|
||||||
using namespace tp;
|
|
||||||
|
|
||||||
void Parser::compileTables(const ContextFreeGrammar& cfGrammar, const RegularGrammar& reGrammar) {}
|
|
||||||
|
|
||||||
void Parser::parse(const Sentence& sentence, AST& out) {}
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
|
|
||||||
#include "Sentence.hpp"
|
|
||||||
|
|
||||||
using namespace tp;
|
|
||||||
|
|
@ -1,47 +0,0 @@
|
||||||
|
|
||||||
#include "NewPlacement.hpp"
|
|
||||||
|
|
||||||
#include "SimpleParser.hpp"
|
|
||||||
|
|
||||||
using namespace tp;
|
|
||||||
|
|
||||||
SimpleParser::SimpleParser() {
|
|
||||||
// Grammar for unified grammar format sentence that tables compiled from
|
|
||||||
|
|
||||||
// Define Context-Free grammar
|
|
||||||
ContextFreeGrammar contextFreeGrammar;
|
|
||||||
{
|
|
||||||
// use existing CF grammar interface
|
|
||||||
contextFreeGrammar.addRule("a", { ContextFreeGrammar::Arg("") });
|
|
||||||
contextFreeGrammar.setStart("a");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Define Regular grammar
|
|
||||||
RegularGrammar<uint1, int1> rg;
|
|
||||||
{
|
|
||||||
// this is basically ast from existing tokenizer
|
|
||||||
rg.addRule(rg.seq({ rg.val('a'), rg.val('b') }), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
mUnifiedGrammarParser.compileTables(contextFreeGrammar, rg);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SimpleParser::compileTables(const Sentence& grammar) {
|
|
||||||
AST unifiedGrammarAst;
|
|
||||||
mUnifiedGrammarParser.parse(grammar, unifiedGrammarAst);
|
|
||||||
|
|
||||||
// compile each ast into RegularGrammar and ContextFree Grammar api instructions
|
|
||||||
ContextFreeGrammar userContextFreeGrammar;
|
|
||||||
RegularGrammar<uint1, int1> userRegularGrammar;
|
|
||||||
|
|
||||||
// ...
|
|
||||||
// split ast into RE and CF part
|
|
||||||
// generate and execute grammar api commands
|
|
||||||
// use existing tokenizer code to create RE transition matrix
|
|
||||||
// ...
|
|
||||||
// compile tables from user grammar
|
|
||||||
|
|
||||||
mUserParser.compileTables(userContextFreeGrammar, userRegularGrammar);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SimpleParser::parse(const Sentence& sentence, AST& out) { mUserParser.parse(sentence, out); }
|
|
||||||
|
|
@ -3,16 +3,17 @@
|
||||||
|
|
||||||
#include "AST.hpp"
|
#include "AST.hpp"
|
||||||
#include "GrammarCompiler.hpp"
|
#include "GrammarCompiler.hpp"
|
||||||
#include "Sentence.hpp"
|
|
||||||
|
|
||||||
namespace tp {
|
namespace tp {
|
||||||
|
|
||||||
|
template <typename tAlphabetType>
|
||||||
class Parser {
|
class Parser {
|
||||||
public:
|
public:
|
||||||
Parser() = default;
|
Parser() = default;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void compileTables(const ContextFreeGrammar& cfGrammar, const RegularGrammar& reGrammar);
|
void compileTables(const ContextFreeGrammar& cfGrammar, const RegularGrammar<tAlphabetType, ualni>& reGrammar) {}
|
||||||
void parse(const Sentence& sentence, AST& out);
|
void parse(const tAlphabetType* sentence, ualni sentenceLength, AST& out) {}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// save load compiled tables
|
// save load compiled tables
|
||||||
|
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "LanguageCommon.hpp"
|
|
||||||
|
|
||||||
namespace tp {
|
|
||||||
struct Sentence {
|
|
||||||
const int1* buffer = nullptr;
|
|
||||||
alni symbolSize = 1;
|
|
||||||
|
|
||||||
explicit Sentence(const char* buff) { buffer = buff; }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
@ -5,16 +5,53 @@
|
||||||
namespace tp {
|
namespace tp {
|
||||||
|
|
||||||
// Gives ability to express grammar in the Unified Format as sentence
|
// Gives ability to express grammar in the Unified Format as sentence
|
||||||
|
template <typename tAlphabetType>
|
||||||
class SimpleParser {
|
class SimpleParser {
|
||||||
public:
|
public:
|
||||||
SimpleParser();
|
SimpleParser() {
|
||||||
|
// Grammar for unified grammar format sentence that tables compiled from
|
||||||
|
|
||||||
|
// Define Context-Free grammar
|
||||||
|
ContextFreeGrammar contextFreeGrammar;
|
||||||
|
{
|
||||||
|
// use existing CF grammar interface
|
||||||
|
contextFreeGrammar.addRule("a", { ContextFreeGrammar::Arg("") });
|
||||||
|
contextFreeGrammar.setStart("a");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Define Regular grammar
|
||||||
|
RegularGrammar<tAlphabetType, ualni> rg;
|
||||||
|
{
|
||||||
|
// this is basically ast from existing tokenizer
|
||||||
|
rg.addRule(rg.seq({ rg.val('a'), rg.val('b') }), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
mUnifiedGrammarParser.compileTables(contextFreeGrammar, rg);
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void compileTables(const Sentence& grammar);
|
void compileTables(const tAlphabetType* grammar, ualni grammarLength) {
|
||||||
void parse(const Sentence& sentence, AST& out);
|
AST unifiedGrammarAst;
|
||||||
|
mUnifiedGrammarParser.parse(grammar, grammarLength, unifiedGrammarAst);
|
||||||
|
|
||||||
|
// compile each ast into RegularGrammar and ContextFree Grammar api instructions
|
||||||
|
ContextFreeGrammar userContextFreeGrammar;
|
||||||
|
RegularGrammar<tAlphabetType, ualni> userRegularGrammar;
|
||||||
|
|
||||||
|
// ...
|
||||||
|
// split ast into RE and CF part
|
||||||
|
// generate and execute grammar api commands
|
||||||
|
// use existing tokenizer code to create RE transition matrix
|
||||||
|
// ...
|
||||||
|
// compile tables from user grammar
|
||||||
|
|
||||||
|
mUserParser.compileTables(userContextFreeGrammar, userRegularGrammar);
|
||||||
|
}
|
||||||
|
|
||||||
|
void parse(const tAlphabetType* grammar, ualni grammarLength, AST& out) { mUserParser.parse(grammar, grammarLength, out); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Parser mUnifiedGrammarParser;
|
Parser<tAlphabetType> mUnifiedGrammarParser;
|
||||||
Parser mUserParser;
|
Parser<tAlphabetType> mUserParser;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,15 @@
|
||||||
|
|
||||||
|
#include "NewPlacement.hpp"
|
||||||
#include "Test.hpp"
|
#include "Test.hpp"
|
||||||
|
|
||||||
using namespace tp;
|
using namespace tp;
|
||||||
|
|
||||||
void test() {
|
void test() {
|
||||||
auto parser = SimpleParser();
|
auto parser = SimpleParser<int1>();
|
||||||
auto grammar = Sentence(gGrammar);
|
|
||||||
|
|
||||||
auto sentence = Sentence(gSentence);
|
|
||||||
auto ast = AST();
|
auto ast = AST();
|
||||||
|
|
||||||
parser.compileTables(grammar);
|
parser.compileTables(gGrammar, String::Logic::calcLength(gGrammar));
|
||||||
parser.parse(sentence, ast);
|
parser.parse(gSentence, String::Logic::calcLength(gSentence), ast);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue