From 9e4a90f53ed4260ee8b27c326f0c7e11036e733f Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 10 Sep 2021 21:56:51 +0800 Subject: ... --- src/parse/Grammar.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'src/parse/Grammar.cpp') 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 + +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 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 -- cgit v1.2.3