LR0: ::expandNonTerminals now writes expansion state in debug mode

This commit is contained in:
Paul Beckingham 2015-12-27 10:34:59 -05:00
parent 08457be4a0
commit 474ed85811

View file

@ -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 <int, int> (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 <std::vector <std::string>>& augmented,
std::vector <std::pair <int, int>>& 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;
}