Commit e4a11171 authored by Florent Jacquemard's avatar Florent Jacquemard
Browse files

bug RunFactory in creation bar transitions (2 transitions nullify and binary...

bug RunFactory in creation bar transitions (2 transitions nullify and binary must be created at once for each state < 0)
parent febbf71c
......@@ -368,7 +368,6 @@ void KeySI::RunsInner(const Transition& t,
std::stack<RunInner<KeySI>*>& si,
const ParsingEnv& env) const
{
assert(State::isWTA(_state));
const label_t& a = t.label();
assert(SymbLabel::inner(a));
......
......@@ -115,13 +115,25 @@ public:
static void innerRun(const K& k,
const Transition& t,
std::stack<RunInner<K>*>& si);
/// unique outside-the-bar base transition or given arity
/// targeting the embedded state.
/// @param state a SWTA state, base state of a target key.
/// @param arity arity of the transition. must be 0 or 2.
/// @param env parsing environment. may be modified.
static const Transition& barTransition(state_t state,
size_t arity,
ParsingEnv& env);
/// unique nullary outside-the-bar base transition targeting the embedded state.
/// @param env parsing environment. may be modified.
/// @todo delete, replaced by barTransition
static const Transition& barTransition0(state_t state, ParsingEnv& env);
/// unique binary outside-the-bar base transition targeting the embedded state.
/// @param env parsing environment. may be modified.
/// @todo delete, replaced by barTransition
static const Transition& barTransition2(state_t state, ParsingEnv& env);
/// allocate and return a special Run representing a parse failure for this key.
......
......@@ -79,14 +79,14 @@ void RunFactory<K>::runsBar(const K& k,
assert(k.complete());
assert(State::isMeta(k.state()));
// create final bar nullary transition. it is added to the env pool.
const Transition& t0 = barTransition0(k.state(), env);
const Transition& t0 = barTransition(k.state(), 0, env);
// let the key class extend this transition and add extension to stack
k.RunsTerminal(t0, st, env);
// RunTerm* r0 = new RunTerm(t0, 0); // empty interval
// st.push(std::make_pair((K*) NULL, r0)); // only for complete keys
// create bar binary transition. it is added to the env pool.
const Transition& t2 = barTransition2(k.state(), env);
const Transition& t2 = barTransition(k.state(), 2, env);
// let the key class extend this transition and add extension to stack
k.RunsInner(t2, si, env);
// innerRuns(k, t2, si);
......@@ -132,6 +132,65 @@ void RunFactory<K>::innerRun(const K& k,
}
template<class K>
const Transition& RunFactory<K>::barTransition(state_t state,
size_t arity,
ParsingEnv& env)
{
assert(State::isMeta(state));
assert((arity == 0) || (arity == 2));
assert(env.pool);
if (env.pool->isRegistered(state))
{
// we assume that there are exactly 1 nullary and 1 binary
// transitions targeting _state in env. pool.
for (Tlist<Transition>::const_iterator
i = env.pool->cbegin(state);
i != env.pool->cend(state); i++)
{
if (i->arity() == arity)
return *i;
}
ERROR("KeyFactory:barTransition0: transition not found in pool, state {}",
state);
throw std::runtime_error("barTransition0 not found\n");
}
else
{
// we construct and add both bar transition of arity 0 and 2 at once
const Weight tone = env.pool->weight_one();
Tlist<Transition>& tl = env.pool->add(state, (state == -1));
// construct and add transition of arity 0
tl.emplace(SymbLabel::make_bar(0), tone);
assert(! tl.empty());
Transition& t0 = tl.last();
TRACE("KeyFactory: add BAR0 transition to pool: {} -> {}", state, t0);
// construct and add transition of arity 2
tl.emplace(SymbLabel::make_bar(2), tone);
assert(! tl.empty());
Transition& t2 = tl.last();
assert(env.wta);
t2.push(env.wta->initial());
t2.push(state - 1); /// @todo macro next Bar state
TRACE("KeyFactory: add BAR2 transition to pool: {} -> {}", state, t2);
if (arity == 0)
{
return t0;
}
else
{
assert(arity == 2);
return t2;
}
}
}
/// @todo delete, replaced by barTransition
template<class K>
const Transition& RunFactory<K>::barTransition0(state_t state, ParsingEnv& env)
{
......@@ -166,6 +225,7 @@ const Transition& RunFactory<K>::barTransition0(state_t state, ParsingEnv& env)
}
/// @todo delete, replaced by barTransition
template<class K>
const Transition& RunFactory<K>::barTransition2(state_t state, ParsingEnv& env)
{
......
......@@ -72,16 +72,16 @@ public:
void clear();
/// constant iterator pointing to the first transition in this transition list.
Tlist<T>::const_iterator cbegin() const { return _table.cbegin(); }
Tlist<T>::const_iterator cbegin() const { return _tlist.cbegin(); }
/// constant iterator pointing to the end of this transition list.
Tlist<T>::const_iterator cend() const { return _table.cend(); }
Tlist<T>::const_iterator cend() const { return _tlist.cend(); }
/// iterator pointing to the first transition in this transition list.
Tlist<T>::iterator begin() { return _table.begin(); }
Tlist<T>::iterator begin() { return _tlist.begin(); }
/// iterator pointing to the end of this transition list.
Tlist<T>::iterator end() { return _table.end(); }
Tlist<T>::iterator end() { return _tlist.end(); }
private:
......@@ -89,7 +89,7 @@ private:
size_t _cpt_size;
/// transition list.
std::list<T> _table;
std::list<T> _tlist;
/// set of all states occuring in transitions of list (in head or body).
std::set<state_t> allStates() const;
......
......@@ -19,20 +19,21 @@ Tlist<T>::~Tlist()
template<class T>
bool Tlist<T>::empty() const
{
return (_table.empty());
return (_tlist.empty());
}
template<class T>
size_t Tlist<T>::size() const
{
return _table.size();
return _tlist.size();
}
template<class T>
T& Tlist<T>::last()
{
return _table.back();
return _tlist.back();
}
......@@ -40,7 +41,7 @@ template<class T>
void Tlist<T>::add(const T& t)
{
assert(t.inner() || t.terminal());
_table.push_back(t);
_tlist.push_back(t);
_cpt_size += t.arity() + 1;
// assert (_parent);
......@@ -48,13 +49,14 @@ void Tlist<T>::add(const T& t)
// _parent->_cpt_size += (t.arity() + 1);
}
template <class T>
template <class... Args>
void Tlist<T>::emplace(Args&&... args)
{
_table.emplace_back(std::forward<Args>(args)...);
assert(! _table.empty());
const T& t = _table.back();
_tlist.emplace_back(std::forward<Args>(args)...);
assert(! _tlist.empty());
const T& t = _tlist.back();
_cpt_size += t.arity() + 1;
}
......@@ -62,10 +64,10 @@ void Tlist<T>::emplace(Args&&... args)
template<class T>
void Tlist<T>::clear()
{
//for(vector<Production*>::iterator i = _table.begin(); i != _table.end(); ++i)
//for(vector<Production*>::iterator i = _tlist.begin(); i != _tlist.end(); ++i)
// delete *i;
//_table.clear();
//_tlist.clear();
}
......@@ -74,15 +76,15 @@ typename Tlist<T>::iterator
Tlist<T>::remove(Tlist<T>::iterator i)
{
_cpt_size -= ((*i).size() + 1);
return _table.erase(i);
return _tlist.erase(i);
}
template<class T>
void Tlist<T>::remove(state_t s)
{
Tlist<T>::iterator i = _table.begin();
while(i != _table.end())
Tlist<T>::iterator i = _tlist.begin();
while(i != _tlist.end())
{
T& t = *i;
// we do not remove transition to terminal symbols
......@@ -91,7 +93,7 @@ void Tlist<T>::remove(state_t s)
_cpt_size -= (t.size() + 1);
TRACE("rm state {}: rm transition {}", s, t);
// remove transition from vector (destructor called)
i = _table.erase(i); // C++11
i = _tlist.erase(i); // C++11
}
else
{
......@@ -99,4 +101,3 @@ void Tlist<T>::remove(state_t s)
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment