tmp
This commit is contained in:
parent
060aaecbeb
commit
7ed6b1f33c
2 changed files with 167 additions and 6 deletions
|
|
@ -1,4 +1,6 @@
|
||||||
|
|
||||||
|
#include "NewPlacement.hpp"
|
||||||
|
|
||||||
#include "SimpleParser.hpp"
|
#include "SimpleParser.hpp"
|
||||||
|
|
||||||
using namespace tp;
|
using namespace tp;
|
||||||
|
|
@ -22,14 +24,13 @@ SimpleParser::SimpleParser() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define Regular grammar
|
// Define Regular grammar
|
||||||
RegularGrammar regularGrammar;
|
RegularGrammar<uint1, int1> rg;
|
||||||
{
|
{
|
||||||
// this is basically ast from existing tokenizer
|
// this is basically ast from existing tokenizer
|
||||||
regularGrammar.addRule(regularGrammar.alternate(regularGrammar.repeat(regularGrammar.symbols("asd")), regularGrammar.symbol("a")));
|
rg.addRule(rg.seq({ rg.val('a'), rg.val('b') }), 0);
|
||||||
regularGrammar.addRule(regularGrammar.alternate(regularGrammar.repeat(regularGrammar.symbols("asd")), regularGrammar.symbol("a")));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mUnifiedGrammarParser.compileTables(contextFreeGrammar, regularGrammar);
|
mUnifiedGrammarParser.compileTables(contextFreeGrammar, rg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SimpleParser::compileTables(const Sentence& grammar) {
|
void SimpleParser::compileTables(const Sentence& grammar) {
|
||||||
|
|
@ -38,7 +39,7 @@ void SimpleParser::compileTables(const Sentence& grammar) {
|
||||||
|
|
||||||
// compile each ast into RegularGrammar and ContextFree Grammar api instructions
|
// compile each ast into RegularGrammar and ContextFree Grammar api instructions
|
||||||
ContextFreeGrammar userContextFreeGrammar;
|
ContextFreeGrammar userContextFreeGrammar;
|
||||||
RegularGrammar userRegularGrammar;
|
RegularGrammar<uint1, int1> userRegularGrammar;
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
// split ast into RE and CF part
|
// split ast into RE and CF part
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,169 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "Buffer.hpp"
|
||||||
#include "LanguageCommon.hpp"
|
#include "LanguageCommon.hpp"
|
||||||
|
#include "Utils.hpp"
|
||||||
|
|
||||||
namespace tp {
|
namespace tp {
|
||||||
|
|
||||||
class ContextFreeGrammar {};
|
class ContextFreeGrammar {};
|
||||||
class RegularGrammar {};
|
|
||||||
|
template <typename tAlphabetType, typename tTokType>
|
||||||
|
class RegularGrammar {
|
||||||
|
|
||||||
|
struct Node {
|
||||||
|
enum Type {
|
||||||
|
NONE,
|
||||||
|
ANY,
|
||||||
|
OR,
|
||||||
|
IF,
|
||||||
|
CLASS,
|
||||||
|
COMPOUND,
|
||||||
|
REPEAT,
|
||||||
|
VAL,
|
||||||
|
} mType = NONE;
|
||||||
|
|
||||||
|
explicit Node(Type type) :
|
||||||
|
mType(type) {}
|
||||||
|
|
||||||
|
virtual ~Node() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
class ValueNode : public Node {
|
||||||
|
public:
|
||||||
|
explicit ValueNode(tAlphabetType val) :
|
||||||
|
mVal(val),
|
||||||
|
Node(Node::VAL) {}
|
||||||
|
|
||||||
|
~ValueNode() override = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
tAlphabetType mVal;
|
||||||
|
};
|
||||||
|
|
||||||
|
class CompoundNode : public Node {
|
||||||
|
public:
|
||||||
|
CompoundNode() :
|
||||||
|
Node(Node::COMPOUND) {}
|
||||||
|
|
||||||
|
CompoundNode(const InitialierList<const Node*>& nodes) :
|
||||||
|
Node(Node::COMPOUND) {
|
||||||
|
mSequence = nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
~CompoundNode() override {
|
||||||
|
for (auto iter : mSequence) {
|
||||||
|
delete iter.data();
|
||||||
|
}
|
||||||
|
mSequence.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Buffer<const Node*> mSequence;
|
||||||
|
};
|
||||||
|
|
||||||
|
class AlternationNode : public Node {
|
||||||
|
public:
|
||||||
|
AlternationNode() :
|
||||||
|
Node(Node::OR) {}
|
||||||
|
|
||||||
|
AlternationNode(const Node* a, const Node* b) :
|
||||||
|
Node(Node::OR) {
|
||||||
|
mFirst = a;
|
||||||
|
mSecond = b;
|
||||||
|
}
|
||||||
|
|
||||||
|
~AlternationNode() override {
|
||||||
|
delete mFirst;
|
||||||
|
delete mSecond;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
const Node* mFirst = nullptr;
|
||||||
|
const Node* mSecond = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
class IfNode : public Node {
|
||||||
|
public:
|
||||||
|
IfNode() :
|
||||||
|
Node(Node::IF) {}
|
||||||
|
|
||||||
|
explicit IfNode(const Node* a) :
|
||||||
|
Node(Node::IF) {
|
||||||
|
mNode = a;
|
||||||
|
}
|
||||||
|
|
||||||
|
~IfNode() override { delete mNode; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
const Node* mNode = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
class AnyNode : public Node {
|
||||||
|
public:
|
||||||
|
AnyNode() :
|
||||||
|
Node(Node::ANY) {}
|
||||||
|
|
||||||
|
~AnyNode() override = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
class RepetitionNode : public Node {
|
||||||
|
public:
|
||||||
|
RepetitionNode() :
|
||||||
|
Node(Node::REPEAT) {}
|
||||||
|
|
||||||
|
explicit RepetitionNode(const Node* rep, bool plus = false) :
|
||||||
|
Node(Node::REPEAT) {
|
||||||
|
mNode = rep;
|
||||||
|
mPlus = plus;
|
||||||
|
}
|
||||||
|
|
||||||
|
~RepetitionNode() override { delete mNode; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
Node* mNode = nullptr;
|
||||||
|
bool mPlus = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
class ClassNode : public Node {
|
||||||
|
public:
|
||||||
|
ClassNode() :
|
||||||
|
Node(Node::CLASS) {}
|
||||||
|
|
||||||
|
explicit ClassNode(const Buffer<Range<tAlphabetType>>& ranges, bool exclude = false) :
|
||||||
|
Node(Node::CLASS) {
|
||||||
|
mExclude = exclude;
|
||||||
|
}
|
||||||
|
|
||||||
|
~ClassNode() override { mRanges.removeAll(); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
Buffer<Range<tAlphabetType>> mRanges;
|
||||||
|
bool mExclude = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
|
RegularGrammar() = default;
|
||||||
|
|
||||||
|
~RegularGrammar() {
|
||||||
|
for (auto rule : mRules) {
|
||||||
|
delete rule->t1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void addRule(const Node* node, tTokType id) { mRules.append({ node, id }); }
|
||||||
|
|
||||||
|
public:
|
||||||
|
const Node* seq(const InitialierList<const Node*>& nodes) { return new CompoundNode(nodes); }
|
||||||
|
const Node* val(tAlphabetType in) { return new ValueNode(in); }
|
||||||
|
const Node* alt(const Node* a, const Node* b) { return new AlternationNode(a, b); }
|
||||||
|
const Node* may(const Node* a) { return new IfNode(a); }
|
||||||
|
const Node* any() { return new AnyNode(); }
|
||||||
|
const Node* rep(const Node* rep, bool plus = false) { return new RepetitionNode(rep, plus); }
|
||||||
|
const Node* ranges(const Buffer<Range<tAlphabetType>>& ranges, bool exclude = false) { return new ClassNode(ranges, exclude); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
Buffer<Pair<const Node*, tTokType>> mRules;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue