Changeset 6257


Ignore:
Timestamp:
Dec 26, 2018, 6:24:08 AM (4 weeks ago)
Author:
cameron
Message:

Contextual simplification: clean-up and fixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/re/re_contextual_simplification.cpp

    r6256 r6257  
    2323public:
    2424    Context(Seq * s, size_t idx);
    25     bool good() const noexcept;
     25    bool empty() const noexcept;
    2626    RE * simplify(RE * re);
    2727private:
    2828    std::vector<RE *> before;
    2929    std::vector<RE *> after;
    30 
    31     Name * any;
    32 
    33     bool _good = true;
    3430
    3531    template<typename Iterator>
     
    9793class AssertionPrep : public RE_Transformer {
    9894public:
    99     using Sense = Assertion::Sense;
    10095    AssertionPrep(Assertion * assertion);
    10196    RE * transformDiff(Diff * d) override;
    102 private:
    103     Sense sense;
    104     Name * any;
    10597};
    10698
     
    110102: RE_Transformer("Contextual Engine", NameTransformationMode::TransformDefinition)
    111103{
    112     if (idx < s->size()) {
    113         this->before = makeContext(s->rbegin() + (s->size() - idx), s->rend());
    114         this->after = makeContext(s->begin() + idx + 1, s->end());
    115         if (before.size() == 0 && after.size() == 0) {
    116             _good = false;
    117         }
    118     } else {
    119         this->_good = false;
    120     }
    121 
    122     this->any = llvm::cast<Name>(makeAny());
    123 }
    124 
    125 bool Context::good() const noexcept {
    126     return this->_good;
     104    this->before = makeContext(s->rbegin() + (s->size() - idx), s->rend());
     105    this->after = makeContext(s->begin() + idx + 1, s->end());
     106}
     107
     108bool Context::empty() const noexcept {
     109    return before.empty() && after.empty();
    127110}
    128111
     
    182165
    183166RE * Context::transformAssertion(Assertion * a) {
    184     using Kind = Assertion::Kind;
    185     if (good() && ValidateAsserted{}.validateRE(a->getAsserted())) {
    186         AssertionPrep prep{a};
    187         RE * asserted = prep.transformRE(a->getAsserted());
    188         Assertion::Sense sense = a->getSense();
    189         RE * simplifiedAsserted = nullptr;
    190         if (a->getKind() == Kind::Lookahead) {
    191             simplifiedAsserted = simplifyAsserted(asserted, after);
    192         } else if (a->getKind() == Kind::Lookbehind) {
    193             asserted = reverseAsserted(asserted);
    194             simplifiedAsserted = reverseAsserted(simplifyAsserted(asserted, before));
    195         } else {
    196             return a;
    197         }
    198 
    199         if (simplifiedAsserted != nullptr) {
    200             if (simplifiedAsserted == asserted) {
    201                 return a;
    202             } else if (isEmptySet(simplifiedAsserted)) {
    203                 if (sense == Assertion::Sense::Positive) return makeAlt();
    204                 else return makeSeq();
    205             } else if (isEmptySeq(simplifiedAsserted)) {
    206                 if (sense == Assertion::Sense::Positive) return makeSeq();
    207                 else return makeAlt();
    208             } else {
    209                 return makeAssertion(simplifiedAsserted, a->getKind(), sense);
    210             }
    211         } else {
    212             return a;
    213         }
     167    Assertion::Sense sense = a->getSense();
     168    AssertionPrep prep{a};
     169    RE * asserted = prep.transformRE(a->getAsserted());
     170    RE * simplifiedAsserted = nullptr;
     171    if (a->getKind() == Assertion::Kind::Lookahead) {
     172        if (after.empty()) return a;
     173        simplifiedAsserted = simplifyAsserted(asserted, after);
     174    } else if (a->getKind() == Assertion::Kind::Lookbehind) {
     175        if (before.empty()) return a;
     176        asserted = reverseAsserted(asserted);
     177        simplifiedAsserted = reverseAsserted(simplifyAsserted(asserted, before));
    214178    } else {
    215179        return a;
    216180    }
    217 }
    218 
    219 Seq * latterSeq(Seq * s, size_t i) {
    220     Seq * seq = llvm::cast<Seq>(makeSeq());
    221     for (auto it = s->begin() + i; it != s->end(); it++) {
    222         seq->push_back(*it);
    223     }
    224     return s;
     181    if (simplifiedAsserted != nullptr) {
     182        if (simplifiedAsserted == asserted) {
     183            return a;
     184        } else if (isEmptySet(simplifiedAsserted)) {
     185            if (sense == Assertion::Sense::Positive) return makeAlt();
     186            else return makeSeq();
     187        } else if (isEmptySeq(simplifiedAsserted)) {
     188            if (sense == Assertion::Sense::Positive) return makeSeq();
     189            else return makeAlt();
     190        } else {
     191            return makeAssertion(simplifiedAsserted, a->getKind(), sense);
     192        }
     193    } else {
     194        return a;
     195    }
    225196}
    226197
     
    247218        bool isWholeSeqSuperSet = context.size() >= a->size();
    248219        bool didChange = false;
    249         elems.reserve(a->size());
    250220
    251221        for (size_t i = 0; i < std::min(a->size(), context.size()); ++i) {
     
    318288
    319289AssertionPrep::AssertionPrep(Assertion * assertion)
    320 : RE_Transformer("", NameTransformationMode::TransformDefinition),
    321   sense(assertion->getSense())
     290: RE_Transformer("", NameTransformationMode::TransformDefinition)
    322291{
    323     this->any = llvm::cast<Name>(makeAny());
    324292}
    325293
     
    338306    bool hasChanged = false;
    339307    for (size_t i = 0; i < s->size(); ++i) {
    340         if (!validateAssertionFree((*s)[i])) {
     308        if (isZeroWidth((*s)[i])) {
    341309            Context context{s, i};
    342             if (!context.good()) {  // abort simplification is context is bad
     310            if (context.empty()) {
    343311                seq.push_back((*s)[i]);
    344312                continue;
     
    348316                hasChanged = true;
    349317                if (isEmptySet(simp)) {
    350                     return makeAlt();
     318                    return simp; // Propagate failure
    351319                } else {
    352320                    seq.push_back(simp);
Note: See TracChangeset for help on using the changeset viewer.