Ignore:
Timestamp:
Dec 24, 2017, 3:00:40 PM (14 months ago)
Author:
cameron
Message:

Correction for nullable assertions; clean-up

File:
1 edited

Legend:

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

    r5267 r5806  
    9393}
    9494
    95 // Deal with case: R1 (Assertion R2) R3
    96 // If R2 is nullable, then R1 R3.
    97 RE * RE_Nullable::removeNullableAssertion(RE * re) {
    98     if (Assertion * a = dyn_cast<Assertion>(re)) {
    99         if (isNullable(a->getAsserted())) {
    100             std::vector<RE *> seq;
    101             return makeSeq(seq.begin(), seq.end());
    102         } else {
    103             return re;
    104         }
    105     } else if (Seq * seq = dyn_cast<Seq>(re)) {
    106         std::vector<RE*> list;
    107         for (auto i = seq->begin(); i != seq->end(); ++i) {
    108             list.push_back(removeNullableAssertion(*i));
    109         }
    110         re = makeSeq(list.begin(), list.end());
    111     } else if (Alt * alt = dyn_cast<Alt>(re)) {
    112         std::vector<RE*> list;
    113         for (auto i = alt->begin(); i != alt->end(); ++i) {
    114             list.push_back(removeNullableAssertion(*i));
    115         }
    116         re = makeAlt(list.begin(), list.end());
    117     }
    118     return re;
    119 }
    120 
    121 // Deal with case: R1 (Assertion R2) R3
    122 // If R3 is nullable, then R1 R2.
    123 RE * RE_Nullable::removeNullableAfterAssertion(RE * re) {
    124     if (isNullableAfterAssertion(re)) {
    125         re = removeNullableAfterAssertion_helper(re);
    126     }
    127     return re;
    128 }
    129 
    130 bool RE_Nullable::isNullableAfterAssertion(const RE * re) {
    131     bool nullable = false;
    132     if (const Seq * seq = dyn_cast<const Seq>(re)) {
    133         if (isNullable(re)) {
    134             return nullable;
    135         } else {
    136             nullable = isa<Assertion>(seq->back()) ? true : isNullableAfterAssertion(seq->back());
    137         }
    138     } else if (const Alt * alt = dyn_cast<const Alt>(re)) {
    139         for (const RE * re : *alt) {
    140             if (isNullableAfterAssertion(re)) {
    141                 nullable = true;
    142                 break;
    143             }
    144         }
    145     }   
    146     return nullable;
    147 }
    148 
    149 RE * RE_Nullable::removeNullableAfterAssertion_helper(RE * re) {
    150     if (Assertion * a = dyn_cast<Assertion>(re)) {
    151         if (a->getSense() == Assertion::Sense::Positive) {
    152             return a->getAsserted();
    153         } else {
    154             return makeDiff(makeAny(), a->getAsserted());
    155         }
    156     } else if (Seq * seq = dyn_cast<Seq>(re)) {
    157         std::vector<RE*> list;
    158         auto i = seq->begin();
    159         for (; i != seq->end() - 1; ++i) {
    160             list.push_back(*i);
    161         }
    162         list.push_back(removeNullableAfterAssertion_helper(*i));
    163         re = makeSeq(list.begin(), list.end());
    164     } else if (Alt * alt = dyn_cast<Alt>(re)) {
    165         std::vector<RE*> list;
    166         for (auto i = alt->begin(); i != alt->end(); ++i) {
    167             list.push_back(removeNullableAfterAssertion_helper(*i));
    168         }
    169         re = makeAlt(list.begin(), list.end());
    170     }
    171     return re;
    172 }
    173 
    17495bool RE_Nullable::isNullable(const RE * re) {
    17596    if (const Seq * re_seq = dyn_cast<const Seq>(re)) {
Note: See TracChangeset for help on using the changeset viewer.