LR0: Implemnted ::closeState

This commit is contained in:
Paul Beckingham 2016-01-05 01:02:14 -05:00
parent 79f5e03a26
commit 1491df085d

View file

@ -131,6 +131,36 @@ LR0::Closure LR0::getClosure (const std::string& symbol) const
////////////////////////////////////////////////////////////////////////////////
bool LR0::closeState (States& states, const int state) const
{
std::cout << "# LR0::closeState " << state << "\n";
// Obtain all the expected symbols for this state.
auto expectedSymbols = getExpectedSymbols (states[state]);
for (auto& expected : expectedSymbols)
{
std::cout << "# expecting " << expected << "\n";
// This will be the new state.
Closure closure;
// Find all the rules in this state that are expecting 'expected'.
for (auto& item : states[state])
{
if (! item.done () &&
item.next () == expected)
{
std::cout << "# matching " << item.dump () << "\n";
Item advanced (item);
advanced.advance ();
std::cout << "# advanced " << advanced.dump () << "\n";
closure.push_back (advanced);
}
}
// Create the new state, and recurse to close it.
states.push_back (closure);
closeState (states, states.size () - 1);
}
return false;
}