output: de-duplicate token value pushing

This commit is contained in:
Matthias Schiffer 2015-04-11 00:09:33 +02:00
parent 1f2a900e40
commit 0f3c53fb63
4 changed files with 28 additions and 58 deletions

View file

@ -302,7 +302,7 @@ void output_t::emit_source() {
emit_reductions(); emit_reductions();
std::fprintf(source_file, "int %spush(%scontext_t *parser, int token, const %stoken_value_t *value", prefix(), prefix(), prefix()); std::fprintf(source_file, "static int %sdo_push(%scontext_t *parser, int token", prefix(), prefix());
for (const auto &arg : get_generator()->get_grammar().extra_args) for (const auto &arg : get_generator()->get_grammar().extra_args)
std::fprintf(source_file, ", %s %s", arg.first.c_str(), arg.second.c_str()); std::fprintf(source_file, ", %s %s", arg.first.c_str(), arg.second.c_str());
std::fprintf(source_file, ") {\n"); std::fprintf(source_file, ") {\n");
@ -314,6 +314,23 @@ void output_t::emit_source() {
std::fprintf(source_file, "\t\t}\n"); std::fprintf(source_file, "\t\t}\n");
std::fprintf(source_file, "\t}\n"); std::fprintf(source_file, "\t}\n");
std::fprintf(source_file, "}\n\n");
std::fprintf(source_file, "int %spush(%scontext_t *parser, int token, const %stoken_value_t *value", prefix(), prefix(), prefix());
for (const auto &arg : get_generator()->get_grammar().extra_args)
std::fprintf(source_file, ", %s %s", arg.first.c_str(), arg.second.c_str());
std::fprintf(source_file, ") {\n");
std::fprintf(source_file, "\tint ret = %sdo_push(parser, token", prefix());
for (const auto &arg : get_generator()->get_grammar().extra_args)
std::fprintf(source_file, ", %s", arg.second.c_str());
std::fprintf(source_file, ");\n\n");
std::fprintf(source_file, "\tif (ret > 0)\n");
std::fprintf(source_file, "\t\tparser->stack[parser->top-1].value.token = *value;\n\n");
std::fprintf(source_file, "\treturn ret;\n");
std::fprintf(source_file, "}\n"); std::fprintf(source_file, "}\n");
} }

View file

@ -43,7 +43,6 @@ void output_lr0_t::emit_state_shift(unsigned state) {
continue; continue;
std::fprintf(source_file, "\t\t\tcase %s:\n", symbol_case(token).c_str()); std::fprintf(source_file, "\t\t\tcase %s:\n", symbol_case(token).c_str());
std::fprintf(source_file, "\t\t\t\tparser->stack[parser->top].value.token = *value;\n");
std::fprintf(source_file, "\t\t\t\tparser->stack[++parser->top].state = %u;\n", unsigned(it->second)); std::fprintf(source_file, "\t\t\t\tparser->stack[++parser->top].state = %u;\n", unsigned(it->second));
std::fprintf(source_file, "\t\t\t\treturn 1;\n\n"); std::fprintf(source_file, "\t\t\t\treturn 1;\n\n");
} }

View file

@ -45,7 +45,6 @@ void output_slr_t::emit_state_shift(unsigned state) {
for (const symbol_t &sym : entry.second) for (const symbol_t &sym : entry.second)
std::fprintf(source_file, "\t\t\tcase %s:\n", symbol_case(sym).c_str()); std::fprintf(source_file, "\t\t\tcase %s:\n", symbol_case(sym).c_str());
std::fprintf(source_file, "\t\t\t\tparser->stack[parser->top].value.token = *value;\n");
std::fprintf(source_file, "\t\t\t\tparser->stack[++parser->top].state = %u;\n", entry.first); std::fprintf(source_file, "\t\t\t\tparser->stack[++parser->top].state = %u;\n", entry.first);
std::fprintf(source_file, "\t\t\t\treturn 1;\n\n"); std::fprintf(source_file, "\t\t\t\treturn 1;\n\n");
} }

View file

