From 7ed6b1f33cee13a84f6cb77f7840bae48337adb5 Mon Sep 17 00:00:00 2001 From: IlyaShurupov Date: Tue, 31 Oct 2023 22:43:37 +0300 Subject: [PATCH] tmp --- Language/private/SimpleParser.cpp | 11 +- Language/public/Grammar.hpp | 162 +++++++++++++++++++++++++++++- 2 files changed, 167 insertions(+), 6 deletions(-) diff --git a/Language/private/SimpleParser.cpp b/Language/private/SimpleParser.cpp index 213b789..1c8b16f 100644 --- a/Language/private/SimpleParser.cpp +++ b/Language/private/SimpleParser.cpp @@ -1,4 +1,6 @@ +#include "NewPlacement.hpp" + #include "SimpleParser.hpp" using namespace tp; @@ -22,14 +24,13 @@ SimpleParser::SimpleParser() { } // Define Regular grammar - RegularGrammar regularGrammar; + RegularGrammar rg; { // this is basically ast from existing tokenizer - regularGrammar.addRule(regularGrammar.alternate(regularGrammar.repeat(regularGrammar.symbols("asd")), regularGrammar.symbol("a"))); - regularGrammar.addRule(regularGrammar.alternate(regularGrammar.repeat(regularGrammar.symbols("asd")), regularGrammar.symbol("a"))); + rg.addRule(rg.seq({ rg.val('a'), rg.val('b') }), 0); } - mUnifiedGrammarParser.compileTables(contextFreeGrammar, regularGrammar); + mUnifiedGrammarParser.compileTables(contextFreeGrammar, rg); } 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 ContextFreeGrammar userContextFreeGrammar; - RegularGrammar userRegularGrammar; + RegularGrammar userRegularGrammar; // ... // split ast into RE and CF part diff --git a/Language/public/Grammar.hpp b/Language/public/Grammar.hpp index e8bd32f..5f7984f 100644 --- a/Language/public/Grammar.hpp +++ b/Language/public/Grammar.hpp @@ -1,9 +1,169 @@ #pragma once +#include "Buffer.hpp" #include "LanguageCommon.hpp" +#include "Utils.hpp" namespace tp { + class ContextFreeGrammar {}; - class RegularGrammar {}; + + template + 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& nodes) : + Node(Node::COMPOUND) { + mSequence = nodes; + } + + ~CompoundNode() override { + for (auto iter : mSequence) { + delete iter.data(); + } + mSequence.clear(); + } + + private: + Buffer 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>& ranges, bool exclude = false) : + Node(Node::CLASS) { + mExclude = exclude; + } + + ~ClassNode() override { mRanges.removeAll(); } + + private: + Buffer> 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& 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>& ranges, bool exclude = false) { return new ClassNode(ranges, exclude); } + + private: + Buffer> mRules; + }; }