LR0: Implemented the LR0::Item object

This commit is contained in:
Paul Beckingham 2015-12-27 11:57:19 -05:00
parent 44ddd462ac
commit 6b4448ec8a
2 changed files with 32 additions and 6 deletions

View file

@ -57,19 +57,23 @@ void LR0::createParseTables (const Grammar& grammar)
// Add all items from augmented grammar, in initial state:
// A --> . B ....
/*
LR0::Closure closure;
for (unsigned int i = 0; i < augmented.size (); ++i)
closure.push_back (LR0::Item (i, 0));
LR0::States states;
states.push_back (closure);
*/
// Iteratively expand non-terminals until there are no more.
/*
int state = 0;
while (expandNonTerminals (augmented, states, state))
;
*/
}
////////////////////////////////////////////////////////////////////////////////
/*
bool LR0::expandNonTerminals (
std::vector <std::vector <std::string>>& augmented,
LR0::States& states,
@ -83,6 +87,7 @@ bool LR0::expandNonTerminals (
return false;
}
*/
////////////////////////////////////////////////////////////////////////////////
void LR0::parse (const std::string& input)
@ -115,6 +120,7 @@ std::string LR0::dump () const
}
////////////////////////////////////////////////////////////////////////////////
/*
std::string LR0::dump (std::vector <std::vector <std::string>>& augmented, States& states) const
{
std::stringstream out;
@ -149,5 +155,23 @@ std::string LR0::dump (std::vector <std::vector <std::string>>& augmented, State
out << "\n";
return out.str ();
}
*/
////////////////////////////////////////////////////////////////////////////////
LR0::Item::Item (std::vector <std::string>& rule)
: _rule (rule)
, _cursor (2)
{
}
////////////////////////////////////////////////////////////////////////////////
bool LR0::Item::advance ()
{
if (_cursor >= _rule.size ())
return false;
++_cursor;
return true;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -44,10 +44,12 @@ protected:
class Item
{
public:
Item (int rule, int cursor) : _rule (rule), _cursor (cursor) {}
Item (std::vector <std::string>&);
bool advance ();
int _rule;
int _cursor;
private:
std::vector <std::string> _rule;
unsigned int _cursor;
};
class Closure : public std::vector <Item>
@ -59,8 +61,8 @@ protected:
};
private:
bool expandNonTerminals (std::vector <std::vector <std::string>>&, States&, int);
std::string dump (std::vector <std::vector <std::string>>&, States&) const;
// bool expandNonTerminals (std::vector <std::vector <std::string>>&, States&, int);
// std::string dump (std::vector <std::vector <std::string>>&, States&) const;
private:
// state column result