diff options
| author | crupest <crupest@outlook.com> | 2021-09-10 20:56:46 +0800 |
|---|---|---|
| committer | crupest <crupest@outlook.com> | 2021-09-10 20:56:46 +0800 |
| commit | 7ba941d5a55213f763686ef367d72c90ad85ef78 (patch) | |
| tree | bf92fe1fd5374a2ef6a52dc72c843c7cd616a285 | |
| parent | 819b582849e8f5c40d1dbcac6c0cf26b265d36b0 (diff) | |
| download | cru-7ba941d5a55213f763686ef367d72c90ad85ef78.tar.gz cru-7ba941d5a55213f763686ef367d72c90ad85ef78.tar.bz2 cru-7ba941d5a55213f763686ef367d72c90ad85ef78.zip | |
...
| -rw-r--r-- | include/cru/parse/Grammar.hpp | 42 | ||||
| -rw-r--r-- | include/cru/parse/Production.hpp | 36 | ||||
| -rw-r--r-- | include/cru/parse/Symbol.hpp | 2 | ||||
| -rw-r--r-- | include/cru/parse/Terminal.hpp | 2 | ||||
| -rw-r--r-- | src/parse/Production.cpp | 12 |
5 files changed, 92 insertions, 2 deletions
diff --git a/include/cru/parse/Grammar.hpp b/include/cru/parse/Grammar.hpp index e69de29b..dd4741df 100644 --- a/include/cru/parse/Grammar.hpp +++ b/include/cru/parse/Grammar.hpp @@ -0,0 +1,42 @@ +#pragma once +#include "Production.hpp" + +#include <vector> + +namespace cru::parse { +class Grammar : public Object { + public: + Grammar(); + + CRU_DELETE_COPY(Grammar) + CRU_DELETE_MOVE(Grammar) + + ~Grammar() override; + + public: + void SetStartSymbol(Nonterminal* start_symbol); + + Terminal* CreateTerminal(String name); + Nonterminal* CreateNonterminal(String name); + Production* CreateProduction(String name, Nonterminal* left, + std::vector<Symbol*> right); + + public: // Getters + Nonterminal* GetStartSymbol() const { return start_symbol_; } + const std::vector<Terminal*>& GetTerminals() const { return terminals_; } + const std::vector<Nonterminal*>& GetNonterminals() const { + return nonterminals_; + } + const std::vector<Symbol*>& GetSymbols() const { return symbols_; } + const std::vector<Production*>& GetProductions() const { + return productions_; + } + + private: + Nonterminal* start_symbol_; + std::vector<Terminal*> terminals_; + std::vector<Nonterminal*> nonterminals_; + std::vector<Symbol*> symbols_; + std::vector<Production*> productions_; +}; +} // namespace cru::parse diff --git a/include/cru/parse/Production.hpp b/include/cru/parse/Production.hpp index e69de29b..bd05a8c4 100644 --- a/include/cru/parse/Production.hpp +++ b/include/cru/parse/Production.hpp @@ -0,0 +1,36 @@ +#pragma once +#include "cru/common/String.hpp" + +#include "Nonterminal.hpp" +#include "Terminal.hpp" + +#include <vector> + +namespace cru::parse { +class Production : public Object { + public: + Production(Grammar* grammar, String name, Nonterminal* left, + std::vector<Symbol*> right); + + CRU_DELETE_COPY(Production) + CRU_DELETE_MOVE(Production) + + ~Production() override; + + public: + Grammar* GetGrammar() const { return grammar_; } + + Nonterminal* GetLeft() const { return left_; } + void SetLeft(Nonterminal* left); + + const std::vector<Symbol*>& GetRight() const { return right_; } + void SetRight(std::vector<Symbol*> right); + + private: + Grammar* grammar_; + String name_; + + Nonterminal* left_; + std::vector<Symbol*> right_; +}; +} // namespace cru::parse diff --git a/include/cru/parse/Symbol.hpp b/include/cru/parse/Symbol.hpp index 5c862c25..e22fb9bc 100644 --- a/include/cru/parse/Symbol.hpp +++ b/include/cru/parse/Symbol.hpp @@ -7,7 +7,7 @@ class Grammar; // Base class of Terminal and Nonterminal. class Symbol : public Object { public: - Symbol(Grammar* grammar, String name); + explicit Symbol(Grammar* grammar, String name); CRU_DELETE_COPY(Symbol) CRU_DELETE_MOVE(Symbol) diff --git a/include/cru/parse/Terminal.hpp b/include/cru/parse/Terminal.hpp index b42ba1f9..8d4a31b6 100644 --- a/include/cru/parse/Terminal.hpp +++ b/include/cru/parse/Terminal.hpp @@ -4,7 +4,7 @@ namespace cru::parse { class Terminal : public Symbol { public: - explicit Terminal(Grammar* grammar, String name); + Terminal(Grammar* grammar, String name); CRU_DELETE_COPY(Terminal) CRU_DELETE_MOVE(Terminal) diff --git a/src/parse/Production.cpp b/src/parse/Production.cpp index e69de29b..52ec46ab 100644 --- a/src/parse/Production.cpp +++ b/src/parse/Production.cpp @@ -0,0 +1,12 @@ +#include "cru/parse/Production.hpp" + +namespace cru::parse { +Production::Production(Grammar* grammar, String name, Nonterminal* left, + std::vector<Symbol*> right) + : grammar_(grammar), + name_(std::move(name)), + left_(left), + right_(std::move(right)) {} + +Production::~Production() {} +} // namespace cru::parse |
