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

non-NULL fail Run

- generated by KeySV
- returned by best() of Records in case of failure (instead of NULL)
- and also by best of parse table
parent dd9af65b
......@@ -3202,6 +3202,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
43CEBDF325DAA5DF005FA9AE /* equiv2.cpp in Sources */,
43CEBDEE25DAA5DF005FA9AE /* config.cpp in Sources */,
43CEBDEF25DAA5DF005FA9AE /* trace.cpp in Sources */,
43CEBDF025DAA5DF005FA9AE /* hash.cpp in Sources */,
......@@ -3333,7 +3334,6 @@
43CEBF5C25DABA3E005FA9AE /* Reduce.cpp in Sources */,
43CEBE0C25DAA666005FA9AE /* RT.cpp in Sources */,
43CEBE0D25DAA666005FA9AE /* LRT.cpp in Sources */,
43CEBDF325DAA5DF005FA9AE /* equiv2.cpp in Sources */,
43CEA93A25EFD1E5007C355B /* ValueFile.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
......
......@@ -280,6 +280,7 @@ protected:
/// @brief number of points to read in the input segment
/// in order to make a transition to this Key state.
/// @todo RM : replaced by size()
virtual inline size_t points() const { return _interval->lsize(); }
// @brief number of input point to read for transition from the given
......
......@@ -188,6 +188,7 @@ public:
inline bool post_laston() const { return _post_laston; }
/// @brief number of input to read for this KeySIO state
/// @todo RM : replaced by size()
virtual inline size_t points() const { return _pre + _interval->lsize(); }
// @brief number of input points to read for making a transition
......
......@@ -187,6 +187,7 @@ protected:
/// @brief number of points to read in the input segment
/// in order to make a transition to this Key state.
/// @todo RM : replaced by size()
virtual inline size_t points() const { return _pre + _interval->lsize(); }
// @brief number of input point to read for transition from the given
......
......@@ -176,7 +176,7 @@ public:
inline pre_t post_off() const { return _post.second; }
/// @brief number of input to read for this KeySIR state
/// @todo OBSOLETE replaced by size()
/// @todo RM : replaced by size()
virtual inline size_t points() const { return _pre.first + _interval->lsize(); }
// @brief number of input point to read for transition from the given
......
......@@ -42,8 +42,8 @@ _fs(k._fs)
// first child
KeySV::KeySV(const KeySV& parent,
const Transition& tr,
const ParsingEnv& env):
const Transition& tr,
const ParsingEnv& env):
KeyS(parent, tr, env),
_list(), // will be replaced
_start(MTU::ZERO),
......@@ -74,10 +74,10 @@ _fs(true)
// next sibling
KeySV::KeySV(const KeySV& previous,
size_t i,
const KeySV& parent,
const Transition& tr,
const ParsingEnv& env):
size_t i,
const KeySV& parent,
const Transition& tr,
const ParsingEnv& env):
KeyS(previous, i, parent, tr, env),
_list(), // will be replaced
_start(previous._start + previous._list.length()),
......@@ -122,9 +122,9 @@ _fs(false)
// up: instanciate = copy
KeySV::KeySV(const KeySV& head,
label_t a,
const KeySV& lastchild,
const ParsingEnv& env):
label_t a,
const KeySV& lastchild,
const ParsingEnv& env):
KeyS(head, a, lastchild, env),
_list(head._list),
_start(head._start),
......@@ -147,6 +147,30 @@ KeySV::~KeySV()
{ }
// std::shared_ptr<Run<KeySV>> KeySV::failRun_zero(const ParsingEnv& env) const
Run<KeySV>* KeySV::failRun_zero(const ParsingEnv& env) const
{
assert(env.wta);
const Transition& t0 = env.dummyTransition_zero();
assert(t0.weight().zero());
label_t a = SymbLabel::make_fail(this->size());
return new Run<KeySV>(t0, a, t0.weight());
// return std::shared_ptr<Run<KeySV>>(new Run<KeySV>(t0, a, t0.weight()));
}
// std::shared_ptr<Run<KeySV>> KeySV::failRun_one(const ParsingEnv& env) const
Run<KeySV>* KeySV::failRun_one(const ParsingEnv& env) const
{
assert(env.wta);
const Transition& t1 = env.dummyTransition_one();
assert(t1.weight().one());
label_t a = SymbLabel::make_fail(this->size());
return new Run<KeySV>(t1, a, t1.weight());
// return std::shared_ptr<Run<KeySV>>(new Run<KeySV>(t1, a, t1.weight()));
}
KeySV& KeySV::operator= (const KeySV& k)
{
if(this != &k)
......@@ -186,7 +210,7 @@ size_t KeySV::size() const
// number of leaf instances (0, 1 or 2)
size_t KeySV::leaf(const Transition& tr,
const ParsingEnv& env) const
const ParsingEnv& env) const
{
if (filterTerminalTransition(tr, env))
return 1;
......@@ -198,15 +222,15 @@ size_t KeySV::leaf(const Transition& tr,
// add leaf instances in q
// should not be called
void KeySV::leaves(const Transition& tr,
const ParsingEnv& env,
std::queue<KeySV>& q) const
const ParsingEnv& env,
std::queue<KeySV>& q) const
{
assert(this->partial()); // will fail
}
label_t KeySV::label(const Transition& tr,
const ParsingEnv& env) const
const ParsingEnv& env) const
{
return tr.label();
/// @todo if label has undefined info,
......@@ -228,8 +252,8 @@ bool KeySV::complete() const
Weight KeySV::weight(label_t a,
const Transition& tr,
const ParsingEnv& env) const
const Transition& tr,
const ParsingEnv& env) const
{
if (tr.inner())
return innerWeight(a, tr, env);
......@@ -240,8 +264,8 @@ Weight KeySV::weight(label_t a,
// weight for the case of a and tr inner.
Weight KeySV::innerWeight(label_t a,
const Transition& tr,
const ParsingEnv& env) const
const Transition& tr,
const ParsingEnv& env) const
{
assert(tr.inner());
assert(SymbLabel::inner(a));
......@@ -298,8 +322,8 @@ bool KeySV::compatible(label_t a) const
// weight for the case of a and tr terminal.
Weight KeySV::terminalWeight(label_t a,
const Transition& tr,
const ParsingEnv& env) const
const Transition& tr,
const ParsingEnv& env) const
{
assert(tr.terminal());
assert(SymbLabel::terminal(a));
......@@ -341,7 +365,7 @@ Weight KeySV::terminalWeight(label_t a,
// compatibility of this key with the given label symbol.
bool KeySV::filterInnerLabel(const label_t& a,
const ParsingEnv& env) const
const ParsingEnv& env) const
{
assert(SymbLabel::inner(a));
......@@ -379,7 +403,7 @@ bool KeySV::filterInnerLabel(const label_t& a,
// compatibility of this key with the given transition.
bool KeySV::filterInnerTransition(const Transition& tr,
const ParsingEnv& env) const
const ParsingEnv& env) const
{
assert(tr.inner());
// TRACE("KeySV.filterTransition {}", tr);
......@@ -389,7 +413,7 @@ bool KeySV::filterInnerTransition(const Transition& tr,
// compatibility of this key with the given label symbol.
bool KeySV::filterTerminalLabel(const label_t& a,
const ParsingEnv& env) const
const ParsingEnv& env) const
{
assert(SymbLabel::terminal(a));
......@@ -430,7 +454,7 @@ bool KeySV::filterTerminalLabel(const label_t& a,
// compatibility of this key with the given transition.
bool KeySV::filterTerminalTransition(const Transition& tr,
const ParsingEnv& env) const
const ParsingEnv& env) const
{
assert(tr.terminal());
// TRACE("KeySV.filterTransition {}", tr);
......@@ -440,7 +464,7 @@ bool KeySV::filterTerminalTransition(const Transition& tr,
// add base wta transition targting this key in s
void KeySV::transitions(std::stack<const Transition*>& st,
ParsingEnv& env) const
ParsingEnv& env) const
{
if (State::isWTA(_state))
transitionsWTA(st, env);
......@@ -451,7 +475,7 @@ void KeySV::transitions(std::stack<const Transition*>& st,
void KeySV::transitionsWTA(std::stack<const Transition*>& st,
ParsingEnv& env) const
ParsingEnv& env) const
{
assert(State::isWTA(_state));
assert(env.wta);
......@@ -470,12 +494,13 @@ void KeySV::transitionsWTA(std::stack<const Transition*>& st,
void KeySV::transitionsBar(std::stack<const Transition*>& st,
ParsingEnv& env) const
ParsingEnv& env) const
{
assert(State::isMeta(_state));
assert(env.wta);
assert(env.pool);
const Transition* t[2] = { NULL, NULL};
const Transition* t[2] = { NULL, NULL}; // 1 NT and 1 Term transition
if (env.pool->isRegistered(_state))
{
// we assume that there are exactly 1 nullary and 1 binary
......@@ -532,7 +557,7 @@ void KeySV::transitionsBar(std::stack<const Transition*>& st,
Transition& KeySV::addNullaryBarTransition(state_t s,
ParsingEnv& env) const
ParsingEnv& env) const
{
assert(State::isMeta(s));
assert(env.pool);
......@@ -567,6 +592,7 @@ Transition& KeySV::addBinaryBarTransition(state_t s,
// number of points to read in the input segment
/// @todo RM : replaced by size()
size_t KeySV::points() const
{
return _list.size();
......
......@@ -15,6 +15,7 @@
#include <stdio.h>
#include <assert.h>
#include <utility> // std::pair, std::make_pair
#include <memory>
#include "util.hpp"
#include "hash.hpp"
......@@ -22,10 +23,13 @@
#include "ValueList.hpp"
#include "KeyS.hpp"
//#include "Distance.hpp"
//#include "Runey.hpp"
namespace Parsing{
//template<class K> class Run;
/// Key (augmented SWTA state) made of
/// - one state of a base WTA
/// - one quantified interval = sequence of rationals (inherited attribute)
......@@ -109,9 +113,31 @@ public:
/// destructor
virtual ~KeySV();
/// allocate and return a special Run representing a parse failure for this key.
/// @param env parsing environment.
/// @return a pointer to a newly allocated fail run, with
/// - transition with dummy symbol, empty body, weight one in the
/// domain of the base wta in the passed environment.
/// - fail symbol
/// - weight zero (in the domain of the base wta).
/// This returned run will answer succesfully to Run.fail().
Run<KeySV>* failRun_zero(const ParsingEnv& env) const;
// std::shared_ptr<Run<KeySV>> failRun_zero(const ParsingEnv& env) const;
/// allocate and return a special Run representing a parse failure for this key.
/// @param env parsing environment.
/// @return a pointer to a newly allocated fail run, with
/// - transition with dummy symbol, empty body, weight one in the
/// domain of the base wta in the passed environment.
/// - fail symbol
/// - weight one (in the domain of the base wta).
/// This returned run will answer succesfully to Run.fail().
Run<KeySV>* failRun_one(const ParsingEnv& env) const;
// std::shared_ptr<Run<KeySV>> failRun_one(const ParsingEnv& env) const;
/// @brief accessor
virtual size_t size() const;
virtual size_t size() const;
/// @brief number of leaf instances that can be obtained
/// from this key and the given args.
......@@ -273,6 +299,7 @@ protected:
/// @brief number of points to read in the input segment
/// in order to make a transition to this Key state.
/// @todo RM : replaced by size()
virtual size_t points() const;
// @brief number of input point to read for transition from the given
......
......@@ -14,20 +14,15 @@ namespace Parsing{
ParsingEnv::ParsingEnv(SWTA* a, InputSegment* s):
InputEnv(s),
wta(a),
pool((a == NULL)?NULL:(new SWTA(wta->weight_one()))) // empty
pool((a == NULL)?NULL:(new SWTA(wta->weight_one()))), // empty
_dt0((a == NULL)?Transition():Transition(SymbLabel::make_dummy(), a->weight_zero())),
_dt1((a == NULL)?Transition():Transition(SymbLabel::make_dummy(), a->weight_one()))
{
TRACE("Creating new Parsing Env");
//init_pool();
}
ParsingEnv::ParsingEnv(const ParsingEnv& env):
InputEnv(env),
wta(env.wta),
pool(env.pool)
{ }
ParsingEnv::ParsingEnv(const InputEnv& env, SWTA* a):
InputEnv(env),
wta(a),
......@@ -38,6 +33,15 @@ pool((a == NULL)?NULL:(new SWTA(wta->weight_one())))
}
ParsingEnv::ParsingEnv(const ParsingEnv& env):
InputEnv(env),
wta(env.wta),
pool(env.pool),
_dt0(env._dt0),
_dt1(env._dt1)
{ }
ParsingEnv::~ParsingEnv()
{
if (pool)
......@@ -49,6 +53,40 @@ ParsingEnv::~ParsingEnv()
}
inline Weight ParsingEnv::weight_zero() const
{
assert(wta);
return wta->weight_zero();
}
inline Weight ParsingEnv::weight_one() const
{
assert(wta);
return wta->weight_one();
}
const Transition& ParsingEnv::dummyTransition_zero() const
{
assert(wta);
assert(! _dt0.weight().unknown());
assert(_dt0.weight().zero());
return _dt0;
}
const Transition& ParsingEnv::dummyTransition_one() const
{
assert(wta);
assert(! _dt1.weight().unknown());
assert(_dt1.weight().one());
return _dt1;
}
//void ParsingEnv::init_pool()
//{
// if (wta != NULL)
......
......@@ -16,6 +16,7 @@
#include "trace.hpp"
#include "InputEnv.hpp"
#include "Weight.hpp"
#include "Transition.hpp"
#include "SWTA.hpp"
......@@ -42,7 +43,7 @@ public:
ParsingEnv(SWTA* wta = NULL, InputSegment* seg = NULL);
/// copy
ParsingEnv(const ParsingEnv&);
ParsingEnv(const ParsingEnv& env);
/// upgrade
ParsingEnv(const InputEnv& env, SWTA* a = NULL);
......@@ -51,24 +52,37 @@ public:
/// @warning the base SWTA and segment must be delete externaly.
virtual ~ParsingEnv();
/// @brief return the 0 value in the weight domains
/// shared by both SWTA of this environment.
/// shortcuts to members of the base SWTA.
/// @warning the base SWTA must be set and non-NULL.
inline Weight weight_zero() const
{ assert(wta); return wta->weight_zero(); }
/// @warning the base SWTA must be set and non-NULL in this environment.
Weight weight_zero() const;
/// @brief return the 1 value in the weight domains
/// shared by both SWTA of this environment.
/// shortcuts to members of the base SWTA.
/// @warning the base SWTA must be set and non-NULL.
inline Weight weight_one() const
{ assert(wta); return wta->weight_one(); }
/// @warning the base SWTA must be set and non-NULL in this environment.
Weight weight_one() const;
/// Transition of dummy label, empty body
/// and weight zero of the domain of the wta of this environment.
/// @warning the base SWTA must be set and non-NULL in this environment.
const Transition& dummyTransition_zero() const;
/// Transition of dummy label, empty body
/// and weight one of the domain of the wta of this environment.
/// @warning the base SWTA must be set and non-NULL in this environment.
const Transition& dummyTransition_one() const;
private:
//void init_pool();
/// dummy transition.
/// with weight one of the domain of the wta of this environment.
/// @warning the base SWTA must be set and non-NULL in this environment.
const Transition _dt0;
const Transition _dt1;
};
} // end namespace Parsing
......
......@@ -29,9 +29,10 @@ public:
/// @brief empty record associated to k.
/// @param comp function for comparing Runs over K.
/// @param failrun fixed run returned by best when there is no best run.
/// @warning the key associated to the record must
/// be set later to a non NULL ptr with initKey.
RecordOne(RunCompare<K> comp);
/// be set later to a non-NULL ptr with setKey.
RecordOne(RunCompare<K> comp, const Run<K>* failrun);
/// deleting a RecordOne frees the best run contained.
virtual ~RecordOne();
......@@ -44,6 +45,8 @@ public:
/// @brief returns the 1-th best run of the record.
/// @param n rank, must be 1 for RecordOne.
/// @return the n-th best run of the record or, if there is none,
/// the failRun set or NULL when the failRun was not set with setFailRun.
virtual const Run<K>* best(size_t n=1);
//virtual Run<K>* best(TableParse<K,Record<K,H>,H>* table, size_t n=1);
......@@ -56,10 +59,13 @@ protected:
};
} // end namespace Parsing
// separated definition of template class
#include "RecordOne.tpp"
} // end namespace Parsing
#endif /* RecordOne_hpp */
......
......@@ -7,10 +7,12 @@
//
namespace Parsing{
template<class K>
RecordOne<K>::RecordOne(RunCompare<K> comp):
Record<K>(comp), // base class constructor with parameter
_best(NULL)
RecordOne<K>::RecordOne(RunCompare<K> comp, const Run<K>* failrun):
Record<K>(comp, failrun), // base class constructor with parameter
_best(nullptr)
{ }
......@@ -38,24 +40,24 @@ template<class K>
const Run<K>* RecordOne<K>::best(size_t n)
{
assert(n == 1);
assert ((_best == NULL) || _best->complete());
assert ((_best == NULL) || (! _best->weight().unknown()));
assert ((_best == nullptr) || _best->complete());
assert ((_best == nullptr) || (! _best->weight().unknown()));
const K* key = this->_key;
assert(key);
// k-best run already computed at construction
if (_best == NULL)
// k-best run _best is already computed at construction
// return a fail Run when there exist no best
if (_best == nullptr)
{
TRACE("RecordOne[{}].best = NULL run", *key);
const Run<K>* fail = this->_fail;
assert((fail == nullptr) || fail->fail());
// return a fail Run
if (this->_fail == NULL)
if (fail == nullptr)
{
this->_fail = new Run<K>(key);
WARN("RecordOne[{}].best: failRun is NULL", *key);
}
assert(this->_fail->fail());
assert(this->_fail->skip() == key->size()); // number of points to skip
return this->_fail;
return fail;
}
return _best;
......@@ -81,7 +83,7 @@ size_t RecordOne<K>::add(const Run<K>* r)
// ignore invalid runs
if (! this->valid(r))
{
if (r == NULL)
if (r == nullptr)
{
WARN("RecordOne[{}].add: reject NULL run", thiskey);
return 2;
......@@ -96,8 +98,8 @@ size_t RecordOne<K>::add(const Run<K>* r)
assert(r);
assert (r->inner() || r->terminal());
assert ((_best == NULL) || _best->complete());
assert ((_best == NULL) || (! _best->weight().unknown()));
assert ((_best == nullptr) || _best->complete());
assert ((_best == nullptr) || (! _best->weight().unknown()));
if (r->partial())
{
......@@ -105,7 +107,7 @@ size_t RecordOne<K>::add(const Run<K>* r)
delete r;
return 5;
}
else if ((_best == NULL) || this->_comp(_best, r))
else if ((_best == nullptr) || this->_comp(_best, r))
{
assert(r->complete());
// r is the new best
......@@ -142,5 +144,8 @@ size_t RecordOne<K>::add(const Run<K>* r)
template<class K>
bool RecordOne<K>::empty() const
{
return (_best == NULL);
return (_best == nullptr);
}
} // end namespace Parsing
......@@ -27,9 +27,10 @@ class RecordQueue : public Record<K>
public:
/// @brief empty record associated to k.
/// @param comp function for comparing Runs over K.
/// @param failrun fixed run returned by best when there is no best run.
/// @warning the key associated to the record must
/// be set later to a non NULL ptr with initKey.
RecordQueue(RunCompare<K> comp);
RecordQueue(RunCompare<K> comp, const Run<K>* failrun);
/// deleting a RecordQueue frees all runs contained
/// in the candidate queue and the best list.
......@@ -45,10 +46,11 @@ public:
/// @brief n-th best run of the record.
/// @param n (as in n-best) is the rank of the run
/// with target to the key of the record.
/// @return the n-th best run of the record or NULL if there is none.
/// Fill the list of best runs up to (at most) k if necessary.
/// @return the n-th best run of the record or, if there is none,
/// the failRun set or NULL when the failRun was not set with setFailRun.
/// Fill the list of best runs up to (at most) n if necessary.
/// If less than k best can be constructed (table is complete),
/// return an NULL run (weight unknown),
/// return a fail run or NULL run (weight unknown),
/// otherwise, the weight of the returned run is known.
virtual const Run<K>* best(size_t n=1);
......@@ -110,10 +112,13 @@ protected:
void addNext(RunRanked<K>* r);
};
} // end namespace Parsing