mirror of
https://github.com/GothenburgBitFactory/timewarrior.git
synced 2025-07-07 20:06:39 +02:00
LR0: Converted ::expandNonTerminals to use new data structure
This commit is contained in:
parent
ad5f1e4fd7
commit
05063904a6
2 changed files with 9 additions and 28 deletions
35
src/LR0.cpp
35
src/LR0.cpp
|
@ -57,44 +57,25 @@ void LR0::createParseTables (const Grammar& grammar)
|
||||||
|
|
||||||
// Add all items from augmented grammar, in initial state:
|
// Add all items from augmented grammar, in initial state:
|
||||||
// A --> . B ....
|
// A --> . B ....
|
||||||
std::vector <std::pair <int, int>> items;
|
LR0::Closure closure;
|
||||||
for (unsigned int i = 0; i < augmented.size (); ++i)
|
for (unsigned int i = 0; i < augmented.size (); ++i)
|
||||||
items.push_back (std::pair <int, int> (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.
|
// Iteratively expand non-terminals until there are no more.
|
||||||
while (expandNonTerminals (augmented, items))
|
while (expandNonTerminals (augmented, states))
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
bool LR0::expandNonTerminals (
|
bool LR0::expandNonTerminals (
|
||||||
std::vector <std::vector <std::string>>& augmented,
|
std::vector <std::vector <std::string>>& augmented,
|
||||||
std::vector <std::pair <int, int>>& items)
|
LR0::States& states)
|
||||||
{
|
{
|
||||||
if (_debug)
|
if (_debug)
|
||||||
{
|
std::cout << "Expand:\n"
|
||||||
std::cout << "Expand:\n";
|
<< dump (augmented, states);
|
||||||
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";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -59,7 +59,7 @@ protected:
|
||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool expandNonTerminals (std::vector <std::vector <std::string>>&, std::vector <std::pair <int, int>>&);
|
bool expandNonTerminals (std::vector <std::vector <std::string>>&, States&);
|
||||||
std::string dump (std::vector <std::vector <std::string>>&, States&) const;
|
std::string dump (std::vector <std::vector <std::string>>&, States&) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue