From fa3794469430b4ddfe0b0ca62850e9911c99b558 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 14 Dec 2021 21:34:59 +0800 Subject: ... --- src/parse/Grammar.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'src/parse/Grammar.cpp') diff --git a/src/parse/Grammar.cpp b/src/parse/Grammar.cpp index 9d486c38..19095150 100644 --- a/src/parse/Grammar.cpp +++ b/src/parse/Grammar.cpp @@ -1,4 +1,5 @@ #include "cru/parse/Grammar.hpp" +#include "cru/common/String.hpp" #include "cru/parse/Symbol.hpp" #include @@ -19,6 +20,10 @@ Grammar::~Grammar() { } } +void Grammar::SetStartSymbol(Nonterminal* start_symbol) { + start_symbol_ = start_symbol; +} + Terminal* Grammar::CreateTerminal(String name) { auto terminal = new Terminal(this, std::move(name)); terminals_.push_back(terminal); @@ -147,8 +152,10 @@ void Grammar::EliminateLeftRecursions() { auto new_right = right; new_right.insert(new_right.cbegin(), jp->GetRight().cbegin(), jp->GetRight().cend()); - // TODO: What should this name be. - CreateProduction(u"", ni, std::move(new_right)); + CreateProduction( + Format(u"Merge of {} and {} (Eliminate Left Recursion)", + production->GetName(), jp->GetName()), + ni, std::move(new_right)); } } } @@ -278,4 +285,19 @@ void Grammar::LeftFactor() { } } } + +String Grammar::ProductionsToString() const { + String result; + + for (const auto& production : productions_) { + result += production->GetName() + u" : "; + result += production->GetLeft()->GetName() + u" := "; + for (auto s : production->GetRight()) { + result += s->GetName() + u" "; + } + result += u"\n"; + } + + return result; +} } // namespace cru::parse -- cgit v1.2.3