@ -158,7 +158,7 @@ static inline std::string * parse_reduce_22(std::string * v, __attribute__((unus
return v; return v;
} }
int parse_push(parse_context_t *parser, int token, const parse_token_value_t *value, __attribute__((unused)) solar::grammar_t * grammar) { static int parse_do_push(parse_context_t *parser, int token, __attribute__((unused)) solar::grammar_t * grammar) {
while (1) { while (1) {
switch (parser->stack[parser->top].state) { switch (parser->stack[parser->top].state) {
case 0: case 0:
@ -174,12 +174,10 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
return 0; return 0;
case TOK_SYMBOL_LC: case TOK_SYMBOL_LC:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 4; parser->stack[++parser->top].state = 4;
return 1; return 1;
case '%': case '%':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 5; parser->stack[++parser->top].state = 5;
return 1; return 1;
@ -208,7 +206,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 4: case 4:
switch (token) { switch (token) {
case '|': case '|':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 6; parser->stack[++parser->top].state = 6;
return 1; return 1;
@ -220,22 +217,18 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 5: case 5:
switch (token) { switch (token) {
case 'e': case 'e':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 7; parser->stack[++parser->top].state = 7;
return 1; return 1;
case 'h': case 'h':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 8; parser->stack[++parser->top].state = 8;
return 1; return 1;
case 's': case 's':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 9; parser->stack[++parser->top].state = 9;
return 1; return 1;
case 't': case 't':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 10; parser->stack[++parser->top].state = 10;
return 1; return 1;
@ -247,7 +240,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 6: case 6:
switch (token) { switch (token) {
case '=': case '=':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 11; parser->stack[++parser->top].state = 11;
return 1; return 1;
@ -259,7 +251,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 7: case 7:
switch (token) { switch (token) {
case 'x': case 'x':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 12; parser->stack[++parser->top].state = 12;
return 1; return 1;
@ -271,7 +262,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 8: case 8:
switch (token) { switch (token) {
case 'e': case 'e':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 13; parser->stack[++parser->top].state = 13;
return 1; return 1;
@ -283,7 +273,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 9: case 9:
switch (token) { switch (token) {
case 'o': case 'o':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 14; parser->stack[++parser->top].state = 14;
return 1; return 1;
@ -295,7 +284,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 10: case 10:
switch (token) { switch (token) {
case 'y': case 'y':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 15; parser->stack[++parser->top].state = 15;
return 1; return 1;
@ -315,7 +303,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 12: case 12:
switch (token) { switch (token) {
case 't': case 't':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 17; parser->stack[++parser->top].state = 17;
return 1; return 1;
@ -327,7 +314,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 13: case 13:
switch (token) { switch (token) {
case 'a': case 'a':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 18; parser->stack[++parser->top].state = 18;
return 1; return 1;
@ -339,7 +325,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 14: case 14:
switch (token) { switch (token) {
case 'u': case 'u':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 19; parser->stack[++parser->top].state = 19;
return 1; return 1;
@ -351,7 +336,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 15: case 15:
switch (token) { switch (token) {
case 'p': case 'p':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 20; parser->stack[++parser->top].state = 20;
return 1; return 1;
@ -363,32 +347,26 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 16: case 16:
switch (token) { switch (token) {
case TOK_BLOCK: case TOK_BLOCK:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 24; parser->stack[++parser->top].state = 24;
return 1; return 1;
case TOK_CHAR: case TOK_CHAR:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 25; parser->stack[++parser->top].state = 25;
return 1; return 1;
case TOK_STRING: case TOK_STRING:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 26; parser->stack[++parser->top].state = 26;
return 1; return 1;
case TOK_SYMBOL_LC: case TOK_SYMBOL_LC:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 27; parser->stack[++parser->top].state = 27;
return 1; return 1;
case TOK_SYMBOL_UC: case TOK_SYMBOL_UC:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 28; parser->stack[++parser->top].state = 28;
return 1; return 1;
case ';': case ';':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 29; parser->stack[++parser->top].state = 29;
return 1; return 1;
@ -400,7 +378,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 17: case 17:
switch (token) { switch (token) {
case 'r': case 'r':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 30; parser->stack[++parser->top].state = 30;
return 1; return 1;
@ -412,7 +389,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 18: case 18:
switch (token) { switch (token) {
case 'd': case 'd':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 31; parser->stack[++parser->top].state = 31;
return 1; return 1;
@ -424,7 +400,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 19: case 19:
switch (token) { switch (token) {
case 'r': case 'r':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 32; parser->stack[++parser->top].state = 32;
return 1; return 1;
@ -436,7 +411,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 20: case 20:
switch (token) { switch (token) {
case 'e': case 'e':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 33; parser->stack[++parser->top].state = 33;
return 1; return 1;
@ -457,7 +431,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 22: case 22:
switch (token) { switch (token) {
case '(': case '(':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 34; parser->stack[++parser->top].state = 34;
return 1; return 1;
@ -552,7 +525,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 30: case 30:
switch (token) { switch (token) {
case 'a': case 'a':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 35; parser->stack[++parser->top].state = 35;
return 1; return 1;
@ -564,7 +536,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 31: case 31:
switch (token) { switch (token) {
case 'e': case 'e':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 36; parser->stack[++parser->top].state = 36;
return 1; return 1;
@ -576,7 +547,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 32: case 32:
switch (token) { switch (token) {
case 'c': case 'c':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 37; parser->stack[++parser->top].state = 37;
return 1; return 1;
@ -588,17 +558,14 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 33: case 33:
switch (token) { switch (token) {
case TOK_CHAR: case TOK_CHAR:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 25; parser->stack[++parser->top].state = 25;
return 1; return 1;
case TOK_SYMBOL_UC: case TOK_SYMBOL_UC:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 28; parser->stack[++parser->top].state = 28;
return 1; return 1;
case TOK_SYMBOL_LC: case TOK_SYMBOL_LC:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 39; parser->stack[++parser->top].state = 39;
return 1; return 1;
@ -610,17 +577,14 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 34: case 34:
switch (token) { switch (token) {
case TOK_SYMBOL: case TOK_SYMBOL:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 41; parser->stack[++parser->top].state = 41;
return 1; return 1;
case TOK_SYMBOL_LC: case TOK_SYMBOL_LC:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 42; parser->stack[++parser->top].state = 42;
return 1; return 1;
case TOK_SYMBOL_UC: case TOK_SYMBOL_UC:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 43; parser->stack[++parser->top].state = 43;
return 1; return 1;
@ -632,7 +596,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 35: case 35:
switch (token) { switch (token) {
case '_': case '_':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 44; parser->stack[++parser->top].state = 44;
return 1; return 1;
@ -644,7 +607,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 36: case 36:
switch (token) { switch (token) {
case 'r': case 'r':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 45; parser->stack[++parser->top].state = 45;
return 1; return 1;
@ -656,7 +618,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 37: case 37:
switch (token) { switch (token) {
case 'e': case 'e':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 46; parser->stack[++parser->top].state = 46;
return 1; return 1;
@ -668,7 +629,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 38: case 38:
switch (token) { switch (token) {
case TOK_BLOCK: case TOK_BLOCK:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 47; parser->stack[++parser->top].state = 47;
return 1; return 1;
@ -680,7 +640,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 39: case 39:
switch (token) { switch (token) {
case TOK_BLOCK: case TOK_BLOCK:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 48; parser->stack[++parser->top].state = 48;
return 1; return 1;
@ -692,7 +651,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 40: case 40:
switch (token) { switch (token) {
case ')': case ')':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 49; parser->stack[++parser->top].state = 49;
return 1; return 1;
@ -770,7 +728,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 44: case 44:
switch (token) { switch (token) {
case 'a': case 'a':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 50; parser->stack[++parser->top].state = 50;
return 1; return 1;
@ -782,7 +739,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 45: case 45:
switch (token) { switch (token) {
case TOK_BLOCK: case TOK_BLOCK:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 51; parser->stack[++parser->top].state = 51;
return 1; return 1;
@ -794,7 +750,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 46: case 46:
switch (token) { switch (token) {
case TOK_BLOCK: case TOK_BLOCK:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 52; parser->stack[++parser->top].state = 52;
return 1; return 1;
@ -806,17 +761,14 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 47: case 47:
switch (token) { switch (token) {
case TOK_SYMBOL: case TOK_SYMBOL:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 41; parser->stack[++parser->top].state = 41;
return 1; return 1;
case TOK_SYMBOL_LC: case TOK_SYMBOL_LC:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 42; parser->stack[++parser->top].state = 42;
return 1; return 1;
case TOK_SYMBOL_UC: case TOK_SYMBOL_UC:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 43; parser->stack[++parser->top].state = 43;
return 1; return 1;
@ -846,7 +798,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 50: case 50:
switch (token) { switch (token) {
case 'r': case 'r':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 54; parser->stack[++parser->top].state = 54;
return 1; return 1;
@ -885,7 +836,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 54: case 54:
switch (token) { switch (token) {
case 'g': case 'g':
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 55; parser->stack[++parser->top].state = 55;
return 1; return 1;
@ -897,7 +847,6 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 55: case 55:
switch (token) { switch (token) {
case TOK_BLOCK: case TOK_BLOCK:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 56; parser->stack[++parser->top].state = 56;
return 1; return 1;
@ -909,17 +858,14 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
case 56: case 56:
switch (token) { switch (token) {
case TOK_SYMBOL: case TOK_SYMBOL:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 41; parser->stack[++parser->top].state = 41;
return 1; return 1;
case TOK_SYMBOL_LC: case TOK_SYMBOL_LC:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 42; parser->stack[++parser->top].state = 42;
return 1; return 1;
case TOK_SYMBOL_UC: case TOK_SYMBOL_UC:
parser->stack[parser->top].value.token = *value;
parser->stack[++parser->top].state = 43; parser->stack[++parser->top].state = 43;
return 1; return 1;
@ -940,3 +886,12 @@ int parse_push(parse_context_t *parser, int token, const parse_token_value_t *va
} }
} }
} }
int parse_push(parse_context_t *parser, int token, const parse_token_value_t *value, __attribute__((unused)) solar::grammar_t * grammar) {
int ret = parse_do_push(parser, token, grammar);
if (ret > 0)
parser->stack[parser->top-1].value.token = *value;
return ret;
}