File size: 5,352 Bytes
4eaabb5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | diff --git a/src/regexp/nullable.cc b/src/regexp/nullable.cc
index 8156b47ef..40bbca79f 100644
--- a/src/regexp/nullable.cc
+++ b/src/regexp/nullable.cc
@@ -11,43 +11,100 @@
namespace re2c {
+namespace {
-static bool nullable(const RESpec &spec, const RE *re, bool &trail)
+struct StackItem {
+ const RE *re; // current sub-RE
+ uint8_t succ; // index of the next sucessor to be visited
+};
+
+static bool nullable(const RESpec &spec, std::vector<StackItem> &stack, const RE *re0)
{
- if (trail) return true;
-
- switch (re->type) {
- case RE::NIL: return true;
- case RE::SYM: return false;
- case RE::ITER:
- return nullable(spec, re->iter.re, trail);
- case RE::TAG:
- trail |= trailing(spec.tags[re->tag.idx]);
- return true;
- case RE::ALT:
- return nullable(spec, re->alt.re1, trail)
- || nullable(spec, re->alt.re2, trail);
- case RE::CAT:
- return nullable(spec, re->cat.re1, trail)
- && nullable(spec, re->cat.re2, trail);
+ // the "nullable" status of the last sub-RE visited by DFS
+ bool null = false;
+
+ const StackItem i0 = {re0, 0};
+ stack.push_back(i0);
+
+ while (!stack.empty()) {
+ const StackItem i = stack.back();
+ stack.pop_back();
+
+ const RE *re = i.re;
+ if (re->type == RE::NIL) {
+ null = true;
+ }
+ else if (re->type == RE::SYM) {
+ null = false;
+ }
+ else if (re->type == RE::TAG) {
+ null = true;
+
+ // Trailing context is always in top-level concatenation, and sub-RE
+ // are visited from left to right. Since we are here, sub-RE to the
+ // left of the trailing context is nullable (otherwise we would not
+ // recurse into the right sub-RE), therefore the whole RE is nullable.
+ if (trailing(spec.tags[re->tag.idx])) {
+ DASSERT(stack.size() == 1 && stack.back().re->type == RE::CAT);
+ stack.pop_back();
+ break;
+ }
+ }
+ else if (re->type == RE::ALT) {
+ if (i.succ == 0) {
+ // recurse into the left sub-RE
+ StackItem k = {re, 1};
+ stack.push_back(k);
+ StackItem j = {re->alt.re1, 0};
+ stack.push_back(j);
+ }
+ else if (!null) {
+ // if the left sub-RE is nullable, so is alternative, so stop
+ // recursion; otherwise recurse into the right sub-RE
+ StackItem j = {re->alt.re2, 0};
+ stack.push_back(j);
+ }
+ }
+ else if (re->type == RE::CAT) {
+ if (i.succ == 0) {
+ // recurse into the left sub-RE
+ StackItem k = {re, 1};
+ stack.push_back(k);
+ StackItem j = {re->cat.re1, 0};
+ stack.push_back(j);
+ }
+ else if (null) {
+ // if the left sub-RE is not nullable, neither is concatenation,
+ // so stop recursion; otherwise recurse into the right sub-RE
+ StackItem j = {re->cat.re2, 0};
+ stack.push_back(j);
+ }
+ }
+ else if (re->type == RE::ITER) {
+ // iteration is nullable if the sub-RE is nullable
+ // (zero repetitions is represented with alternative)
+ StackItem j = {re->iter.re, 0};
+ stack.push_back(j);
+ }
}
- return false; /* unreachable */
+
+ DASSERT(stack.empty());
+ return null;
}
-/*
- * warn about rules that match empty string
- * (including rules with nonempty trailing context)
- * false positives on partially self-shadowed rules like [^]?
- */
+} // anonymous namespace
+
+// Warn about rules that match empty string (including rules with nonempty
+// trailing context). False positives on partially self-shadowed rules like [^]?
void warn_nullable(const RESpec &spec, const std::string &cond)
{
// rule for <> is special -- it doesn't have a regexp
if (cond == "0") return;
+ std::vector<StackItem> stack;
const size_t nre = spec.res.size();
for (size_t i = 0; i < nre; ++i) {
- bool trail = false;
- if (nullable(spec, spec.res[i], trail)) {
+ if (nullable(spec, stack, spec.res[i])) {
spec.msg.warn.match_empty_string(spec.rules[i].code->loc, cond);
}
}
diff --git a/test/messages/nullable.i.c b/test/messages/nullable.i.c
new file mode 100644
index 000000000..6122bca9f
--- /dev/null
+++ b/test/messages/nullable.i.c
@@ -0,0 +1,20 @@
+/* Generated by re2c */
+
+{
+ YYCTYPE yych;
+ if (YYLIMIT <= YYCURSOR) YYFILL(1);
+ yych = *YYCURSOR;
+ switch (yych) {
+ case 'b': goto yy3;
+ default: goto yy2;
+ }
+yy2:
+ {}
+yy3:
+ ++YYCURSOR;
+ YYCURSOR -= 1;
+ {}
+}
+
+messages/nullable.i.re:3:9: warning: rule matches empty string [-Wmatch-empty-string]
+messages/nullable.i.re:4:3: warning: rule matches empty string [-Wmatch-empty-string]
diff --git a/test/messages/nullable.i.re b/test/messages/nullable.i.re
new file mode 100644
index 000000000..096e82ff1
--- /dev/null
+++ b/test/messages/nullable.i.re
@@ -0,0 +1,6 @@
+/*!re2c
+
+"" / "b" {}
+"" {}
+
+*/ |