From 05063904a6f6342c22155c1472b408eb3076fc52 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 27 Dec 2015 11:19:36 -0500 Subject: [PATCH] LR0: Converted ::expandNonTerminals to use new data structure --- src/LR0.cpp | 35 ++++++++--------------------------- src/LR0.h | 2 +- 2 files changed, 9 insertions(+), 28 deletions(-) diff --git a/src/LR0.cpp b/src/LR0.cpp index 2b4e25bd..4a9fb845 100644 --- a/src/LR0.cpp +++ b/src/LR0.cpp @@ -57,44 +57,25 @@ void LR0::createParseTables (const Grammar& grammar) // Add all items from augmented grammar, in initial state: // A --> . B .... - std::vector > items; + LR0::Closure closure; for (unsigned int i = 0; i < augmented.size (); ++i) - items.push_back (std::pair (i, 0)); + closure.push_back (LR0::Item (i, 0)); + LR0::States states; + states.push_back (closure); // Iteratively expand non-terminals until there are no more. - while (expandNonTerminals (augmented, items)) + while (expandNonTerminals (augmented, states)) ; } //////////////////////////////////////////////////////////////////////////////// bool LR0::expandNonTerminals ( std::vector >& augmented, - std::vector >& items) + LR0::States& states) { if (_debug) - { - std::cout << "Expand:\n"; - int count = 0; - for (auto& item : items) - { - std::cout << " [" << count++ << "] " - << augmented[item.first][0] - << " " - << augmented[item.first][1]; - - for (int i = 2; i < (int)augmented[item.first].size (); ++i) - { - if (i - 2 == item.second) - std::cout << " ."; - - std::cout << " " << augmented[item.first][i]; - } - - std::cout << "\n"; - } - - std::cout << "\n"; - } + std::cout << "Expand:\n" + << dump (augmented, states); diff --git a/src/LR0.h b/src/LR0.h index 1b11840e..838c8fd7 100644 --- a/src/LR0.h +++ b/src/LR0.h @@ -59,7 +59,7 @@ protected: }; private: - bool expandNonTerminals (std::vector >&, std::vector >&); + bool expandNonTerminals (std::vector >&, States&); std::string dump (std::vector >&, States&) const; private: