From 3e1299fc49c3a1a717fcee0d6c5d849d1f64a627 Mon Sep 17 00:00:00 2001 From: Ulya Trofimovich Date: Wed, 6 Mar 2019 21:47:49 +0000 Subject: [PATCH] Improved label generation with EOF rule (removed unused and added missing labels). --- re2c/src/codegen/emit_action.cc | 14 ++++++++------ re2c/src/codegen/emit_dfa.cc | 3 --- re2c/src/codegen/go.h | 1 + re2c/src/codegen/go_construct.cc | 1 + re2c/src/codegen/go_used_labels.cc | 3 +++ re2c/test/eof/eof_01.i--eager-skip.c | 10 ++-------- re2c/test/eof/eof_01.i.c | 4 ---- re2c/test/eof/eof_02.i.c | 3 --- re2c/test/eof/eof_03.i.c | 3 --- re2c/test/eof/eof_04.i8.c | 3 --- re2c/test/eof/nonblocking_push.fi.c | 23 ----------------------- re2c/test/eof/utf8_any.i.c | 1 - 12 files changed, 15 insertions(+), 54 deletions(-) diff --git a/re2c/src/codegen/emit_action.cc b/re2c/src/codegen/emit_action.cc index 4869f866..b25370b2 100644 --- a/re2c/src/codegen/emit_action.cc +++ b/re2c/src/codegen/emit_action.cc @@ -241,15 +241,16 @@ void need(Output &o, uint32_t ind, size_t some) void gen_rescan_label(Output &o, const State *s) { const opt_t *opts = o.block().opts; - - if (opts->eof == NOEOF || endstate(s)) return; - - o.wstring(opts->labelPrefix).wlabel(s->label).ws("_:\n"); - - if (opts->fFlag) { + if (opts->eof == NOEOF || endstate(s)) { + ; // no rescan label + } + else if (opts->fFlag) { o.wstring(opts->yyfilllabel).wu32(o.fill_index).ws(":\n"); ++o.fill_index; } + else { + o.wstring(opts->labelPrefix).wlabel(s->label).ws("_:\n"); + } } void gen_goto_case(Output &o, uint32_t ind, const State *from, const State *to, @@ -566,6 +567,7 @@ bool endstate(const State *s) // usually 'end' states are final states (not all final states are 'end' // states), but sometimes 'end' state happens to be initial non-accepting // state, e.g. in case of rule '[]' + DASSERT(s->go.nSpans > 0); const Action::type_t &a = s->go.span[0].to->action.type; return s->go.nSpans == 1 && (a == Action::RULE || a == Action::ACCEPT); diff --git a/re2c/src/codegen/emit_dfa.cc b/re2c/src/codegen/emit_dfa.cc index 17101147..f81b8e4c 100644 --- a/re2c/src/codegen/emit_dfa.cc +++ b/re2c/src/codegen/emit_dfa.cc @@ -31,9 +31,6 @@ static void emit_eof(Output &, uint32_t, const Code*); void emit_state (Output & o, uint32_t ind, const State * s, bool used_label) { const opt_t *opts = o.block().opts; - used_label |= opts->eof != NOEOF - && (s->action.type == Action::MOVE - || s->action.type == Action::INITIAL); if (used_label) { o.wstring(opts->labelPrefix).wlabel(s->label).ws(":\n"); diff --git a/re2c/src/codegen/go.h b/re2c/src/codegen/go.h index 86ddc3ac..469a5038 100644 --- a/re2c/src/codegen/go.h +++ b/re2c/src/codegen/go.h @@ -85,6 +85,7 @@ struct Linear size_t nbranches; Branch *branches; + const State *def; Linear(const Span *s, uint32_t n, const State *next, bool skip, uint32_t eof); ~Linear(); diff --git a/re2c/src/codegen/go_construct.cc b/re2c/src/codegen/go_construct.cc index a328b792..dc74282a 100644 --- a/re2c/src/codegen/go_construct.cc +++ b/re2c/src/codegen/go_construct.cc @@ -111,6 +111,7 @@ Linear::Linear(const Span *s, uint32_t n, const State *next , bool skip, uint32_t eof) : nbranches(0) , branches(new Branch[n]) + , def(eof == NOEOF ? NULL : next) { for (;;) { if (n == 1 && s[0].to == next) { diff --git a/re2c/src/codegen/go_used_labels.cc b/re2c/src/codegen/go_used_labels.cc index a3cc5c95..7b53240f 100644 --- a/re2c/src/codegen/go_used_labels.cc +++ b/re2c/src/codegen/go_used_labels.cc @@ -32,6 +32,9 @@ void Linear::used_labels (std::set & used) const used.insert(to->label); } } + if (def) { + used.insert(def->label); + } } void If::used_labels (std::set & used) const diff --git a/re2c/test/eof/eof_01.i--eager-skip.c b/re2c/test/eof/eof_01.i--eager-skip.c index 1857cd2a..2fe31ff4 100644 --- a/re2c/test/eof/eof_01.i--eager-skip.c +++ b/re2c/test/eof/eof_01.i--eager-skip.c @@ -49,7 +49,6 @@ static bool lex(input_t & in, unsigned int &count) { char yych; -yy1: yy1_: yych = *in.cur++; switch (yych) { @@ -134,7 +133,6 @@ yy3: yy4: yy4_: yych = *in.cur; -yy5: switch (yych) { case '\n': ++in.cur; @@ -149,9 +147,7 @@ yy6: { continue; } yy7: yy7_: - yych = *in.cur; -yy8: - ++in.cur; + yych = *in.cur++; switch (yych) { case '"': goto yy26; case '\\': goto yy28; @@ -164,9 +160,7 @@ yy8: } yy9: yy9_: - yych = *in.cur; -yy10: - ++in.cur; + yych = *in.cur++; switch (yych) { case '\'': goto yy29; case '\\': goto yy31; diff --git a/re2c/test/eof/eof_01.i.c b/re2c/test/eof/eof_01.i.c index 3f700f2b..e9e26262 100644 --- a/re2c/test/eof/eof_01.i.c +++ b/re2c/test/eof/eof_01.i.c @@ -49,7 +49,6 @@ static bool lex(input_t & in, unsigned int &count) { char yych; -yy1: yy1_: yych = *in.cur; switch (yych) { @@ -136,7 +135,6 @@ yy4: ++in.cur; yy4_: yych = *in.cur; -yy5: switch (yych) { case '\n': goto yy4; default: @@ -151,7 +149,6 @@ yy7: ++in.cur; yy7_: yych = *in.cur; -yy8: switch (yych) { case '"': goto yy26; case '\\': goto yy28; @@ -166,7 +163,6 @@ yy9: ++in.cur; yy9_: yych = *in.cur; -yy10: switch (yych) { case '\'': goto yy29; case '\\': goto yy31; diff --git a/re2c/test/eof/eof_02.i.c b/re2c/test/eof/eof_02.i.c index 72c1cc0f..fb1d3995 100644 --- a/re2c/test/eof/eof_02.i.c +++ b/re2c/test/eof/eof_02.i.c @@ -49,7 +49,6 @@ static bool lex(input_t & in, unsigned int &count) { char yych; -yy1: yy1_: yych = *in.cur; switch (yych) { @@ -78,7 +77,6 @@ yy4: ++in.cur; yy4_: yych = *in.cur; -yy5: switch (yych) { case '\n': goto yy4; default: @@ -93,7 +91,6 @@ yy7: ++in.cur; yy7_: yych = *in.cur; -yy8: switch (yych) { case '0': case '1': diff --git a/re2c/test/eof/eof_03.i.c b/re2c/test/eof/eof_03.i.c index d1911156..6d1b5914 100644 --- a/re2c/test/eof/eof_03.i.c +++ b/re2c/test/eof/eof_03.i.c @@ -49,7 +49,6 @@ static bool lex(input_t & in, unsigned int &count) { char yych; -yy1: yy1_: yych = *in.cur; switch (yych) { @@ -78,7 +77,6 @@ yy4: ++in.cur; yy4_: yych = *in.cur; -yy5: switch (yych) { case '\n': goto yy4; default: @@ -93,7 +91,6 @@ yy7: ++in.cur; yy7_: yych = *in.cur; -yy8: switch (yych) { case '0': case '1': diff --git a/re2c/test/eof/eof_04.i8.c b/re2c/test/eof/eof_04.i8.c index fdc8e242..80df5908 100644 --- a/re2c/test/eof/eof_04.i8.c +++ b/re2c/test/eof/eof_04.i8.c @@ -54,7 +54,6 @@ static bool lex(input_t & in, unsigned int &count) { uint8_t yych; unsigned int yyaccept = 0; -yy1: yy1_: yych = *in.cur; switch (yych) { @@ -249,7 +248,6 @@ yy2: in.mar = ++in.cur; yy2_: yych = *in.cur; -yy3: switch (yych) { case 0x00: case 0x01: @@ -441,7 +439,6 @@ yy5: ++in.cur; yy5_: yych = *in.cur; -yy6: switch (yych) { case '\n': goto yy5; default: diff --git a/re2c/test/eof/nonblocking_push.fi.c b/re2c/test/eof/nonblocking_push.fi.c index 197cb810..3caaeddd 100644 --- a/re2c/test/eof/nonblocking_push.fi.c +++ b/re2c/test/eof/nonblocking_push.fi.c @@ -118,9 +118,7 @@ case 20: goto yyFillLabel20; -yy1: yy0: -yy1_: yyFillLabel0: in.yych = *in.cur; switch (in.yych) { @@ -194,10 +192,8 @@ yy4: { printf("< Unexpected character >%c<\n", in.yych); return FAIL; } yy6: ++in.cur; -yy6_: yyFillLabel1: in.yych = *in.cur; -yy7: switch (in.yych) { case '\n': case ' ': goto yy6; @@ -212,7 +208,6 @@ yy8: { printf("< whitespace\n"); return WHITESPACE; } yy9: ++in.cur; -yy9_: yyFillLabel2: in.yych = *in.cur; yy10: @@ -280,7 +275,6 @@ yy11: { printf("< word\n"); return WORD; } yy12: ++in.cur; -yy12_: yyFillLabel3: in.yych = *in.cur; switch (in.yych) { @@ -295,7 +289,6 @@ yyFillLabel3: } yy13: ++in.cur; -yy13_: yyFillLabel4: in.yych = *in.cur; switch (in.yych) { @@ -310,7 +303,6 @@ yyFillLabel4: } yy14: ++in.cur; -yy14_: yyFillLabel5: in.yych = *in.cur; switch (in.yych) { @@ -325,7 +317,6 @@ yyFillLabel5: } yy15: ++in.cur; -yy15_: yyFillLabel6: in.yych = *in.cur; switch (in.yych) { @@ -340,7 +331,6 @@ yyFillLabel6: } yy16: in.mark = ++in.cur; -yy16_: yyFillLabel7: in.yych = *in.cur; switch (in.yych) { @@ -355,7 +345,6 @@ yyFillLabel7: } yy17: ++in.cur; -yy17_: yyFillLabel8: in.yych = *in.cur; switch (in.yych) { @@ -372,7 +361,6 @@ yy18: goto yy11; yy19: ++in.cur; -yy19_: yyFillLabel9: in.yych = *in.cur; switch (in.yych) { @@ -386,7 +374,6 @@ yyFillLabel9: } yy20: ++in.cur; -yy20_: yyFillLabel10: in.yych = *in.cur; switch (in.yych) { @@ -400,7 +387,6 @@ yyFillLabel10: } yy21: ++in.cur; -yy21_: yyFillLabel11: in.yych = *in.cur; switch (in.yych) { @@ -414,7 +400,6 @@ yyFillLabel11: } yy22: ++in.cur; -yy22_: yyFillLabel12: in.yych = *in.cur; switch (in.yych) { @@ -428,7 +413,6 @@ yyFillLabel12: } yy23: ++in.cur; -yy23_: yyFillLabel13: in.yych = *in.cur; switch (in.yych) { @@ -442,7 +426,6 @@ yyFillLabel13: } yy24: ++in.cur; -yy24_: yyFillLabel14: in.yych = *in.cur; switch (in.yych) { @@ -456,7 +439,6 @@ yyFillLabel14: } yy25: ++in.cur; -yy25_: yyFillLabel15: in.yych = *in.cur; switch (in.yych) { @@ -470,7 +452,6 @@ yyFillLabel15: } yy26: ++in.cur; -yy26_: yyFillLabel16: in.yych = *in.cur; switch (in.yych) { @@ -484,7 +465,6 @@ yyFillLabel16: } yy27: ++in.cur; -yy27_: yyFillLabel17: in.yych = *in.cur; switch (in.yych) { @@ -498,7 +478,6 @@ yyFillLabel17: } yy28: ++in.cur; -yy28_: yyFillLabel18: in.yych = *in.cur; switch (in.yych) { @@ -512,7 +491,6 @@ yyFillLabel18: } yy29: ++in.cur; -yy29_: yyFillLabel19: in.yych = *in.cur; switch (in.yych) { @@ -526,7 +504,6 @@ yyFillLabel19: } yy30: ++in.cur; -yy30_: yyFillLabel20: in.yych = *in.cur; switch (in.yych) { diff --git a/re2c/test/eof/utf8_any.i.c b/re2c/test/eof/utf8_any.i.c index e4702f54..3bcae189 100644 --- a/re2c/test/eof/utf8_any.i.c +++ b/re2c/test/eof/utf8_any.i.c @@ -1395,7 +1395,6 @@ static Result lex1_simple(uint8_t *cur, uint8_t* end, uint8_t* /* unused */) { uint8_t yych; -yy31: yy31_: yych = *cur; switch (yych) { -- 2.49.0