From e8f7155d98c22579c871c293a0daa5f7752e7c2b Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Tue, 29 Jan 2013 16:27:04 +0100 Subject: [PATCH] grammar: record used associativity and print useless ones Record which symbol associativity is used, and display useless ones. * src/symtab.h, src/symtab.c (register_assoc, print_assoc_warnings): New * src/symtab.c (init_assoc, is_assoc_used): New * src/main.c: Use print_assoc_warnings * src/conflicts.c: Use register_assoc * tests/conflicts.at (Useless associativity warning): New. Due to the new warning, many tests had to be updated. * tests/conflicts.at tests/existing.at tests/regression.at: Add the associativity warning in the expected results. * tests/java.at: Fix the java calculator's grammar to remove a useless associativity. * doc/bison.texi (mfcalc example): Fix associativity to remove warning. --- doc/bison.texi | 2 +- src/conflicts.c | 3 + src/main.c | 2 + src/symtab.c | 64 +++++++++++++++++++++ src/symtab.h | 8 +++ tests/conflicts.at | 56 ++++++++++++++++++ tests/existing.at | 137 ++++++++++++++++++++++++++++++++++++++++++-- tests/java.at | 6 +- tests/regression.at | 6 +- 9 files changed, 275 insertions(+), 9 deletions(-) diff --git a/doc/bison.texi b/doc/bison.texi index 8d7bb437..d2d3da38 100644 --- a/doc/bison.texi +++ b/doc/bison.texi @@ -2411,7 +2411,7 @@ Here are the C and Bison declarations for the multi-function calculator. %type exp @group -%right '=' +%precedence '=' %left '-' '+' %left '*' '/' %precedence NEG /* negation--unary minus */ diff --git a/src/conflicts.c b/src/conflicts.c index 5744ac36..80ea74a8 100644 --- a/src/conflicts.c +++ b/src/conflicts.c @@ -303,16 +303,19 @@ resolve_sr_conflict (state *s, int ruleno, symbol **errors, int *nerrs) break; case right_assoc: + register_assoc (i, redrule->prec->number); log_resolution (redrule, i, right_resolution); flush_reduce (lookahead_tokens, i); break; case left_assoc: + register_assoc (i, redrule->prec->number); log_resolution (redrule, i, left_resolution); flush_shift (s, i); break; case non_assoc: + register_assoc (i, redrule->prec->number); log_resolution (redrule, i, nonassoc_resolution); flush_shift (s, i); flush_reduce (lookahead_tokens, i); diff --git a/src/main.c b/src/main.c index 22703d1d..eb7dffab 100644 --- a/src/main.c +++ b/src/main.c @@ -146,6 +146,8 @@ main (int argc, char *argv[]) print_precedence_warnings (); + print_assoc_warnings (); + /* Output file names. */ compute_output_file_names (); diff --git a/src/symtab.c b/src/symtab.c index 3028df9f..e48c11e5 100644 --- a/src/symtab.c +++ b/src/symtab.c @@ -52,6 +52,12 @@ location startsymbol_location; static symgraph **prec_nodes; +/*-----------------------------------. +| Store which associativity is used. | +`-----------------------------------*/ + +bool *used_assoc = NULL; + /*---------------------------------. | Create a new symbol, named TAG. | `---------------------------------*/ @@ -1073,3 +1079,61 @@ print_precedence_warnings (void) _("useless precedence for %s"), s->tag); } } + +/*---------------------------------------. +| Initialize association tracking table. | +`---------------------------------------*/ + +static void +init_assoc (void) +{ + graphid i; + used_assoc = xcalloc(nsyms, sizeof(*used_assoc)); + for (i = 0; i < nsyms; ++i) + used_assoc[i] = false; +} + +/*------------------------------------------------------------------. +| Test if the associativity for the symbols is defined and useless. | +`------------------------------------------------------------------*/ + +static inline bool +is_assoc_useless (symbol *s) +{ + return s + && s->assoc != undef_assoc + && s->assoc != precedence_assoc + && !used_assoc[s->number]; +} + +/*-------------------------------. +| Register a used associativity. | +`-------------------------------*/ + +void +register_assoc (graphid i, graphid j) +{ + if (!used_assoc) + init_assoc (); + used_assoc[i] = true; + used_assoc[j] = true; +} + +/*------------------------------------------------------. +| Print a warning for each unused symbol associativity. | +`------------------------------------------------------*/ + +void +print_assoc_warnings (void) +{ + graphid i; + if (!used_assoc) + init_assoc (); + for (i = 0; i < nsyms; ++i) + { + symbol *s = symbols[i]; + if (is_assoc_useless (s)) + complain (&s->location, Wother, + _("useless associativity for %s"), s->tag); + } +} diff --git a/src/symtab.h b/src/symtab.h index fee54999..33374fb3 100644 --- a/src/symtab.h +++ b/src/symtab.h @@ -270,6 +270,14 @@ void register_precedence (graphid first, graphid snd); void print_precedence_warnings (void); +/*----------------------. +| Symbol associativity | +`----------------------*/ + +void register_assoc (int i, int j); + +void print_assoc_warnings (void); + /*-----------------. | Semantic types. | `-----------------*/ diff --git a/tests/conflicts.at b/tests/conflicts.at index d2fb298d..92e4ae87 100644 --- a/tests/conflicts.at +++ b/tests/conflicts.at @@ -17,6 +17,38 @@ AT_BANNER([[Conflicts.]]) +## ------------------------------ ## +## Useless associativity warning. ## +## ------------------------------ ## + +AT_SETUP([Useless associativity warning]) + +AT_DATA([[input.y]], +[[%token T +%left A B +%right C +%nonassoc D +%precedence E + +%% +e: T A T + | T B T + | T C T + | T D T + | T E T +; +]]) + +AT_BISON_CHECK([input.y], 0, [], +[[input.y:5.13: warning: useless precedence for E [-Wother] +input.y:2.7: warning: useless associativity for A [-Wother] +input.y:2.9: warning: useless associativity for B [-Wother] +input.y:3.8: warning: useless associativity for C [-Wother] +input.y:4.11: warning: useless associativity for D [-Wother] +]]) + +AT_CLEANUP + ## ------------------------ ## ## Token declaration order. ## @@ -85,6 +117,19 @@ input.y:24.17: warning: useless precedence for T [-Wother] input.y:24.19: warning: useless precedence for U [-Wother] input.y:25.13: warning: useless precedence for V [-Wother] input.y:25.15: warning: useless precedence for W [-Wother] +input.y:18.8: warning: useless associativity for E [-Wother] +input.y:18.10: warning: useless associativity for F [-Wother] +input.y:18.12: warning: useless associativity for G [-Wother] +input.y:19.8: warning: useless associativity for H [-Wother] +input.y:19.10: warning: useless associativity for I [-Wother] +input.y:20.8: warning: useless associativity for J [-Wother] +input.y:21.8: warning: useless associativity for K [-Wother] +input.y:22.8: warning: useless associativity for L [-Wother] +input.y:22.10: warning: useless associativity for M [-Wother] +input.y:22.12: warning: useless associativity for N [-Wother] +input.y:23.11: warning: useless associativity for O [-Wother] +input.y:23.13: warning: useless associativity for P [-Wother] +input.y:23.15: warning: useless associativity for Q [-Wother] ]]) AT_COMPILE([input]) @@ -133,6 +178,15 @@ AT_BISON_CHECK([-fcaret -o input.c input.y], 0, [], [[input.y:2.13: warning: useless precedence for Z [-Wother] %precedence Z ^ +input.y:5.7: warning: useless associativity for W [-Wother] + %left W + ^ +input.y:6.8: warning: useless associativity for V [-Wother] + %right V + ^ +input.y:7.11: warning: useless associativity for U [-Wother] + %nonassoc U + ^ ]]) AT_CLEANUP @@ -1144,6 +1198,8 @@ e: e '+' e AT_BISON_CHECK([-o input.c input.y], 0, [], [[input.y: warning: 4 shift/reduce conflicts [-Wconflicts-sr] +input.y:1.7-9: warning: useless associativity for '+' [-Wother] +input.y:2.7-9: warning: useless associativity for '*' [-Wother] ]]) AT_CLEANUP diff --git a/tests/existing.at b/tests/existing.at index 0209c35a..6118e7dc 100644 --- a/tests/existing.at +++ b/tests/existing.at @@ -427,8 +427,44 @@ dnl don't like even `print $!4;'. dnl BISON-STDERR [AT_COND_CASE([[canonical LR]], -[[input.y: warning: 265 shift/reduce conflicts [-Wconflicts-sr]]], -[[input.y: warning: 65 shift/reduce conflicts [-Wconflicts-sr]]])[ +[[input.y: warning: 265 shift/reduce conflicts [-Wconflicts-sr] +input.y:19.8-16: warning: useless associativity for FUNC_CALL [-Wother] +input.y:21.8-14: warning: useless associativity for YNUMBER [-Wother] +input.y:21.16-22: warning: useless associativity for YSTRING [-Wother] +input.y:22.14-22: warning: useless associativity for APPEND_OP [-Wother] +input.y:23.8-15: warning: useless associativity for ASSIGNOP [-Wother] +input.y:23.33-41: warning: useless associativity for CONCAT_OP [-Wother] +input.y:27.8-18: warning: useless associativity for LEX_GETLINE [-Wother] +input.y:28.8-13: warning: useless associativity for LEX_IN [-Wother] +input.y:29.23-31: warning: useless associativity for INCREMENT [-Wother] +input.y:29.33-41: warning: useless associativity for DECREMENT [-Wother] +input.y:30.8-18: warning: useless associativity for LEX_BUILTIN [-Wother] +input.y:30.20-29: warning: useless associativity for LEX_LENGTH [-Wother] +input.y:40.11-13: warning: useless associativity for ',' [-Wother] +input.y:47.8-10: warning: useless associativity for '!' [-Wother] +input.y:47.12-16: warning: useless associativity for UNARY [-Wother] +input.y:50.7-9: warning: useless associativity for '$' [-Wother] +input.y:51.7-9: warning: useless associativity for '(' [-Wother] +input.y:51.11-13: warning: useless associativity for ')' [-Wother]]], +[[input.y: warning: 65 shift/reduce conflicts [-Wconflicts-sr] +input.y:19.8-16: warning: useless associativity for FUNC_CALL [-Wother] +input.y:21.8-14: warning: useless associativity for YNUMBER [-Wother] +input.y:21.16-22: warning: useless associativity for YSTRING [-Wother] +input.y:22.14-22: warning: useless associativity for APPEND_OP [-Wother] +input.y:23.8-15: warning: useless associativity for ASSIGNOP [-Wother] +input.y:23.33-41: warning: useless associativity for CONCAT_OP [-Wother] +input.y:27.8-18: warning: useless associativity for LEX_GETLINE [-Wother] +input.y:28.8-13: warning: useless associativity for LEX_IN [-Wother] +input.y:29.23-31: warning: useless associativity for INCREMENT [-Wother] +input.y:29.33-41: warning: useless associativity for DECREMENT [-Wother] +input.y:30.8-18: warning: useless associativity for LEX_BUILTIN [-Wother] +input.y:30.20-29: warning: useless associativity for LEX_LENGTH [-Wother] +input.y:40.11-13: warning: useless associativity for ',' [-Wother] +input.y:47.8-10: warning: useless associativity for '!' [-Wother] +input.y:47.12-16: warning: useless associativity for UNARY [-Wother] +input.y:50.7-9: warning: useless associativity for '$' [-Wother] +input.y:51.7-9: warning: useless associativity for '(' [-Wother] +input.y:51.11-13: warning: useless associativity for ')' [-Wother]]])[ ]], dnl LAST-STATE @@ -1369,9 +1405,21 @@ dnl INPUT dnl BISON-STDERR [AT_COND_CASE([[canonical LR]], [[input.y: warning: 1876 shift/reduce conflicts [-Wconflicts-sr] -input.y: warning: 144 reduce/reduce conflicts [-Wconflicts-rr]]], +input.y: warning: 144 reduce/reduce conflicts [-Wconflicts-rr] +input.y:32.9-12: warning: useless associativity for HQUA [-Wother] +input.y:53.8-14: warning: useless associativity for HASSIGN [-Wother] +input.y:54.9-15: warning: useless associativity for HORELSE [-Wother] +input.y:55.9-16: warning: useless associativity for HANDTHEN [-Wother] +input.y:61.9-12: warning: useless associativity for HNOT [-Wother] +input.y:68.7-11: warning: useless associativity for UNEAR [-Wother]]], [[input.y: warning: 78 shift/reduce conflicts [-Wconflicts-sr] -input.y: warning: 10 reduce/reduce conflicts [-Wconflicts-rr]]])[ +input.y: warning: 10 reduce/reduce conflicts [-Wconflicts-rr] +input.y:32.9-12: warning: useless associativity for HQUA [-Wother] +input.y:53.8-14: warning: useless associativity for HASSIGN [-Wother] +input.y:54.9-15: warning: useless associativity for HORELSE [-Wother] +input.y:55.9-16: warning: useless associativity for HANDTHEN [-Wother] +input.y:61.9-12: warning: useless associativity for HNOT [-Wother] +input.y:68.7-11: warning: useless associativity for UNEAR [-Wother]]])[ ]], dnl LAST-STATE @@ -1954,6 +2002,87 @@ dnl without being followed by "of".) dnl BISON-STDERR [[input.y:471.11-48: warning: rule useless in parser due to conflicts: path: ORDINAL LAST object_type relative_path [-Wother] +input.y:19.8-12: warning: useless associativity for LABEL [-Wother] +input.y:20.8-15: warning: useless associativity for VARIABLE [-Wother] +input.y:21.8-13: warning: useless associativity for NUMBER [-Wother] +input.y:22.8-11: warning: useless associativity for TEXT [-Wother] +input.y:25.8-14: warning: useless associativity for ORDINAL [-Wother] +input.y:30.8-11: warning: useless associativity for LAST [-Wother] +input.y:31.8-9: warning: useless associativity for UP [-Wother] +input.y:32.8-11: warning: useless associativity for DOWN [-Wother] +input.y:35.8-10: warning: useless associativity for BOX [-Wother] +input.y:36.8-13: warning: useless associativity for CIRCLE [-Wother] +input.y:37.8-14: warning: useless associativity for ELLIPSE [-Wother] +input.y:38.8-10: warning: useless associativity for ARC [-Wother] +input.y:39.8-11: warning: useless associativity for LINE [-Wother] +input.y:40.8-12: warning: useless associativity for ARROW [-Wother] +input.y:42.8-13: warning: useless associativity for SPLINE [-Wother] +input.y:43.8-13: warning: useless associativity for HEIGHT [-Wother] +input.y:44.8-13: warning: useless associativity for RADIUS [-Wother] +input.y:45.8-12: warning: useless associativity for WIDTH [-Wother] +input.y:46.8-15: warning: useless associativity for DIAMETER [-Wother] +input.y:47.8-11: warning: useless associativity for FROM [-Wother] +input.y:48.8-9: warning: useless associativity for TO [-Wother] +input.y:49.8-9: warning: useless associativity for AT [-Wother] +input.y:53.8-12: warning: useless associativity for SOLID [-Wother] +input.y:54.8-13: warning: useless associativity for DOTTED [-Wother] +input.y:55.8-13: warning: useless associativity for DASHED [-Wother] +input.y:56.8-11: warning: useless associativity for CHOP [-Wother] +input.y:59.8-12: warning: useless associativity for LJUST [-Wother] +input.y:60.8-12: warning: useless associativity for RJUST [-Wother] +input.y:61.8-12: warning: useless associativity for ABOVE [-Wother] +input.y:62.8-12: warning: useless associativity for BELOW [-Wother] +input.y:63.8-9: warning: useless associativity for OF [-Wother] +input.y:66.8-14: warning: useless associativity for BETWEEN [-Wother] +input.y:67.8-10: warning: useless associativity for AND [-Wother] +input.y:68.8-11: warning: useless associativity for HERE [-Wother] +input.y:69.8-12: warning: useless associativity for DOT_N [-Wother] +input.y:70.8-12: warning: useless associativity for DOT_E [-Wother] +input.y:71.8-12: warning: useless associativity for DOT_W [-Wother] +input.y:72.8-12: warning: useless associativity for DOT_S [-Wother] +input.y:73.8-13: warning: useless associativity for DOT_NE [-Wother] +input.y:74.8-13: warning: useless associativity for DOT_SE [-Wother] +input.y:75.8-13: warning: useless associativity for DOT_NW [-Wother] +input.y:76.8-13: warning: useless associativity for DOT_SW [-Wother] +input.y:77.8-12: warning: useless associativity for DOT_C [-Wother] +input.y:78.8-16: warning: useless associativity for DOT_START [-Wother] +input.y:79.8-14: warning: useless associativity for DOT_END [-Wother] +input.y:85.8-10: warning: useless associativity for SIN [-Wother] +input.y:86.8-10: warning: useless associativity for COS [-Wother] +input.y:87.8-12: warning: useless associativity for ATAN2 [-Wother] +input.y:88.8-10: warning: useless associativity for LOG [-Wother] +input.y:89.8-10: warning: useless associativity for EXP [-Wother] +input.y:90.8-11: warning: useless associativity for SQRT [-Wother] +input.y:91.8-12: warning: useless associativity for K_MAX [-Wother] +input.y:92.8-12: warning: useless associativity for K_MIN [-Wother] +input.y:93.8-10: warning: useless associativity for INT [-Wother] +input.y:94.8-11: warning: useless associativity for RAND [-Wother] +input.y:95.8-12: warning: useless associativity for SRAND [-Wother] +input.y:98.8-10: warning: useless associativity for TOP [-Wother] +input.y:99.8-13: warning: useless associativity for BOTTOM [-Wother] +input.y:100.8-12: warning: useless associativity for UPPER [-Wother] +input.y:101.8-12: warning: useless associativity for LOWER [-Wother] +input.y:116.8-18: warning: useless associativity for LEFT_CORNER [-Wother] +input.y:117.8-19: warning: useless associativity for RIGHT_CORNER [-Wother] +input.y:118.8-12: warning: useless associativity for NORTH [-Wother] +input.y:119.8-12: warning: useless associativity for SOUTH [-Wother] +input.y:120.8-11: warning: useless associativity for EAST [-Wother] +input.y:121.8-11: warning: useless associativity for WEST [-Wother] +input.y:122.8-13: warning: useless associativity for CENTER [-Wother] +input.y:123.8-10: warning: useless associativity for END [-Wother] +input.y:124.8-12: warning: useless associativity for START [-Wother] +input.y:127.8-11: warning: useless associativity for PLOT [-Wother] +input.y:128.8-16: warning: useless associativity for THICKNESS [-Wother] +input.y:129.8-11: warning: useless associativity for FILL [-Wother] +input.y:130.8-14: warning: useless associativity for COLORED [-Wother] +input.y:131.8-15: warning: useless associativity for OUTLINED [-Wother] +input.y:134.8-14: warning: useless associativity for SPRINTF [-Wother] +input.y:137.7-9: warning: useless associativity for '.' [-Wother] +input.y:156.23-25: warning: useless associativity for '(' [-Wother] +input.y:157.20-22: warning: useless associativity for '`' [-Wother] +input.y:159.48-50: warning: useless associativity for '@<:@' [-Wother] +input.y:170.7-9: warning: useless associativity for ',' [-Wother] +input.y:181.8-10: warning: useless associativity for '!' [-Wother] ]], dnl LAST-STATE diff --git a/tests/java.at b/tests/java.at index 1fff3628..c5aae2f5 100644 --- a/tests/java.at +++ b/tests/java.at @@ -54,11 +54,11 @@ AT_DATA([Calc.y], %token NUM "number" %type exp -%nonassoc '=' /* comparison */ +%nonassoc '=' /* comparison */ %left '-' '+' %left '*' '/' -%left NEG /* negation--unary minus */ -%right '^' /* exponentiation */ +%precedence NEG /* negation--unary minus */ +%right '^' /* exponentiation */ /* Grammar follows */ %% diff --git a/tests/regression.at b/tests/regression.at index 02840bb3..7a5ed3e9 100644 --- a/tests/regression.at +++ b/tests/regression.at @@ -377,7 +377,10 @@ exp: ; %% ]]) -AT_BISON_CHECK([-v -o input.c input.y]) +AT_BISON_CHECK([-v -o input.c input.y], 0, [], +[[input.y:1.29-32: warning: useless associativity for "||" [-Wother] +input.y:2.29-32: warning: useless associativity for "<=" [-Wother] +]]) AT_CLEANUP @@ -1147,6 +1150,7 @@ AT_BISON_OPTION_POPDEFS AT_BISON_CHECK([[-o input.c input.y]], [[0]],, [[input.y:24.5-19: warning: rule useless in parser due to conflicts: start: start [-Wother] input.y:28.5-19: warning: rule useless in parser due to conflicts: sr_conflict: TK2 "tok alias" [-Wother] +input.y:18.7-9: warning: useless associativity for TK1 [-Wother] ]]) AT_COMPILE([[input]]) AT_PARSER_CHECK([[./input]]) -- 2.45.2