From: Joel E. Denny Date: Sat, 29 Jul 2006 05:53:42 +0000 (+0000) Subject: Enable declaration of default %printer/%destructor. Make the parser X-Git-Tag: v2.3b~321 X-Git-Url: https://git.saurik.com/bison.git/commitdiff_plain/ec5479ce351b0365549e9c7d570b0ee86a4a6589?ds=inline Enable declaration of default %printer/%destructor. Make the parser use these for all user-declared grammar symbols for which the user does not declare a specific %printer/%destructor. Thus, the parser uses it for token 0 if the user declares it but not if Bison generates it as $end. Discussed starting at , , and . * NEWS (2.3+): Mention. * doc/bison.texinfo (Actions in Mid-Rule): It's no longer impossible to declare a %destructor for a mid-rule's semantic value. It's just impossible to declare one specific to it. (Freeing Discarded Symbols): Mention that @$ can be used in %destructor code. Describe default %destructor form. * src/parse-gram.y (grammar_declaration): Parse default %printer/%destructor declarations. * src/output.c (symbol_destructors_output): Use symbol_destructor_get and symbol_destructor_location_get rather than accessing the destructor and destructor_location members of struct symbol. (symbol_printers_output): Likewise but for %printer's. * src/reader.c (symbol_should_be_used): Likewise but for %destructor's again. * src/symtab.c (default_destructor, default_destructor_location, default_printer, default_printer_location): New static global variables to record the default %destructor and %printer. (symbol_destructor_get, symbol_destructor_location_get, symbol_printer_get, symbol_printer_location_get): New functions to compute the appropriate %destructor and %printer for a symbol. (default_destructor_set, default_printer_set): New functions to set the default %destructor and %printer. * src/symtab.h: Prototype all those new functions. * tests/actions.at (Default %printer and %destructor): New test to check that the right %printer and %destructor are called, that they're not called for $end, and that $$ and @$ work correctly. (Default %printer and %destructor for user-declared end token): New test to check that the default %printer and %destructor are called for a user-declared end token. * tests/input.at (Default %printer and %destructor redeclared, Unused values with default %destructor): New tests to check related grammar warnings and errors. --- diff --git a/ChangeLog b/ChangeLog index d603301c..08c71bc0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,47 @@ +2006-07-29 Joel E. Denny + + Enable declaration of default %printer/%destructor. Make the parser + use these for all user-declared grammar symbols for which the user does + not declare a specific %printer/%destructor. Thus, the parser uses it + for token 0 if the user declares it but not if Bison generates it as + $end. Discussed starting at + , + , + and + . + * NEWS (2.3+): Mention. + * doc/bison.texinfo (Actions in Mid-Rule): It's no longer impossible to + declare a %destructor for a mid-rule's semantic value. It's just + impossible to declare one specific to it. + (Freeing Discarded Symbols): Mention that @$ can be used in %destructor + code. Describe default %destructor form. + * src/parse-gram.y (grammar_declaration): Parse default + %printer/%destructor declarations. + * src/output.c (symbol_destructors_output): Use symbol_destructor_get + and symbol_destructor_location_get rather than accessing the destructor + and destructor_location members of struct symbol. + (symbol_printers_output): Likewise but for %printer's. + * src/reader.c (symbol_should_be_used): Likewise but for %destructor's + again. + * src/symtab.c (default_destructor, default_destructor_location, + default_printer, default_printer_location): New static global + variables to record the default %destructor and %printer. + (symbol_destructor_get, symbol_destructor_location_get, + symbol_printer_get, symbol_printer_location_get): New functions to + compute the appropriate %destructor and %printer for a symbol. + (default_destructor_set, default_printer_set): New functions to set the + default %destructor and %printer. + * src/symtab.h: Prototype all those new functions. + * tests/actions.at (Default %printer and %destructor): New test to + check that the right %printer and %destructor are called, that they're + not called for $end, and that $$ and @$ work correctly. + (Default %printer and %destructor for user-declared end token): New + test to check that the default %printer and %destructor are called for + a user-declared end token. + * tests/input.at (Default %printer and %destructor redeclared, Unused + values with default %destructor): New tests to check related grammar + warnings and errors. + 2006-07-29 Joel E. Denny Clean up handling of %destructor for the end token (token 0). diff --git a/NEWS b/NEWS index d133f193..e9c68cab 100644 --- a/NEWS +++ b/NEWS @@ -12,6 +12,23 @@ Changes in version 2.3+: * Locations columns and lines start at 1. In accordance with the GNU Coding Standards and Emacs. +* You may now declare a default %destructor and %printer: + + For example: + + %union { char *string; } + %token STRING1 + %token STRING2 + %type string1 + %type string2 + %destructor { free ($$); } + %destructor { free ($$); printf ("%d", @$.first_line); } STRING1 string1 + + guarantees that, when the parser discards any user-declared symbol, it passes + its semantic value to `free'. However, when the parser discards a `STRING1' + or a `string1', it also prints its line number to `stdout'. It performs only + the second `%destructor' in this case, so it invokes `free' only once. + * Except for LALR(1) parsers in C with POSIX Yacc emulation enabled (with `-y', `--yacc', or `%yacc'), Bison no longer generates #define statements for associating token numbers with token names. Removing the #define statements diff --git a/doc/bison.texinfo b/doc/bison.texinfo index 2741703e..0f27a575 100644 --- a/doc/bison.texinfo +++ b/doc/bison.texinfo @@ -3348,8 +3348,8 @@ it might discard the previous semantic context @code{$5} without restoring it. Thus, @code{$5} needs a destructor (@pxref{Destructor Decl, , Freeing Discarded Symbols}). -However, Bison currently provides no means to declare a destructor for a -mid-rule action's semantic value. +However, Bison currently provides no means to declare a destructor specific to +a particular mid-rule action's semantic value. One solution is to bury the mid-rule action inside a nonterminal symbol and to declare a destructor for that symbol: @@ -4007,26 +4007,40 @@ symbol is automatically discarded. Invoke the braced @var{code} whenever the parser discards one of the @var{symbols}. Within @var{code}, @code{$$} designates the semantic value associated -with the discarded symbol. The additional parser parameters are also -available (@pxref{Parser Function, , The Parser Function -@code{yyparse}}). +with the discarded symbol, and @code{@@$} designates its location. +The additional parser parameters are also available (@pxref{Parser Function, , +The Parser Function @code{yyparse}}). +@end deffn + +@deffn {Directive} %destructor @{ @var{code} @} +@cindex default %destructor +Invoke the braced @var{code} whenever the parser discards any user-declared +grammar symbol for which the user has not specifically declared any +@code{%destructor}. +This is known as the default @code{%destructor}. +As in the previous form, @code{$$}, @code{@@$}, and the additional parser +parameters are available. @end deffn For instance: @smallexample -%union -@{ - char *string; -@} -%token STRING -%type string -%destructor @{ free ($$); @} STRING string +%union @{ char *string; @} +%token STRING1 +%token STRING2 +%type string1 +%type string2 +%destructor @{ free ($$); @} +%destructor @{ free ($$); printf ("%d", @@$.first_line); @} STRING1 string1 @end smallexample @noindent -guarantees that when a @code{STRING} or a @code{string} is discarded, -its associated memory will be freed. +guarantees that, when the parser discards any user-declared symbol, it passes +its semantic value to @code{free}. +However, when the parser discards a @code{STRING1} or a @code{string1}, it also +prints its line number to @code{stdout}. +It performs only the second @code{%destructor} in this case, so it invokes +@code{free} only once. @sp 1 diff --git a/src/output.c b/src/output.c index 534d82af..5aa029ec 100644 --- a/src/output.c +++ b/src/output.c @@ -390,7 +390,7 @@ symbol_destructors_output (FILE *out) fputs ("m4_define([b4_symbol_destructors], \n[", out); for (i = 0; i < nsyms; ++i) - if (symbols[i]->destructor) + if (symbol_destructor_get (symbols[i])) { symbol *sym = symbols[i]; @@ -399,10 +399,12 @@ symbol_destructors_output (FILE *out) destructor, optional typename. */ fprintf (out, "%s[", sep); sep = ",\n"; - escaped_output (out, sym->destructor_location.start.file); - fprintf (out, ", %d, ", sym->destructor_location.start.line); + escaped_output (out, symbol_destructor_location_get (sym).start.file); + fprintf (out, ", %d, ", + symbol_destructor_location_get (sym).start.line); escaped_output (out, sym->tag); - fprintf (out, ", %d, [[%s]]", sym->number, sym->destructor); + fprintf (out, ", %d, [[%s]]", sym->number, + symbol_destructor_get (sym)); if (sym->type_name) fprintf (out, ", [[%s]]", sym->type_name); fputc (']', out); @@ -423,7 +425,7 @@ symbol_printers_output (FILE *out) fputs ("m4_define([b4_symbol_printers], \n[", out); for (i = 0; i < nsyms; ++i) - if (symbols[i]->printer) + if (symbol_printer_get (symbols[i])) { symbol *sym = symbols[i]; @@ -432,10 +434,10 @@ symbol_printers_output (FILE *out) printer, optional typename. */ fprintf (out, "%s[", sep); sep = ",\n"; - escaped_output (out, sym->printer_location.start.file); - fprintf (out, ", %d, ", sym->printer_location.start.line); + escaped_output (out, symbol_printer_location_get (sym).start.file); + fprintf (out, ", %d, ", symbol_printer_location_get (sym).start.line); escaped_output (out, sym->tag); - fprintf (out, ", %d, [[%s]]", sym->number, sym->printer); + fprintf (out, ", %d, [[%s]]", sym->number, symbol_printer_get (sym)); if (sym->type_name) fprintf (out, ", [[%s]]", sym->type_name); fputc (']', out); diff --git a/src/parse-gram.c b/src/parse-gram.c index 828e9f53..06adea17 100644 --- a/src/parse-gram.c +++ b/src/parse-gram.c @@ -552,7 +552,7 @@ union yyalloc /* YYNNTS -- Number of nonterminals. */ #define YYNNTS 29 /* YYNRULES -- Number of rules. */ -#define YYNRULES 92 +#define YYNRULES 94 /* YYNRULES -- Number of states. */ #define YYNSTATES 131 @@ -608,13 +608,13 @@ static const yytype_uint8 yyprhs[] = 0, 0, 3, 8, 9, 12, 14, 16, 19, 22, 24, 28, 30, 33, 35, 38, 41, 45, 47, 50, 53, 55, 59, 61, 63, 67, 70, 72, 75, 78, - 81, 83, 85, 87, 89, 91, 93, 96, 100, 104, - 106, 108, 109, 111, 115, 116, 120, 121, 125, 129, - 133, 135, 137, 139, 140, 142, 144, 147, 149, 151, - 154, 157, 161, 163, 166, 168, 171, 173, 176, 179, - 180, 184, 186, 190, 193, 194, 197, 200, 204, 208, - 212, 214, 216, 217, 219, 221, 223, 225, 227, 229, - 231, 233, 234 + 81, 83, 85, 87, 89, 91, 93, 96, 99, 103, + 107, 110, 112, 114, 115, 117, 121, 122, 126, 127, + 131, 135, 139, 141, 143, 145, 146, 148, 150, 153, + 155, 157, 160, 163, 167, 169, 172, 174, 177, 179, + 182, 185, 186, 190, 192, 196, 199, 200, 203, 206, + 210, 214, 218, 220, 222, 223, 225, 227, 229, 231, + 233, 235, 237, 239, 240 }; /* YYRHS -- A `-1'-separated list of the rules' RHS. */ @@ -629,21 +629,22 @@ static const yytype_int8 yyrhs[] = -1, 34, -1, 35, 48, 3, -1, 36, 45, -1, 37, -1, 38, 3, -1, 39, 3, -1, 41, 80, -1, 42, -1, 43, -1, 44, -1, 54, -1, 66, - -1, 63, -1, 40, 83, -1, 8, 45, 69, -1, - 9, 45, 69, -1, 19, -1, 32, -1, -1, 49, - -1, 56, 62, 45, -1, -1, 6, 64, 71, -1, - -1, 5, 65, 71, -1, 7, 55, 69, -1, 67, - 68, 69, -1, 10, -1, 11, -1, 12, -1, -1, - 55, -1, 83, -1, 69, 83, -1, 55, -1, 81, - -1, 81, 4, -1, 81, 84, -1, 81, 4, 84, - -1, 70, -1, 71, 70, -1, 73, -1, 72, 73, - -1, 74, -1, 61, 54, -1, 1, 54, -1, -1, - 82, 75, 76, -1, 77, -1, 76, 52, 77, -1, - 76, 54, -1, -1, 77, 83, -1, 77, 45, -1, - 77, 13, 83, -1, 77, 14, 4, -1, 77, 15, - 55, -1, 3, -1, 80, -1, -1, 78, -1, 45, - -1, 49, -1, 46, -1, 50, -1, 81, -1, 84, - -1, 3, -1, -1, 51, 47, -1 + -1, 63, -1, 40, 83, -1, 8, 45, -1, 8, + 45, 69, -1, 9, 45, 69, -1, 9, 45, -1, + 19, -1, 32, -1, -1, 49, -1, 56, 62, 45, + -1, -1, 6, 64, 71, -1, -1, 5, 65, 71, + -1, 7, 55, 69, -1, 67, 68, 69, -1, 10, + -1, 11, -1, 12, -1, -1, 55, -1, 83, -1, + 69, 83, -1, 55, -1, 81, -1, 81, 4, -1, + 81, 84, -1, 81, 4, 84, -1, 70, -1, 71, + 70, -1, 73, -1, 72, 73, -1, 74, -1, 61, + 54, -1, 1, 54, -1, -1, 82, 75, 76, -1, + 77, -1, 76, 52, 77, -1, 76, 54, -1, -1, + 77, 83, -1, 77, 45, -1, 77, 13, 83, -1, + 77, 14, 4, -1, 77, 15, 55, -1, 3, -1, + 80, -1, -1, 78, -1, 45, -1, 49, -1, 46, + -1, 50, -1, 81, -1, 84, -1, 3, -1, -1, + 51, 47, -1 }; /* YYRLINE[YYN] -- source line where rule number YYN was defined. */ @@ -652,13 +653,13 @@ static const yytype_uint16 yyrline[] = 0, 205, 205, 213, 215, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 236, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, - 251, 252, 253, 254, 258, 259, 260, 264, 272, 280, - 284, 298, 299, 303, 331, 331, 336, 336, 341, 352, - 367, 368, 369, 373, 374, 379, 380, 385, 390, 395, - 401, 407, 418, 419, 428, 429, 435, 436, 437, 444, - 444, 448, 449, 450, 455, 456, 458, 460, 462, 464, - 474, 475, 481, 485, 490, 506, 508, 517, 522, 523, - 528, 535, 537 + 251, 252, 253, 254, 258, 259, 260, 264, 268, 276, + 284, 288, 292, 306, 307, 311, 339, 339, 344, 344, + 349, 360, 375, 376, 377, 381, 382, 387, 388, 393, + 398, 403, 409, 415, 426, 427, 436, 437, 443, 444, + 445, 452, 452, 456, 457, 458, 463, 464, 466, 468, + 470, 472, 482, 483, 489, 493, 498, 514, 516, 525, + 530, 531, 536, 543, 545 }; #endif @@ -711,12 +712,12 @@ static const yytype_uint8 yyr1[] = 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 61, 61, 61, 61, 61, 61, - 61, 62, 62, 61, 64, 63, 65, 63, 63, 66, - 67, 67, 67, 68, 68, 69, 69, 70, 70, 70, - 70, 70, 71, 71, 72, 72, 73, 73, 73, 75, - 74, 76, 76, 76, 77, 77, 77, 77, 77, 77, - 78, 78, 79, 79, 80, 81, 81, 82, 83, 83, - 84, 85, 85 + 61, 61, 61, 62, 62, 61, 64, 63, 65, 63, + 63, 66, 67, 67, 67, 68, 68, 69, 69, 70, + 70, 70, 70, 70, 71, 71, 72, 72, 73, 73, + 73, 75, 74, 76, 76, 76, 77, 77, 77, 77, + 77, 77, 78, 78, 79, 79, 80, 81, 81, 82, + 83, 83, 84, 85, 85 }; /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */ @@ -725,13 +726,13 @@ static const yytype_uint8 yyr2[] = 0, 2, 4, 0, 2, 1, 1, 2, 2, 1, 3, 1, 2, 1, 2, 2, 3, 1, 2, 2, 1, 3, 1, 1, 3, 2, 1, 2, 2, 2, - 1, 1, 1, 1, 1, 1, 2, 3, 3, 1, - 1, 0, 1, 3, 0, 3, 0, 3, 3, 3, - 1, 1, 1, 0, 1, 1, 2, 1, 1, 2, - 2, 3, 1, 2, 1, 2, 1, 2, 2, 0, - 3, 1, 3, 2, 0, 2, 2, 3, 3, 3, - 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, - 1, 0, 2 + 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, + 2, 1, 1, 0, 1, 3, 0, 3, 0, 3, + 3, 3, 1, 1, 1, 0, 1, 1, 2, 1, + 1, 2, 2, 3, 1, 2, 1, 2, 1, 2, + 2, 0, 3, 1, 3, 2, 0, 2, 2, 3, + 3, 3, 1, 1, 0, 1, 1, 1, 1, 1, + 1, 1, 1, 0, 2 }; /* YYDEFACT[STATE-NAME] -- Default rule to reduce with in state @@ -739,20 +740,20 @@ static const yytype_uint8 yyr2[] = means the default is an error. */ static const yytype_uint8 yydefact[] = { - 3, 0, 0, 1, 46, 44, 0, 0, 0, 50, - 51, 52, 0, 0, 9, 39, 0, 11, 0, 13, - 0, 0, 0, 17, 0, 0, 20, 0, 40, 22, + 3, 0, 0, 1, 48, 46, 0, 0, 0, 52, + 53, 54, 0, 0, 9, 41, 0, 11, 0, 13, + 0, 0, 0, 17, 0, 0, 20, 0, 42, 22, 23, 0, 0, 26, 0, 0, 0, 0, 30, 31, - 32, 0, 6, 33, 41, 4, 5, 35, 34, 53, - 0, 0, 0, 0, 0, 84, 7, 8, 82, 12, + 32, 0, 6, 33, 43, 4, 5, 35, 34, 55, + 0, 0, 0, 37, 40, 86, 7, 8, 84, 12, 14, 15, 0, 18, 19, 0, 0, 25, 27, 28, - 90, 86, 85, 88, 36, 89, 29, 0, 87, 0, - 0, 64, 66, 69, 42, 0, 54, 0, 57, 62, - 47, 58, 45, 48, 55, 37, 38, 80, 83, 10, - 81, 16, 21, 24, 68, 67, 0, 65, 2, 74, - 43, 49, 63, 59, 60, 56, 92, 70, 71, 61, - 74, 73, 0, 0, 0, 76, 75, 72, 77, 78, - 79 + 92, 88, 87, 90, 36, 91, 29, 0, 89, 0, + 0, 66, 68, 71, 44, 0, 56, 0, 59, 64, + 49, 60, 47, 50, 57, 38, 39, 82, 85, 10, + 83, 16, 21, 24, 70, 69, 0, 67, 2, 76, + 45, 51, 65, 61, 62, 58, 94, 72, 73, 63, + 76, 75, 0, 0, 0, 78, 77, 74, 79, 80, + 81 }; /* YYDEFGOTO[NTERM-NUM]. */ @@ -796,10 +797,10 @@ static const yytype_int8 yypgoto[] = positive, shift that token. If negative, reduce the rule which number is the opposite. If zero, do what YYDEFACT says. If YYTABLE_NINF, syntax error. */ -#define YYTABLE_NINF -92 +#define YYTABLE_NINF -94 static const yytype_int16 yytable[] = { - 74, 114, -91, 77, 95, 96, 97, 4, 5, 6, + 74, 114, -93, 77, 95, 96, 97, 4, 5, 6, 7, 8, 9, 10, 11, 71, 52, 57, 72, 70, 113, 15, 59, 119, 88, 120, 112, 121, 112, 3, 70, 53, 54, 55, 28, 70, 58, 62, 111, 60, @@ -1009,112 +1010,112 @@ yy_symbol_value_print (yyoutput, yytype, yyvaluep, yylocationp) #line 183 "parse-gram.y" { fputs (quotearg_style (c_quoting_style, (yyvaluep->chars)), stderr); }; /* Line 670 of yacc.c. */ -#line 1013 "parse-gram.c" +#line 1014 "parse-gram.c" break; case 4: /* "\"integer\"" */ /* Line 670 of yacc.c. */ #line 194 "parse-gram.y" { fprintf (stderr, "%d", (yyvaluep->integer)); }; /* Line 670 of yacc.c. */ -#line 1020 "parse-gram.c" +#line 1021 "parse-gram.c" break; case 45: /* "\"{...}\"" */ /* Line 670 of yacc.c. */ #line 185 "parse-gram.y" { fprintf (stderr, "{\n%s\n}", (yyvaluep->chars)); }; /* Line 670 of yacc.c. */ -#line 1027 "parse-gram.c" +#line 1028 "parse-gram.c" break; case 46: /* "\"char\"" */ /* Line 670 of yacc.c. */ #line 178 "parse-gram.y" { fputs (char_name ((yyvaluep->character)), stderr); }; /* Line 670 of yacc.c. */ -#line 1034 "parse-gram.c" +#line 1035 "parse-gram.c" break; case 47: /* "\"epilogue\"" */ /* Line 670 of yacc.c. */ #line 185 "parse-gram.y" { fprintf (stderr, "{\n%s\n}", (yyvaluep->chars)); }; /* Line 670 of yacc.c. */ -#line 1041 "parse-gram.c" +#line 1042 "parse-gram.c" break; case 49: /* "\"identifier\"" */ /* Line 670 of yacc.c. */ #line 190 "parse-gram.y" { fputs ((yyvaluep->uniqstr), stderr); }; /* Line 670 of yacc.c. */ -#line 1048 "parse-gram.c" +#line 1049 "parse-gram.c" break; case 50: /* "\"identifier:\"" */ /* Line 670 of yacc.c. */ #line 191 "parse-gram.y" { fprintf (stderr, "%s:", (yyvaluep->uniqstr)); }; /* Line 670 of yacc.c. */ -#line 1055 "parse-gram.c" +#line 1056 "parse-gram.c" break; case 53: /* "\"%{...%}\"" */ /* Line 670 of yacc.c. */ #line 185 "parse-gram.y" { fprintf (stderr, "{\n%s\n}", (yyvaluep->chars)); }; /* Line 670 of yacc.c. */ -#line 1062 "parse-gram.c" +#line 1063 "parse-gram.c" break; case 55: /* "\"type\"" */ /* Line 670 of yacc.c. */ #line 189 "parse-gram.y" { fprintf (stderr, "<%s>", (yyvaluep->uniqstr)); }; /* Line 670 of yacc.c. */ -#line 1069 "parse-gram.c" +#line 1070 "parse-gram.c" break; case 78: /* "content" */ /* Line 670 of yacc.c. */ #line 185 "parse-gram.y" { fprintf (stderr, "{\n%s\n}", (yyvaluep->chars)); }; /* Line 670 of yacc.c. */ -#line 1076 "parse-gram.c" +#line 1077 "parse-gram.c" break; case 79: /* "content.opt" */ /* Line 670 of yacc.c. */ #line 185 "parse-gram.y" { fprintf (stderr, "{\n%s\n}", (yyvaluep->chars)); }; /* Line 670 of yacc.c. */ -#line 1083 "parse-gram.c" +#line 1084 "parse-gram.c" break; case 80: /* "braceless" */ /* Line 670 of yacc.c. */ #line 185 "parse-gram.y" { fprintf (stderr, "{\n%s\n}", (yyvaluep->chars)); }; /* Line 670 of yacc.c. */ -#line 1090 "parse-gram.c" +#line 1091 "parse-gram.c" break; case 81: /* "id" */ /* Line 670 of yacc.c. */ #line 197 "parse-gram.y" { fprintf (stderr, "%s", (yyvaluep->symbol)->tag); }; /* Line 670 of yacc.c. */ -#line 1097 "parse-gram.c" +#line 1098 "parse-gram.c" break; case 82: /* "id_colon" */ /* Line 670 of yacc.c. */ #line 198 "parse-gram.y" { fprintf (stderr, "%s:", (yyvaluep->symbol)->tag); }; /* Line 670 of yacc.c. */ -#line 1104 "parse-gram.c" +#line 1105 "parse-gram.c" break; case 83: /* "symbol" */ /* Line 670 of yacc.c. */ #line 197 "parse-gram.y" { fprintf (stderr, "%s", (yyvaluep->symbol)->tag); }; /* Line 670 of yacc.c. */ -#line 1111 "parse-gram.c" +#line 1112 "parse-gram.c" break; case 84: /* "string_as_id" */ /* Line 670 of yacc.c. */ #line 197 "parse-gram.y" { fprintf (stderr, "%s", (yyvaluep->symbol)->tag); }; /* Line 670 of yacc.c. */ -#line 1118 "parse-gram.c" +#line 1119 "parse-gram.c" break; default: break; @@ -1636,7 +1637,7 @@ YYLTYPE yylloc; boundary_set (&yylloc.end, current_file, 1, 1); } /* Line 1082 of yacc.c. */ -#line 1640 "parse-gram.c" +#line 1641 "parse-gram.c" yylsp[0] = yylloc; goto yysetstate; @@ -1718,6 +1719,9 @@ YYLTYPE yylloc; YYDPRINTF ((stderr, "Entering state %d\n", yystate)); + if (yystate == YYFINAL) + YYACCEPT; + goto yybackup; /*-----------. @@ -1767,9 +1771,6 @@ yybackup: goto yyreduce; } - if (yyn == YYFINAL) - YYACCEPT; - /* Count tokens shifted since error; after three, turn off error status. */ if (yyerrstatus) @@ -1778,9 +1779,8 @@ yybackup: /* Shift the lookahead token. */ YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc); - /* Discard the shifted token unless it is eof. */ - if (yychar != YYEOF) - yychar = YYEMPTY; + /* Discard the shifted token. */ + yychar = YYEMPTY; yystate = yyn; *++yyvsp = yylval; @@ -1821,73 +1821,73 @@ yyreduce: switch (yyn) { case 6: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 220 "parse-gram.y" { prologue_augment (translate_code ((yyvsp[(1) - (1)].chars), (yylsp[(1) - (1)])), (yylsp[(1) - (1)]), union_seen); } break; case 7: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 221 "parse-gram.y" { prologue_augment ((yyvsp[(2) - (2)].chars), (yylsp[(2) - (2)]), true); } break; case 8: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 222 "parse-gram.y" { prologue_augment ((yyvsp[(2) - (2)].chars), (yylsp[(2) - (2)]), false); } break; case 9: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 223 "parse-gram.y" { debug_flag = true; } break; case 10: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 224 "parse-gram.y" { muscle_insert ((yyvsp[(2) - (3)].chars), (yyvsp[(3) - (3)].chars)); } break; case 11: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 225 "parse-gram.y" { defines_flag = true; } break; case 12: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 226 "parse-gram.y" { muscle_code_grow ("end_header", (yyvsp[(2) - (2)].chars), (yylsp[(2) - (2)])); } break; case 13: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 227 "parse-gram.y" { error_verbose = true; } break; case 14: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 228 "parse-gram.y" { expected_sr_conflicts = (yyvsp[(2) - (2)].integer); } break; case 15: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 229 "parse-gram.y" { expected_rr_conflicts = (yyvsp[(2) - (2)].integer); } break; case 16: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 230 "parse-gram.y" { spec_file_prefix = (yyvsp[(3) - (3)].chars); } break; case 17: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 232 "parse-gram.y" { nondeterministic_parser = true; @@ -1896,7 +1896,7 @@ yyreduce: break; case 18: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 237 "parse-gram.y" { muscle_code_grow ("initial_action", translate_symbol_action ((yyvsp[(2) - (2)].chars), (yylsp[(2) - (2)])), (yylsp[(2) - (2)])); @@ -1904,91 +1904,91 @@ yyreduce: break; case 19: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 240 "parse-gram.y" { add_param ("lex_param", (yyvsp[(2) - (2)].chars), (yylsp[(2) - (2)])); } break; case 20: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 241 "parse-gram.y" { locations_flag = true; } break; case 21: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 242 "parse-gram.y" { spec_name_prefix = (yyvsp[(3) - (3)].chars); } break; case 22: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 243 "parse-gram.y" { no_lines_flag = true; } break; case 23: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 244 "parse-gram.y" { nondeterministic_parser = true; } break; case 24: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 245 "parse-gram.y" { spec_outfile = (yyvsp[(3) - (3)].chars); } break; case 25: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 246 "parse-gram.y" { add_param ("parse_param", (yyvsp[(2) - (2)].chars), (yylsp[(2) - (2)])); } break; case 26: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 247 "parse-gram.y" { pure_parser = true; } break; case 27: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 248 "parse-gram.y" { version_check (&(yylsp[(2) - (2)]), (yyvsp[(2) - (2)].chars)); } break; case 28: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 249 "parse-gram.y" { skeleton = (yyvsp[(2) - (2)].chars); } break; case 29: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 250 "parse-gram.y" { muscle_code_grow ("start_header", (yyvsp[(2) - (2)].chars), (yylsp[(2) - (2)])); } break; case 30: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 251 "parse-gram.y" { token_table_flag = true; } break; case 31: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 252 "parse-gram.y" { report_flag = report_states; } break; case 32: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 253 "parse-gram.y" { yacc_flag = true; } break; case 36: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 261 "parse-gram.y" { grammar_start_symbol_set ((yyvsp[(2) - (2)].symbol), (yylsp[(2) - (2)])); @@ -1996,8 +1996,16 @@ yyreduce: break; case 37: -/* Line 1270 of yacc.c. */ +/* Line 1269 of yacc.c. */ #line 265 "parse-gram.y" + { + default_destructor_set (translate_symbol_action ((yyvsp[(2) - (2)].chars), (yylsp[(2) - (2)])), (yylsp[(2) - (2)])); + } + break; + + case 38: +/* Line 1269 of yacc.c. */ +#line 269 "parse-gram.y" { symbol_list *list; const char *action = translate_symbol_action ((yyvsp[(2) - (3)].chars), (yylsp[(2) - (3)])); @@ -2007,9 +2015,9 @@ yyreduce: } break; - case 38: -/* Line 1270 of yacc.c. */ -#line 273 "parse-gram.y" + case 39: +/* Line 1269 of yacc.c. */ +#line 277 "parse-gram.y" { symbol_list *list; const char *action = translate_symbol_action ((yyvsp[(2) - (3)].chars), (yylsp[(2) - (3)])); @@ -2019,37 +2027,45 @@ yyreduce: } break; - case 39: -/* Line 1270 of yacc.c. */ -#line 281 "parse-gram.y" + case 40: +/* Line 1269 of yacc.c. */ +#line 285 "parse-gram.y" + { + default_printer_set (translate_symbol_action ((yyvsp[(2) - (2)].chars), (yylsp[(2) - (2)])), (yylsp[(2) - (2)])); + } + break; + + case 41: +/* Line 1269 of yacc.c. */ +#line 289 "parse-gram.y" { default_prec = true; } break; - case 40: -/* Line 1270 of yacc.c. */ -#line 285 "parse-gram.y" + case 42: +/* Line 1269 of yacc.c. */ +#line 293 "parse-gram.y" { default_prec = false; } break; - case 41: -/* Line 1270 of yacc.c. */ -#line 298 "parse-gram.y" + case 43: +/* Line 1269 of yacc.c. */ +#line 306 "parse-gram.y" {} break; - case 42: -/* Line 1270 of yacc.c. */ -#line 299 "parse-gram.y" + case 44: +/* Line 1269 of yacc.c. */ +#line 307 "parse-gram.y" { muscle_code_grow ("union_name", (yyvsp[(1) - (1)].uniqstr), (yylsp[(1) - (1)])); } break; - case 43: -/* Line 1270 of yacc.c. */ -#line 304 "parse-gram.y" + case 45: +/* Line 1269 of yacc.c. */ +#line 312 "parse-gram.y" { char const *body = (yyvsp[(3) - (3)].chars); @@ -2073,39 +2089,39 @@ yyreduce: } break; - case 44: -/* Line 1270 of yacc.c. */ -#line 331 "parse-gram.y" + case 46: +/* Line 1269 of yacc.c. */ +#line 339 "parse-gram.y" { current_class = nterm_sym; } break; - case 45: -/* Line 1270 of yacc.c. */ -#line 332 "parse-gram.y" + case 47: +/* Line 1269 of yacc.c. */ +#line 340 "parse-gram.y" { current_class = unknown_sym; current_type = NULL; } break; - case 46: -/* Line 1270 of yacc.c. */ -#line 336 "parse-gram.y" + case 48: +/* Line 1269 of yacc.c. */ +#line 344 "parse-gram.y" { current_class = token_sym; } break; - case 47: -/* Line 1270 of yacc.c. */ -#line 337 "parse-gram.y" + case 49: +/* Line 1269 of yacc.c. */ +#line 345 "parse-gram.y" { current_class = unknown_sym; current_type = NULL; } break; - case 48: -/* Line 1270 of yacc.c. */ -#line 342 "parse-gram.y" + case 50: +/* Line 1269 of yacc.c. */ +#line 350 "parse-gram.y" { tag_seen = true; symbol_list *list; @@ -2115,9 +2131,9 @@ yyreduce: } break; - case 49: -/* Line 1270 of yacc.c. */ -#line 353 "parse-gram.y" + case 51: +/* Line 1269 of yacc.c. */ +#line 361 "parse-gram.y" { symbol_list *list; ++current_prec; @@ -2131,69 +2147,69 @@ yyreduce: } break; - case 50: -/* Line 1270 of yacc.c. */ -#line 367 "parse-gram.y" + case 52: +/* Line 1269 of yacc.c. */ +#line 375 "parse-gram.y" { (yyval.assoc) = left_assoc; } break; - case 51: -/* Line 1270 of yacc.c. */ -#line 368 "parse-gram.y" + case 53: +/* Line 1269 of yacc.c. */ +#line 376 "parse-gram.y" { (yyval.assoc) = right_assoc; } break; - case 52: -/* Line 1270 of yacc.c. */ -#line 369 "parse-gram.y" + case 54: +/* Line 1269 of yacc.c. */ +#line 377 "parse-gram.y" { (yyval.assoc) = non_assoc; } break; - case 53: -/* Line 1270 of yacc.c. */ -#line 373 "parse-gram.y" + case 55: +/* Line 1269 of yacc.c. */ +#line 381 "parse-gram.y" { current_type = NULL; } break; - case 54: -/* Line 1270 of yacc.c. */ -#line 374 "parse-gram.y" + case 56: +/* Line 1269 of yacc.c. */ +#line 382 "parse-gram.y" { current_type = (yyvsp[(1) - (1)].uniqstr); tag_seen = true; } break; - case 55: -/* Line 1270 of yacc.c. */ -#line 379 "parse-gram.y" + case 57: +/* Line 1269 of yacc.c. */ +#line 387 "parse-gram.y" { (yyval.list) = symbol_list_new ((yyvsp[(1) - (1)].symbol), (yylsp[(1) - (1)])); } break; - case 56: -/* Line 1270 of yacc.c. */ -#line 380 "parse-gram.y" + case 58: +/* Line 1269 of yacc.c. */ +#line 388 "parse-gram.y" { (yyval.list) = symbol_list_prepend ((yyvsp[(1) - (2)].list), (yyvsp[(2) - (2)].symbol), (yylsp[(2) - (2)])); } break; - case 57: -/* Line 1270 of yacc.c. */ -#line 386 "parse-gram.y" + case 59: +/* Line 1269 of yacc.c. */ +#line 394 "parse-gram.y" { current_type = (yyvsp[(1) - (1)].uniqstr); tag_seen = true; } break; - case 58: -/* Line 1270 of yacc.c. */ -#line 391 "parse-gram.y" + case 60: +/* Line 1269 of yacc.c. */ +#line 399 "parse-gram.y" { symbol_class_set ((yyvsp[(1) - (1)].symbol), current_class, (yylsp[(1) - (1)]), true); symbol_type_set ((yyvsp[(1) - (1)].symbol), current_type, (yylsp[(1) - (1)])); } break; - case 59: -/* Line 1270 of yacc.c. */ -#line 396 "parse-gram.y" + case 61: +/* Line 1269 of yacc.c. */ +#line 404 "parse-gram.y" { symbol_class_set ((yyvsp[(1) - (2)].symbol), current_class, (yylsp[(1) - (2)]), true); symbol_type_set ((yyvsp[(1) - (2)].symbol), current_type, (yylsp[(1) - (2)])); @@ -2201,9 +2217,9 @@ yyreduce: } break; - case 60: -/* Line 1270 of yacc.c. */ -#line 402 "parse-gram.y" + case 62: +/* Line 1269 of yacc.c. */ +#line 410 "parse-gram.y" { symbol_class_set ((yyvsp[(1) - (2)].symbol), current_class, (yylsp[(1) - (2)]), true); symbol_type_set ((yyvsp[(1) - (2)].symbol), current_type, (yylsp[(1) - (2)])); @@ -2211,9 +2227,9 @@ yyreduce: } break; - case 61: -/* Line 1270 of yacc.c. */ -#line 408 "parse-gram.y" + case 63: +/* Line 1269 of yacc.c. */ +#line 416 "parse-gram.y" { symbol_class_set ((yyvsp[(1) - (3)].symbol), current_class, (yylsp[(1) - (3)]), true); symbol_type_set ((yyvsp[(1) - (3)].symbol), current_type, (yylsp[(1) - (3)])); @@ -2222,95 +2238,95 @@ yyreduce: } break; - case 68: -/* Line 1270 of yacc.c. */ -#line 438 "parse-gram.y" + case 70: +/* Line 1269 of yacc.c. */ +#line 446 "parse-gram.y" { yyerrok; } break; - case 69: -/* Line 1270 of yacc.c. */ -#line 444 "parse-gram.y" + case 71: +/* Line 1269 of yacc.c. */ +#line 452 "parse-gram.y" { current_lhs = (yyvsp[(1) - (1)].symbol); current_lhs_location = (yylsp[(1) - (1)]); } break; - case 71: -/* Line 1270 of yacc.c. */ -#line 448 "parse-gram.y" + case 73: +/* Line 1269 of yacc.c. */ +#line 456 "parse-gram.y" { grammar_current_rule_end ((yylsp[(1) - (1)])); } break; - case 72: -/* Line 1270 of yacc.c. */ -#line 449 "parse-gram.y" + case 74: +/* Line 1269 of yacc.c. */ +#line 457 "parse-gram.y" { grammar_current_rule_end ((yylsp[(3) - (3)])); } break; - case 74: -/* Line 1270 of yacc.c. */ -#line 455 "parse-gram.y" + case 76: +/* Line 1269 of yacc.c. */ +#line 463 "parse-gram.y" { grammar_current_rule_begin (current_lhs, current_lhs_location); } break; - case 75: -/* Line 1270 of yacc.c. */ -#line 457 "parse-gram.y" + case 77: +/* Line 1269 of yacc.c. */ +#line 465 "parse-gram.y" { grammar_current_rule_symbol_append ((yyvsp[(2) - (2)].symbol), (yylsp[(2) - (2)])); } break; - case 76: -/* Line 1270 of yacc.c. */ -#line 459 "parse-gram.y" + case 78: +/* Line 1269 of yacc.c. */ +#line 467 "parse-gram.y" { grammar_current_rule_action_append ((yyvsp[(2) - (2)].chars), (yylsp[(2) - (2)])); } break; - case 77: -/* Line 1270 of yacc.c. */ -#line 461 "parse-gram.y" + case 79: +/* Line 1269 of yacc.c. */ +#line 469 "parse-gram.y" { grammar_current_rule_prec_set ((yyvsp[(3) - (3)].symbol), (yylsp[(3) - (3)])); } break; - case 78: -/* Line 1270 of yacc.c. */ -#line 463 "parse-gram.y" + case 80: +/* Line 1269 of yacc.c. */ +#line 471 "parse-gram.y" { grammar_current_rule_dprec_set ((yyvsp[(3) - (3)].integer), (yylsp[(3) - (3)])); } break; - case 79: -/* Line 1270 of yacc.c. */ -#line 465 "parse-gram.y" + case 81: +/* Line 1269 of yacc.c. */ +#line 473 "parse-gram.y" { grammar_current_rule_merge_set ((yyvsp[(3) - (3)].uniqstr), (yylsp[(3) - (3)])); } break; - case 82: -/* Line 1270 of yacc.c. */ -#line 481 "parse-gram.y" + case 84: +/* Line 1269 of yacc.c. */ +#line 489 "parse-gram.y" { static char one[] = "1"; (yyval.chars) = one; } break; - case 84: -/* Line 1270 of yacc.c. */ -#line 491 "parse-gram.y" + case 86: +/* Line 1269 of yacc.c. */ +#line 499 "parse-gram.y" { (yyvsp[(1) - (1)].chars)[strlen ((yyvsp[(1) - (1)].chars)) - 1] = '\n'; (yyval.chars) = translate_code ((yyvsp[(1) - (1)].chars) + 1, (yylsp[(1) - (1)])); } break; - case 85: -/* Line 1270 of yacc.c. */ -#line 507 "parse-gram.y" + case 87: +/* Line 1269 of yacc.c. */ +#line 515 "parse-gram.y" { (yyval.symbol) = symbol_from_uniqstr ((yyvsp[(1) - (1)].uniqstr), (yylsp[(1) - (1)])); } break; - case 86: -/* Line 1270 of yacc.c. */ -#line 509 "parse-gram.y" + case 88: +/* Line 1269 of yacc.c. */ +#line 517 "parse-gram.y" { (yyval.symbol) = symbol_get (char_name ((yyvsp[(1) - (1)].character)), (yylsp[(1) - (1)])); symbol_class_set ((yyval.symbol), token_sym, (yylsp[(1) - (1)]), false); @@ -2318,24 +2334,24 @@ yyreduce: } break; - case 87: -/* Line 1270 of yacc.c. */ -#line 517 "parse-gram.y" + case 89: +/* Line 1269 of yacc.c. */ +#line 525 "parse-gram.y" { (yyval.symbol) = symbol_from_uniqstr ((yyvsp[(1) - (1)].uniqstr), (yylsp[(1) - (1)])); } break; - case 90: -/* Line 1270 of yacc.c. */ -#line 529 "parse-gram.y" + case 92: +/* Line 1269 of yacc.c. */ +#line 537 "parse-gram.y" { (yyval.symbol) = symbol_get (quotearg_style (c_quoting_style, (yyvsp[(1) - (1)].chars)), (yylsp[(1) - (1)])); symbol_class_set ((yyval.symbol), token_sym, (yylsp[(1) - (1)]), false); } break; - case 92: -/* Line 1270 of yacc.c. */ -#line 538 "parse-gram.y" + case 94: +/* Line 1269 of yacc.c. */ +#line 546 "parse-gram.y" { muscle_code_grow ("epilogue", translate_code ((yyvsp[(2) - (2)].chars), (yylsp[(2) - (2)])), (yylsp[(2) - (2)])); gram_scanner_last_string_free (); @@ -2343,8 +2359,8 @@ yyreduce: break; -/* Line 1270 of yacc.c. */ -#line 2348 "parse-gram.c" +/* Line 1269 of yacc.c. */ +#line 2364 "parse-gram.c" default: break; } YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc); @@ -2495,9 +2511,6 @@ yyerrlab1: YY_STACK_PRINT (yyss, yyssp); } - if (yyn == YYFINAL) - YYACCEPT; - *++yyvsp = yylval; yyerror_range[1] = yylloc; @@ -2538,7 +2551,7 @@ yyexhaustedlab: #endif yyreturn: - if (yychar != YYEOF && yychar != YYEMPTY) + if (yychar != YYEMPTY) yydestruct ("Cleanup: discarding lookahead", yytoken, &yylval, &yylloc); /* Do not reclaim the symbols of the rule which action triggered @@ -2564,8 +2577,8 @@ yyreturn: } -/* Line 1490 of yacc.c. */ -#line 544 "parse-gram.y" +/* Line 1486 of yacc.c. */ +#line 552 "parse-gram.y" diff --git a/src/parse-gram.h b/src/parse-gram.h index a55d4e7e..dda76d4a 100644 --- a/src/parse-gram.h +++ b/src/parse-gram.h @@ -159,7 +159,7 @@ #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED typedef union YYSTYPE -{/* Line 1539 of yacc.c. */ +{/* Line 1535 of yacc.c. */ #line 97 "parse-gram.y" symbol *symbol; @@ -170,7 +170,7 @@ typedef union YYSTYPE uniqstr uniqstr; unsigned char character; } -/* Line 1539 of yacc.c. */ +/* Line 1535 of yacc.c. */ #line 175 "parse-gram.h" YYSTYPE; # define YYSTYPE_IS_TRIVIAL 1 diff --git a/src/parse-gram.y b/src/parse-gram.y index bc012637..49f85777 100644 --- a/src/parse-gram.y +++ b/src/parse-gram.y @@ -261,6 +261,10 @@ grammar_declaration: { grammar_start_symbol_set ($2, @2); } +| "%destructor" "{...}" + { + default_destructor_set (translate_symbol_action ($2, @2), @2); + } | "%destructor" "{...}" symbols.1 { symbol_list *list; @@ -277,6 +281,10 @@ grammar_declaration: symbol_printer_set (list->sym, action, @2); symbol_list_free ($3); } +| "%printer" "{...}" + { + default_printer_set (translate_symbol_action ($2, @2), @2); + } | "%default-prec" { default_prec = true; diff --git a/src/reader.c b/src/reader.c index 928be1b8..5084618f 100644 --- a/src/reader.c +++ b/src/reader.c @@ -259,7 +259,7 @@ grammar_current_rule_begin (symbol *lhs, location loc) static bool symbol_should_be_used (symbol_list const *s) { - return (s->sym->destructor + return (symbol_destructor_get (s->sym) || (s->midrule && s->midrule->used)); } diff --git a/src/symtab.c b/src/symtab.c index b71a3fba..821ad56c 100644 --- a/src/symtab.c +++ b/src/symtab.c @@ -41,6 +41,15 @@ symbol *accept = NULL; symbol *startsymbol = NULL; location startsymbol_location; +/*-----------------------------------. +| Default %destructor and %printer. | +`-----------------------------------*/ + +static const char *default_destructor = NULL; +static location default_destructor_location; +static const char *default_printer = NULL; +static location default_printer_location; + /*---------------------------------. | Create a new symbol, named TAG. | `---------------------------------*/ @@ -147,6 +156,33 @@ symbol_destructor_set (symbol *sym, const char *destructor, location loc) } } +/*---------------------------------------. +| Get the computed %destructor for SYM. | +`---------------------------------------*/ + +const char * +symbol_destructor_get (symbol *sym) +{ + /* Token 0 cannot have a %destructor unless the user renames it. */ + if (UNIQSTR_EQ (sym->tag, uniqstr_new ("$end"))) + return NULL; + + if (sym->destructor != NULL) + return sym->destructor; + return default_destructor; +} + +/*---------------------------------------------------------------. +| Get the grammar location of the %destructor computed for SYM. | +`---------------------------------------------------------------*/ + +location +symbol_destructor_location_get (symbol *sym) +{ + if (sym->destructor != NULL) + return sym->destructor_location; + return default_destructor_location; +} /*---------------------------------------------------------------. | Set the PRINTER associated with SYM. Do nothing if passed 0. | @@ -164,6 +200,34 @@ symbol_printer_set (symbol *sym, const char *printer, location loc) } } +/*------------------------------------. +| Get the computed %printer for SYM. | +`------------------------------------*/ + +const char * +symbol_printer_get (symbol *sym) +{ + /* Token 0 cannot have a %printer unless the user renames it. */ + if (UNIQSTR_EQ (sym->tag, uniqstr_new ("$end"))) + return NULL; + + if (sym->printer != NULL) + return sym->printer; + return default_printer; +} + +/*------------------------------------------------------------. +| Get the grammar location of the %printer computed for SYM. | +`------------------------------------------------------------*/ + +location +symbol_printer_location_get (symbol *sym) +{ + if (sym->printer != NULL) + return sym->printer_location; + return default_printer_location; +} + /*-----------------------------------------------------------------. | Set the PRECEDENCE associated with SYM. Does nothing if invoked | @@ -666,3 +730,32 @@ symbols_pack (void) _("the start symbol %s is a token"), startsymbol->tag); } + + +/*-----------------------------------. +| Set default %destructor/%printer. | +`-----------------------------------*/ + +void +default_destructor_set (const char *destructor, location loc) +{ + if (default_destructor != NULL) + { + complain_at (loc, _("redeclaration for default %%destructor")); + complain_at (default_destructor_location, _("previous declaration")); + } + default_destructor = destructor; + default_destructor_location = loc; +} + +void +default_printer_set (const char *printer, location loc) +{ + if (default_printer != NULL) + { + complain_at (loc, _("redeclaration for default %%printer")); + complain_at (default_printer_location, _("previous declaration")); + } + default_printer = printer; + default_printer_location = loc; +} diff --git a/src/symtab.h b/src/symtab.h index 00770a6e..1a3ada4c 100644 --- a/src/symtab.h +++ b/src/symtab.h @@ -61,16 +61,35 @@ struct symbol /** The location of its first occurrence. */ location location; - /** Its %type and associated printer and destructor. */ + /** Its \c \%type. */ uniqstr type_name; + /** Its \c \%type's location. */ location type_location; - /** Does not own the memory. */ + /** Any \c \%destructor declared specifically for this symbol. + + Access this field only through symbol's interface functions. For + example, if symbol::destructor = NULL, the default + \c \%destructor or a per-type \c \%destructor might be appropriate, and + \c symbol_destructor_get will compute the correct one. */ const char *destructor; + + /** The location of \c symbol::destructor. + + Access this field only through symbol's interface functions. + \sa symbol::destructor */ location destructor_location; - /** Printer. */ + /** Any \c \%printer declared specifically for this symbol. + + Access this field only through symbol's interface functions. + \sa symbol::destructor */ const char *printer; + + /** The location of \c symbol::printer. + + Access this field only through symbol's interface functions. + \sa symbol::destructor */ location printer_location; symbol_number number; @@ -125,9 +144,25 @@ void symbol_type_set (symbol *sym, uniqstr type_name, location loc); /** Set the \c destructor associated with \c sym. */ void symbol_destructor_set (symbol *sym, const char *destructor, location loc); +/** Get the computed \c \%destructor for \c sym, or \c NULL if none. */ +const char *symbol_destructor_get (symbol *sym); + +/** Get the grammar location of the computed \c \%destructor for \c sym. + + \pre symbol_destructor_get (sym) != NULL */ +location symbol_destructor_location_get (symbol *sym); + /** Set the \c printer associated with \c sym. */ void symbol_printer_set (symbol *sym, const char *printer, location loc); +/** Get the computed \c \%printer for \c sym, or \c NULL if none. */ +const char *symbol_printer_get (symbol *sym); + +/** Get the grammar location of the computed \c \%printer for \c sym. + + \pre symbol_printer_get (sym) != NULL */ +location symbol_printer_location_get (symbol *sym); + /* Set the \c precedence associated with \c sym. Ensure that \a symbol is a terminal. @@ -155,7 +190,7 @@ extern symbol *accept; /** The user start symbol. */ extern symbol *startsymbol; -/** The location of the \c %start declaration. */ +/** The location of the \c \%start declaration. */ extern location startsymbol_location; @@ -181,4 +216,15 @@ void symbols_check_defined (void); #token_translations. */ void symbols_pack (void); + +/*-----------------------------------. +| Default %destructor and %printer. | +`-----------------------------------*/ + +/** Set the default \c \%destructor. */ +void default_destructor_set (const char *destructor, location loc); + +/** Set the default \c \%printer. */ +void default_printer_set (const char *printer, location loc); + #endif /* !SYMTAB_H_ */ diff --git a/tests/actions.at b/tests/actions.at index 2a2bce75..d84c7ee8 100644 --- a/tests/actions.at +++ b/tests/actions.at @@ -575,3 +575,202 @@ AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union]) AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser]) AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union]) + + + + + +## --------------------------------- ## +## Default %printer and %destructor. ## +## --------------------------------- ## + +# Check that the right %printer and %destructor are called, that they're not +# called for $end, and that $$ and @$ work correctly. + +AT_SETUP([Default %printer and %destructor]) + +AT_DATA_GRAMMAR([[input.y]], +[[%error-verbose +%debug +%locations +%initial-action { + @$.first_line = @$.last_line = 1; + @$.first_column = @$.last_column = 1; +} + +%{ +# include +# include + static void yyerror (const char *msg); + static int yylex (void); +# define USE(SYM) +%} + +%printer { + fprintf (yyoutput, "Default printer for '%c' @ %d", $$, @$.first_column); +} +%destructor { + fprintf (stdout, "Default destructor for '%c' @ %d.\n", $$, @$.first_column); +} + +%printer { + fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column); +} 'b' 'c' +%destructor { + fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column); +} 'b' 'c' + +%% + +start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ; + +%% + +static int +yylex (void) +{ + static const char *input = "abcd"; + static int column = 1; + yylval = *input++; + yylloc.first_line = yylloc.last_line = 1; + yylloc.first_column = yylloc.last_column = column++; + return yylval; +} + +static void +yyerror (const char *msg) +{ + fprintf (stderr, "%s\n", msg); +} + +int +main (void) +{ + yydebug = 1; + return yyparse (); +} +]]) + +AT_CHECK([bison -o input.c input.y]) +AT_COMPILE([input]) +AT_PARSER_CHECK([./input], 1, +[[Default destructor for 'd' @ 4. +'b'/'c' destructor for 'c' @ 3. +'b'/'c' destructor for 'b' @ 2. +Default destructor for 'a' @ 1. +]], +[[Starting parse +Entering state 0 +Reading a token: Next token is token 'a' (1.1-1.1: Default printer for 'a' @ 1) +Shifting token 'a' (1.1-1.1: Default printer for 'a' @ 1) +Entering state 1 +Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2) +Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2) +Entering state 3 +Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3) +Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3) +Entering state 5 +Reading a token: Next token is token 'd' (1.4-1.4: Default printer for 'd' @ 4) +Shifting token 'd' (1.4-1.4: Default printer for 'd' @ 4) +Entering state 6 +Reading a token: Now at end of input. +syntax error, unexpected $end, expecting 'e' +Error: popping token 'd' (1.4-1.4: Default printer for 'd' @ 4) +Stack now 0 1 3 5 +Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3) +Stack now 0 1 3 +Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2) +Stack now 0 1 +Error: popping token 'a' (1.1-1.1: Default printer for 'a' @ 1) +Stack now 0 +Cleanup: discarding lookahead token $end (1.5-1.5: ) +Stack now 0 +]]) + +AT_CLEANUP + + + + + +## ------------------------------------------------------------- ## +## Default %printer and %destructor for user-declared end token. ## +## ------------------------------------------------------------- ## + +AT_SETUP([Default %printer and %destructor for user-declared end token]) + +AT_DATA_GRAMMAR([[input.y]], +[[%error-verbose +%debug +%locations +%initial-action { + @$.first_line = @$.last_line = 1; + @$.first_column = @$.last_column = 1; +} + +%{ +# include +# include + static void yyerror (const char *msg); + static int yylex (void); +# define USE(SYM) +%} + +%token END 0 +%printer { + fprintf (yyoutput, "Default printer for '%c' @ %d", $$, @$.first_column); +} +%destructor { + fprintf (stdout, "Default destructor for '%c' @ %d.\n", $$, @$.first_column); +} + +%% + +start: { $$ = 'S'; } ; + +%% + +static int +yylex (void) +{ + yylval = 'E'; + yylloc.first_line = yylloc.last_line = 1; + yylloc.first_column = yylloc.last_column = 1; + return 0; +} + +static void +yyerror (const char *msg) +{ + fprintf (stderr, "%s\n", msg); +} + +int +main (void) +{ + yydebug = 1; + return yyparse (); +} +]]) + +AT_CHECK([bison -o input.c input.y]) +AT_COMPILE([input]) +AT_PARSER_CHECK([./input], 0, +[[Default destructor for 'E' @ 1. +Default destructor for 'S' @ 1. +]], +[[Starting parse +Entering state 0 +Reducing stack by rule 1 (line 37): +-> $$ = nterm start (1.1-1.1: Default printer for 'S' @ 1) +Stack now 0 +Entering state 1 +Reading a token: Now at end of input. +Shifting token END (1.1-1.1: Default printer for 'E' @ 1) +Entering state 2 +Stack now 0 1 2 +Cleanup: popping token END (1.1-1.1: Default printer for 'E' @ 1) +Cleanup: popping nterm start (1.1-1.1: Default printer for 'S' @ 1) +]]) + +AT_CLEANUP diff --git a/tests/input.at b/tests/input.at index 223428f1..e50d99d8 100644 --- a/tests/input.at +++ b/tests/input.at @@ -168,6 +168,65 @@ AT_CHECK_UNUSED_VALUES([1]) AT_CLEANUP +## --------------------------------------------- ## +## Default %printer and %destructor redeclared. ## +## --------------------------------------------- ## + +AT_SETUP([Default %printer and %destructor redeclared]) + +AT_DATA([[input.y]], +[[%destructor { destroy ($$); } +%printer { destroy ($$); } + +%destructor { destroy ($$); } +%printer { destroy ($$); } + +%% + +start: ; + +%destructor { destroy ($$); }; +%printer { destroy ($$); }; +]]) + +AT_CHECK([bison input.y], [1], [], +[[input.y:4.13-29: redeclaration for default %destructor +input.y:1.13-29: previous declaration +input.y:5.10-26: redeclaration for default %printer +input.y:2.10-26: previous declaration +input.y:11.13-29: redeclaration for default %destructor +input.y:4.13-29: previous declaration +input.y:12.10-26: redeclaration for default %printer +input.y:5.10-26: previous declaration +]]) + +AT_CLEANUP + + +## ---------------------------------------- ## +## Unused values with default %destructor. ## +## ---------------------------------------- ## + +AT_SETUP([Unused values with default %destructor]) + +AT_DATA([[input.y]], +[[%destructor { destroy ($$); } + +%% + +start: end end { $1; } ; +end: { } ; +]]) + +AT_CHECK([bison input.y], [0], [], +[[input.y:5.8-22: warning: unset value: $$ +input.y:5.8-22: warning: unused value: $2 +input.y:6.6-8: warning: unset value: $$ +]]) + +AT_CLEANUP + + ## ---------------------- ## ## Incompatible Aliases. ## ## ---------------------- ##