From c7aa6a3bebf36406696d96548753f4ec529bd1cf Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 3 Jan 2016 17:24:32 -0500 Subject: [PATCH] LR0: Modified ::getExpectedSymbols to use index-based looping - Index-based looping makes it easier to link the items in the states to be linked back to the augmented grammar. --- src/LR0.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/LR0.cpp b/src/LR0.cpp index 36caae6c..2c787179 100644 --- a/src/LR0.cpp +++ b/src/LR0.cpp @@ -80,7 +80,8 @@ void LR0::initialize (const Grammar& grammar) } //////////////////////////////////////////////////////////////////////////////// -// Collect a unique set of expected symbols from the closure. +// Collect a unique set of expected symbols from the closure. This is the set +// of symbols where '● X' appears in an item. std::set LR0::getExpectedSymbols (const Closure& closure) const { std::set expected; @@ -98,22 +99,25 @@ LR0::Closure LR0::getClosure (const std::string& symbol) const std::set seen; LR0::Closure result; - for (auto& rule : _augmented) + for (unsigned int r = 0; r < _augmented.size (); ++r) { - if (rule[0] == symbol) + if (_augmented[r][0] == symbol) { - Item item (rule); + Item item (_augmented[r]); + item.setGrammarRuleIndex (r); result.push_back (item); - seen.insert (rule[0]); + seen.insert (_augmented[r][0]); auto nextSymbol = item.next (); if (seen.find (nextSymbol) == seen.end ()) { - for (auto& rule : _augmented) + for (unsigned int r = 0; r < _augmented.size (); ++r) { - if (rule[0] == nextSymbol) + if (_augmented[r][0] == nextSymbol) { - result.push_back (Item (rule)); + Item item (_augmented[r]); + item.setGrammarRuleIndex (r); + result.push_back (item); seen.insert (nextSymbol); } }