Changeset 5806


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

Correction for nullable assertions; clean-up

Location:
icGREP/icgrep-devel/icgrep/re
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/re/re_assertion.h

    r5725 r5806  
    1111#include <re/re_nullable.h>
    1212#include <re/re_seq.h>
     13#include <re/re_alt.h>
    1314
    1415namespace re {
     
    5455
    5556inline RE * makeAssertion(RE * asserted, Assertion::Kind k, Assertion::Sense s) {
    56     if (RE_Nullable::isNullable(asserted)) return makeSeq();
     57    if (RE_Nullable::isNullable(asserted)) {
     58        if (k == Assertion::Kind::Boundary) {
     59            if (s == Assertion::Sense::Positive) return makeAlt();
     60            else return makeSeq();
     61        }
     62        if (s == Assertion::Sense::Positive) return makeSeq();
     63        else return makeAlt();
     64    }
    5765    return new Assertion(asserted, k, s);
    5866}
  • 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)) {
  • icGREP/icgrep-devel/icgrep/re/re_nullable.h

    r5732 r5806  
    1313    static RE * removeNullablePrefix(RE * re);
    1414    static RE * removeNullableSuffix(RE * re);
    15     static RE * removeNullableAssertion(RE * re);
    16     static RE * removeNullableAfterAssertion(RE * re);
    1715    static bool isNullable(const RE * re);
    1816    static bool hasNullablePrefix(const RE * re);
    1917    static bool hasNullableSuffix(const RE * re);
    2018private:
    21     static bool isNullableAfterAssertion(const RE * re);
    22     static RE * removeNullableAfterAssertion_helper(RE * re);
    2319    static bool isNullable(const Vector * vec);
    2420};
  • icGREP/icgrep-devel/icgrep/re/re_toolchain.cpp

    r5804 r5806  
    9999        errs() << "RemoveNullableSuffix:\n" << Printer_RE::PrintRE(r) << '\n';
    100100    }
    101     r = RE_Nullable::removeNullableAssertion(r);
    102     if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowStrippedREs)) {
    103         errs() << "RemoveNullableAssertion:\n" << Printer_RE::PrintRE(r) << '\n';
    104     }
    105101    r = RE_Star_Normal::star_normal(r);
    106102    if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowSimplifiedREs)) {
     
    136132        errs() << "RemoveNullableSuffix:\n" << Printer_RE::PrintRE(r) << '\n';
    137133    }
    138     r = RE_Nullable::removeNullableAssertion(r);
    139     if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowStrippedREs)) {
    140         errs() << "RemoveNullableAssertion:\n" << Printer_RE::PrintRE(r) << '\n';
    141     }
    142     //r = RE_Nullable::removeNullableAfterAssertion(r);
    143     //if (PrintOptions.isSet(ShowAllREs) || PrintOptions.isSet(ShowStrippedREs)) {
    144     //    errs() << "RemoveNullableAfterAssertion\n" << Printer_RE::PrintRE(r) << '\n';
    145     //}
    146 
    147134    r = RE_Simplifier::simplify(r);
    148135
Note: See TracChangeset for help on using the changeset viewer.