aboutsummaryrefslogtreecommitdiff
path: root/src/parse/Grammar.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-09-10 21:56:51 +0800
committercrupest <crupest@outlook.com>2021-09-10 21:56:51 +0800
commit9e4a90f53ed4260ee8b27c326f0c7e11036e733f (patch)
treed15684101d6b9b6f34d4e3f8dc9978deac040715 /src/parse/Grammar.cpp
parent7ba941d5a55213f763686ef367d72c90ad85ef78 (diff)
downloadcru-9e4a90f53ed4260ee8b27c326f0c7e11036e733f.tar.gz
cru-9e4a90f53ed4260ee8b27c326f0c7e11036e733f.tar.bz2
cru-9e4a90f53ed4260ee8b27c326f0c7e11036e733f.zip
...
Diffstat (limited to 'src/parse/Grammar.cpp')
-rw-r--r--src/parse/Grammar.cpp38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/parse/Grammar.cpp b/src/parse/Grammar.cpp
index e69de29b..8d929212 100644
--- a/src/parse/Grammar.cpp
+++ b/src/parse/Grammar.cpp
@@ -0,0 +1,38 @@
+#include "cru/parse/Grammar.hpp"
+#include "cru/parse/Symbol.hpp"
+
+#include <algorithm>
+
+namespace cru::parse {
+Grammar::Grammar() {}
+
+Grammar::~Grammar() {}
+
+Terminal* Grammar::CreateTerminal(String name) {
+ auto terminal = new Terminal(this, std::move(name));
+ terminals_.push_back(terminal);
+ symbols_.push_back(terminal);
+ return terminal;
+}
+
+Nonterminal* Grammar::CreateNonterminal(String name) {
+ auto nonterminal = new Nonterminal(this, std::move(name));
+ nonterminals_.push_back(nonterminal);
+ symbols_.push_back(nonterminal);
+ return nonterminal;
+}
+
+Production* Grammar::CreateProduction(String name, Nonterminal* left,
+ std::vector<Symbol*> right) {
+ Expects(left->GetGrammar() == this);
+ Expects(std::all_of(right.cbegin(), right.cend(), [this](Symbol* symbol) {
+ return symbol->GetGrammar() == this;
+ }));
+
+ auto production =
+ new Production(this, std::move(name), left, std::move(right));
+ productions_.push_back(production);
+ return production;
+}
+
+} // namespace cru::parse