From 474ed85811d93e6086823a2fd8ced2012798729a Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 27 Dec 2015 10:34:59 -0500 Subject: [PATCH] LR0: ::expandNonTerminals now writes expansion state in debug mode --- src/LR0.cpp | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/LR0.cpp b/src/LR0.cpp index 33c90117..53d8dc0c 100644 --- a/src/LR0.cpp +++ b/src/LR0.cpp @@ -51,6 +51,8 @@ void LR0::createParseTables (const Grammar& grammar) std::cout << " " << term; std::cout << "\n"; } + + std::cout << "\n"; } // Add all items from augmented grammar, in initial state: @@ -59,7 +61,7 @@ void LR0::createParseTables (const Grammar& grammar) for (unsigned int i = 0; i < augmented.size (); ++i) items.push_back (std::pair (i, 0)); - // TODO Add new states. + // Iteratively expand non-terminals until there are no more. while (expandNonTerminals (augmented, items)) ; } @@ -69,6 +71,32 @@ bool LR0::expandNonTerminals ( std::vector >& augmented, std::vector >& items) { + 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"; + } + + return false; }