From 6b4448ec8a8fdc059c09df9da4bddfb83be4ac57 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 27 Dec 2015 11:57:19 -0500 Subject: [PATCH] LR0: Implemented the LR0::Item object --- src/LR0.cpp | 26 +++++++++++++++++++++++++- src/LR0.h | 12 +++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/LR0.cpp b/src/LR0.cpp index d0f5317b..db1a73d9 100644 --- a/src/LR0.cpp +++ b/src/LR0.cpp @@ -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 >& 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 >& augmented, States& states) const { std::stringstream out; @@ -149,5 +155,23 @@ std::string LR0::dump (std::vector >& augmented, State out << "\n"; return out.str (); } +*/ + +//////////////////////////////////////////////////////////////////////////////// +LR0::Item::Item (std::vector & rule) +: _rule (rule) +, _cursor (2) +{ +} + +//////////////////////////////////////////////////////////////////////////////// +bool LR0::Item::advance () +{ + if (_cursor >= _rule.size ()) + return false; + + ++_cursor; + return true; +} //////////////////////////////////////////////////////////////////////////////// diff --git a/src/LR0.h b/src/LR0.h index 95d7aac1..46224bbd 100644 --- a/src/LR0.h +++ b/src/LR0.h @@ -44,10 +44,12 @@ protected: class Item { public: - Item (int rule, int cursor) : _rule (rule), _cursor (cursor) {} + Item (std::vector &); + bool advance (); - int _rule; - int _cursor; + private: + std::vector _rule; + unsigned int _cursor; }; class Closure : public std::vector @@ -59,8 +61,8 @@ protected: }; private: - bool expandNonTerminals (std::vector >&, States&, int); - std::string dump (std::vector >&, States&) const; +// bool expandNonTerminals (std::vector >&, States&, int); +// std::string dump (std::vector >&, States&) const; private: // state column result