Make parser template class
This commit is contained in:
parent
419ebb43d1
commit
b3e5bf0941
9 changed files with 50 additions and 94 deletions
|
|
@ -5,16 +5,53 @@
|
|||
namespace tp {
|
||||
|
||||
// Gives ability to express grammar in the Unified Format as sentence
|
||||
template <typename tAlphabetType>
|
||||
class SimpleParser {
|
||||
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:
|
||||
void compileTables(const Sentence& grammar);
|
||||
void parse(const Sentence& sentence, AST& out);
|
||||
void compileTables(const tAlphabetType* grammar, ualni grammarLength) {
|
||||
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:
|
||||
Parser mUnifiedGrammarParser;
|
||||
Parser mUserParser;
|
||||
Parser<tAlphabetType> mUnifiedGrammarParser;
|
||||
Parser<tAlphabetType> mUserParser;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue