glr2.cc: put create_state_set_index in unnamed namespace
[bison.git] / tests / named-refs.at
blob07ce433f61db2cd87835fe2608eb9fdeaaddb78b
1 # Named references test.                           -*- Autotest -*-
3 # Copyright (C) 2009-2015, 2018-2021 Free Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
18 # FIXME: Duplication with calc.at.
19 AT_BANNER([[Named references tests.]])
21 # FIXME: large duplication with calc.at.
22 AT_SETUP([Tutorial calculator])
23 AT_BISON_OPTION_PUSHDEFS
24 AT_DATA_GRAMMAR([test.y],
27 #include <assert.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <ctype.h>
32 typedef int semantic_value;
33 FILE *input;
34 static semantic_value global_result = 0;
35 static int global_count = 0;
36 static int power (int base, int exponent);
37 ]AT_YYERROR_DECLARE[
38 ]AT_YYLEX_DECLARE[
41 %union
43   semantic_value ival;
46 %token CALC_EOF 0 "end of input"
47 %token <ival> NUM "number"
48 %type  <ival> exp
50 %nonassoc '='   /* comparison          */
51 %left '-' '+'
52 %left '*' '/'
53 %precedence NEG /* negation--unary minus */
54 %right '^'      /* exponentiation        */
57 input:
58   line
59 | input line         {}
62 line:
63   '\n'
64 | exp '\n'           {}
67 exp:
68   NUM                { $$ = $NUM; }
69 | exp[l] '=' exp[r]
70   {
71     if ($l != $r)
72       fprintf (stderr, "calc: error: %d != %d\n", $l, $r);
73    $$ = $l;
74   }
75 | exp[x] '+' { $<ival>$ = $x; } [l] exp[r]  { $$ = $<ival>l + $r;    }
76 | exp[l] '-' exp[r]  { $$ = $l - $r;        }
77 | exp[l] '*' exp[r]  { $$ = $l * $r;        }
78 | exp[l] '/' exp[r]  { $$ = $l / $r;        }
79 | '-' exp  %prec NEG { $$ = -$2;            }
80 | exp[l] '^' exp[r]  { $$ = power ($l, $r); }
81 | '(' exp[e] ')'     { $$ = $e;           }
82 | '(' error ')'      { $$ = 1111; yyerrok;  }
83 | '!'                { $$ = 0; YYERROR;     }
84 | '-' error          { $$ = 0; YYERROR;     }
87 ]AT_YYERROR_DEFINE[
88 static int get_char (void)
90   return getc (input);
93 static void unget_char (int c)
95   ungetc (c, input);
98 static int read_signed_integer (void)
100   int c = get_char ();
101   int sign = 1;
102   int n = 0;
103   if (c == '-')
104     {
105       c = get_char ();
106       sign = -1;
107     }
108   while (isdigit (c))
109     {
110       n = 10 * n + (c - '0');
111       c = get_char ();
112     }
113   unget_char ( c);
114   return sign * n;
117 static int
118 yylex (void)
120   int c;
121   /* Skip white space.  */
122   while ((c = get_char ()) == ' ' || c == '\t') {}
124   /* process numbers   */
125   if (c == '.' || isdigit (c))
126     {
127       unget_char ( c);
128       (yylval).ival = read_signed_integer ();
129       return NUM;
130     }
132   /* Return end-of-file.  */
133   if (c == EOF)
134     return CALC_EOF;
136   /* Return single chars. */
137   return c;
140 static int power (int base, int exponent)
142   int res = 1;
143   assert (0 <= exponent);
144   for (/* Niente */; exponent; --exponent)
145     res *= base;
146   return res;
149 int main (int argc, const char **argv)
151   semantic_value result = 0;
152   int count = 0;
153   int status;
154   if (argc == 2)
155     input = fopen (argv[1], "r");
156   else
157     input = stdin;
158   if (!input)
159     {
160       perror (argv[1]);
161       return 3;
162     }
163   status = yyparse ();
164   fclose (input);
165   assert (global_result == result); (void) global_result; (void) result;
166   assert (global_count == count);   (void) global_count;  (void) count;
167   return status;
171 AT_DATA([input.txt],
173 1 + 2 * 3 = 7
174 1 + 2 * -3 = -5
175 -1^2 = -1
176 (-1)^2 = 1
177 ---1 = -1
178 1 - 2 - 3 = -4
179 1 - (2 - 3) = 2
180 2^2^3 = 256
181 (2^2)^3 = 64
184 AT_FULL_COMPILE([[test]])
185 AT_PARSER_CHECK([test input.txt], 0, [], [stderr])
186 AT_BISON_OPTION_POPDEFS
187 AT_CLEANUP
191 ## ------------------------------------ ##
192 ## Undefined and ambiguous references.  ##
193 ## ------------------------------------ ##
196 AT_SETUP([Undefined and ambiguous references])
197 AT_BISON_OPTION_PUSHDEFS
198 AT_DATA_GRAMMAR([test.y],
201 static int power (int base, int exponent);
202 ]AT_YYERROR_DECLARE[
203 ]AT_YYLEX_DECLARE[
206 %union
208   int ival;
211 %token CALC_EOF 0 "end of input"
212 %token <ival> NUM "number"
213 %type  <ival> exp
215 %nonassoc '='   /* comparison          */
216 %left '-' '+'
217 %left '*' '/'
218 %precedence NEG /* negation--unary minus */
219 %right '^'      /* exponentiation        */
222 input:
223   line
224 | input line         {}
227 line:
228   '\n'
229 | exp '\n'           {}
232 exp:
233   NUM { $$ = $NUM; }
234 | exp[l] '=' exp[r]
235   {
236     if ($l != $r)
237       fprintf (stderr, "calc: error: %d != %d\n", $l, $r);
238    $$ = $l;
239   }
240 | exp[x] '+' { $<ival>$ = $x; } [l] exp[r] { $$ = $<ival>lo9 + $r; }
241 | exp[x] '-' { $<ival>$ = $x; } [l] exp[r] { $$ = $<ival>exp - $r; }
242 | exp[x] '*' { $<ival>$ = $x; } [l] exp[r] { $$ = $l * $r; }
243 | exp[l] '/' exp[r]  { $$ = $l / $r;        }
244 | '-' exp  %prec NEG { $$ = -$2;            }
245 | exp[l] '^' exp[r]  { $$ = power ($l, $r12); }
246 | '(' exp ')'        { $$ = $expo;           }
247 | '(' error ')'      { $$ = 1111; yyerrok;  }
248 | '!'                { $$ = 0; YYERROR;     }
249 | '-' error          { $$ = 0; YYERROR;     }
254 AT_BISON_CHECK([-fcaret -o test.c test.y], 1, [],
255 [[test.y:52.51-60: error: invalid reference: '$<ival>lo9'
256    52 | | exp[x] '+' { $<ival>$ = $x; } [l] exp[r] { $$ = $<ival>lo9 + $r; }
257       |                                                   ^~~~~~~~~~
258 test.y:52.3-68: note: symbol not found in production: lo9
259    52 | | exp[x] '+' { $<ival>$ = $x; } [l] exp[r] { $$ = $<ival>lo9 + $r; }
260       |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
261 test.y:53.51-60: warning: misleading reference: '$<ival>exp' [-Wother]
262    53 | | exp[x] '-' { $<ival>$ = $x; } [l] exp[r] { $$ = $<ival>exp - $r; }
263       |                                                   ^~~~~~~~~~
264 test.y:44.1-3: note: refers to: $exp at $$
265    44 | exp:
266       | ^~~
267 test.y:53.7: note: possibly meant: $x, hiding $exp at $1
268    53 | | exp[x] '-' { $<ival>$ = $x; } [l] exp[r] { $$ = $<ival>exp - $r; }
269       |       ^
270 test.y:53.41: note: possibly meant: $r, hiding $exp at $4
271    53 | | exp[x] '-' { $<ival>$ = $x; } [l] exp[r] { $$ = $<ival>exp - $r; }
272       |                                         ^
273 test.y:54.51-52: error: $l of 'exp' has no declared type
274    54 | | exp[x] '*' { $<ival>$ = $x; } [l] exp[r] { $$ = $l * $r; }
275       |                                                   ^~
276 test.y:57.40-43: error: invalid reference: '$r12'
277    57 | | exp[l] '^' exp[r]  { $$ = power ($l, $r12); }
278       |                                        ^~~~
279 test.y:57.3-47: note: symbol not found in production: r12
280    57 | | exp[l] '^' exp[r]  { $$ = power ($l, $r12); }
281       |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
282 test.y:58.29-33: error: invalid reference: '$expo'
283    58 | | '(' exp ')'        { $$ = $expo;           }
284       |                             ^~~~~
285 test.y:58.3-46: note: symbol not found in production: expo
286    58 | | '(' exp ')'        { $$ = $expo;           }
287       |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
289 AT_BISON_OPTION_POPDEFS
290 AT_CLEANUP
293 ## ----------------------- ##
294 ## Misleading references.  ##
295 ## ----------------------- ##
297 AT_SETUP([Misleading references])
298 AT_BISON_OPTION_PUSHDEFS
299 AT_DATA_GRAMMAR([test.y],
302 start: foo foo.bar { $foo.bar; }
303 foo: '1'
304 foo.bar: '2'
306 AT_BISON_CHECK([-o test.c test.y], 0, [],
307 [[test.y:11.22-29: warning: misleading reference: '$foo.bar' [-Wother]
308 test.y:11.8-10: note: refers to: $foo at $1
309 test.y:11.12-18: note: possibly meant: $[foo.bar] at $2
311 AT_BISON_OPTION_POPDEFS
312 AT_CLEANUP
314 #######################################################################
316 AT_SETUP([Many kinds of errors])
317 AT_BISON_OPTION_PUSHDEFS
318 AT_DATA_GRAMMAR([test.y],
320 %token IDENT
321 %token NUMBER
322 %token ASSIGNOP
323 %token IF
324 %token IF1
325 %token THEN
326 %token ELSE
327 %token FI
328 %token WHILE
329 %token DO
330 %token OD
331 %start program
333 if_stmt1: IF expr[cond] THEN stmt[then] ELSE stmt.list[else] FI
334           { $if_stmt1 = new IfStmt($cond1, $then.f1, $else); };
335 if_stmt2: IF expr[cond] THEN stmt[then] FI
336           { $if_stmt2 = new IfStmt($cond, $stmt.field, 0); };
337 if_stmt3: IF expr[cond] THEN stmt.list FI
338           { $if_stmt3 = new IfStmt($cond, $stmt.list, 0); };
339 if_stmt4: IF expr[cond] THEN stmt[xyz] ELSE stmt[xyz] FI
340           { $if_stmt4 = new IfStmt($cond, $xyz, $cond); };
341 if_stmt5: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
342           { $if_stmt5 = new IfStmt($cond, $stmt.list, $else); };
343 if_stmt6: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
344           { $if_stmt6 = new IfStmt($cond, $stmt.list.field, $else); };
345 if_stmt7: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
346           { $if_stmt7 = new IfStmt($cond, $[stmt.list].field, $else); };
347 if_stmt8: IF expr[cond] THEN stmt.list[then.1] ELSE stmt.list[else] FI
348           { $if_stmt8 = new IfStmt($cond, $then.1, $else); };
349 if_stmt9: IF expr[cond] THEN stmt.list[then.1] ELSE stmt.list[else] FI
350           { $if_stmt9 = new IfStmt($cond, $then.1.field, $else); };
351 if_stmt10: IF expr[cond] THEN stmt[stmt.x] FI
352           { $if_stmt10 = new IfStmt($cond, $stmt.x, 0); };
353 if-stmt-a: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
354           { $if-stmt-a = new IfStmt($cond, $then, $else); };
355 if-stmt-b: IF expr[cond] THEN if-stmt-a[then-a] ELSE stmt.list[else] FI
356           { $[if-stmt-b] = new IfStmt($cond, $then-a.f, $else); };
357 program: stmt.list;
358 stmt.list:  stmt ';' stmt.list { $3->insert($stmt); $$ = $3; }
359         |   stmt ';' { SL = new StmtList();  SL->insert($1); $$ = SL; }
360         ;
361 stmt:  assign_stmt { $$ = $1; }
362     |  if_stmt { $$ = $1; }
363     |  if_stmt1 { $$ = $1; }
364     |  while_stmt { $$ = $1; }
365     ;
366 assign_stmt: IDENT ASSIGNOP expr
367        { $$ = new AssignStmt(string($1),$3); };
368 if_stmt: IF expr[cond] THEN stmt.list FI
369        { $if_stmt = new IfStmt($cond, $[stmt.list], 0); };
370 while_stmt[res]: WHILE expr DO stmt.list OD
371        { $res = new WhileStmt($[expr], $[stmt.list]); };
372 expr: expr '+' term   { $$ = new Plus($1,$3); }
373     | expr '-' term   { $$ = new Minus($1,$3); }
374     | term            { $$ = $1; }
375     ;
376 term: term '*' factor   { $$ = new Times($1,$3); }
377     | factor            { $$ = $1; }
378     ;
379 factor:     '(' expr ')'  { $$ = $2; }
380     |       NUMBER { $$ = new Number($1); }
381     |       IDENT { $$ = new Ident(string($1)); }
382     ;
384 AT_BISON_CHECK([-o test.c test.y], 1, [],
385 [[test.y:24.36-41: error: invalid reference: '$cond1'
386 test.y:23.11-24.62: note: symbol not found in production: cond1
387 test.y:26.43-53: error: invalid reference: '$stmt.field'
388 test.y:25.11-26.60: note: symbol not found in production: stmt
389 test.y:25.35-38: note: possibly meant: $then.field, hiding $stmt.field at $4
390 test.y:28.43-52: error: invalid reference: '$stmt.list'
391 test.y:27.11-28.59: note: symbol not found in production: stmt
392 test.y:27.30-38: note: possibly meant: $[stmt.list] at $4
393 test.y:30.43-46: error: ambiguous reference: '$xyz'
394 test.y:29.35-37: note: refers to: $xyz at $4
395 test.y:29.50-52: note: refers to: $xyz at $6
396 test.y:32.43-52: error: invalid reference: '$stmt.list'
397 test.y:31.11-32.63: note: symbol not found in production: stmt
398 test.y:31.40-43: note: possibly meant: $then, hiding $[stmt.list] at $4
399 test.y:31.61-64: note: possibly meant: $else, hiding $[stmt.list] at $6
400 test.y:34.43-58: error: invalid reference: '$stmt.list.field'
401 test.y:33.11-34.69: note: symbol not found in production: stmt
402 test.y:33.40-43: note: possibly meant: $then.field, hiding $[stmt.list].field at $4
403 test.y:33.61-64: note: possibly meant: $else.field, hiding $[stmt.list].field at $6
404 test.y:36.43-54: error: invalid reference: '$[stmt.list]'
405 test.y:35.11-36.71: note: symbol not found in production: stmt.list
406 test.y:35.40-43: note: possibly meant: $then, hiding $[stmt.list] at $4
407 test.y:35.61-64: note: possibly meant: $else, hiding $[stmt.list] at $6
408 test.y:38.43-49: error: invalid reference: '$then.1'
409 test.y:37.11-38.60: note: symbol not found in production: then
410 test.y:37.40-45: note: possibly meant: $[then.1] at $4
411 test.y:40.43-55: error: invalid reference: '$then.1.field'
412 test.y:39.11-40.66: note: symbol not found in production: then
413 test.y:39.40-45: note: possibly meant: $[then.1].field at $4
414 test.y:42.44-50: error: invalid reference: '$stmt.x'
415 test.y:41.12-42.57: note: symbol not found in production: stmt
416 test.y:41.36-41: note: possibly meant: $[stmt.x].x, hiding $stmt.x at $4
417 test.y:41.36-41: note: possibly meant: $[stmt.x] at $4
418 test.y:44.13-22: error: invalid reference: '$if-stmt-a'
419 test.y:43.12-44.59: note: symbol not found in production: if
420 test.y:43.1-9: note: possibly meant: $[if-stmt-a] at $$
421 test.y:46.46-54: error: invalid reference: '$then-a.f'
422 test.y:45.12-46.65: note: symbol not found in production: then
423 test.y:45.41-46: note: possibly meant: $[then-a].f at $4
426 AT_BISON_CHECK([-fcaret -o test.c test.y], 1, [],
427 [[test.y:24.36-41: error: invalid reference: '$cond1'
428    24 |           { $if_stmt1 = new IfStmt($cond1, $then.f1, $else); };
429       |                                    ^~~~~~
430 test.y:23.11-24.62: note: symbol not found in production: cond1
431    23 | if_stmt1: IF expr[cond] THEN stmt[then] ELSE stmt.list[else] FI
432       |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
433 test.y:26.43-53: error: invalid reference: '$stmt.field'
434    26 |           { $if_stmt2 = new IfStmt($cond, $stmt.field, 0); };
435       |                                           ^~~~~~~~~~~
436 test.y:25.11-26.60: note: symbol not found in production: stmt
437    25 | if_stmt2: IF expr[cond] THEN stmt[then] FI
438       |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
439 test.y:25.35-38: note: possibly meant: $then.field, hiding $stmt.field at $4
440    25 | if_stmt2: IF expr[cond] THEN stmt[then] FI
441       |                                   ^~~~
442 test.y:28.43-52: error: invalid reference: '$stmt.list'
443    28 |           { $if_stmt3 = new IfStmt($cond, $stmt.list, 0); };
444       |                                           ^~~~~~~~~~
445 test.y:27.11-28.59: note: symbol not found in production: stmt
446    27 | if_stmt3: IF expr[cond] THEN stmt.list FI
447       |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
448 test.y:27.30-38: note: possibly meant: $[stmt.list] at $4
449    27 | if_stmt3: IF expr[cond] THEN stmt.list FI
450       |                              ^~~~~~~~~
451 test.y:30.43-46: error: ambiguous reference: '$xyz'
452    30 |           { $if_stmt4 = new IfStmt($cond, $xyz, $cond); };
453       |                                           ^~~~
454 test.y:29.35-37: note: refers to: $xyz at $4
455    29 | if_stmt4: IF expr[cond] THEN stmt[xyz] ELSE stmt[xyz] FI
456       |                                   ^~~
457 test.y:29.50-52: note: refers to: $xyz at $6
458    29 | if_stmt4: IF expr[cond] THEN stmt[xyz] ELSE stmt[xyz] FI
459       |                                                  ^~~
460 test.y:32.43-52: error: invalid reference: '$stmt.list'
461    32 |           { $if_stmt5 = new IfStmt($cond, $stmt.list, $else); };
462       |                                           ^~~~~~~~~~
463 test.y:31.11-32.63: note: symbol not found in production: stmt
464    31 | if_stmt5: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
465       |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
466 test.y:31.40-43: note: possibly meant: $then, hiding $[stmt.list] at $4
467    31 | if_stmt5: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
468       |                                        ^~~~
469 test.y:31.61-64: note: possibly meant: $else, hiding $[stmt.list] at $6
470    31 | if_stmt5: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
471       |                                                             ^~~~
472 test.y:34.43-58: error: invalid reference: '$stmt.list.field'
473    34 |           { $if_stmt6 = new IfStmt($cond, $stmt.list.field, $else); };
474       |                                           ^~~~~~~~~~~~~~~~
475 test.y:33.11-34.69: note: symbol not found in production: stmt
476    33 | if_stmt6: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
477       |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
478 test.y:33.40-43: note: possibly meant: $then.field, hiding $[stmt.list].field at $4
479    33 | if_stmt6: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
480       |                                        ^~~~
481 test.y:33.61-64: note: possibly meant: $else.field, hiding $[stmt.list].field at $6
482    33 | if_stmt6: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
483       |                                                             ^~~~
484 test.y:36.43-54: error: invalid reference: '$[stmt.list]'
485    36 |           { $if_stmt7 = new IfStmt($cond, $[stmt.list].field, $else); };
486       |                                           ^~~~~~~~~~~~
487 test.y:35.11-36.71: note: symbol not found in production: stmt.list
488    35 | if_stmt7: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
489       |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
490 test.y:35.40-43: note: possibly meant: $then, hiding $[stmt.list] at $4
491    35 | if_stmt7: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
492       |                                        ^~~~
493 test.y:35.61-64: note: possibly meant: $else, hiding $[stmt.list] at $6
494    35 | if_stmt7: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
495       |                                                             ^~~~
496 test.y:38.43-49: error: invalid reference: '$then.1'
497    38 |           { $if_stmt8 = new IfStmt($cond, $then.1, $else); };
498       |                                           ^~~~~~~
499 test.y:37.11-38.60: note: symbol not found in production: then
500    37 | if_stmt8: IF expr[cond] THEN stmt.list[then.1] ELSE stmt.list[else] FI
501       |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
502 test.y:37.40-45: note: possibly meant: $[then.1] at $4
503    37 | if_stmt8: IF expr[cond] THEN stmt.list[then.1] ELSE stmt.list[else] FI
504       |                                        ^~~~~~
505 test.y:40.43-55: error: invalid reference: '$then.1.field'
506    40 |           { $if_stmt9 = new IfStmt($cond, $then.1.field, $else); };
507       |                                           ^~~~~~~~~~~~~
508 test.y:39.11-40.66: note: symbol not found in production: then
509    39 | if_stmt9: IF expr[cond] THEN stmt.list[then.1] ELSE stmt.list[else] FI
510       |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
511 test.y:39.40-45: note: possibly meant: $[then.1].field at $4
512    39 | if_stmt9: IF expr[cond] THEN stmt.list[then.1] ELSE stmt.list[else] FI
513       |                                        ^~~~~~
514 test.y:42.44-50: error: invalid reference: '$stmt.x'
515    42 |           { $if_stmt10 = new IfStmt($cond, $stmt.x, 0); };
516       |                                            ^~~~~~~
517 test.y:41.12-42.57: note: symbol not found in production: stmt
518    41 | if_stmt10: IF expr[cond] THEN stmt[stmt.x] FI
519       |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
520 test.y:41.36-41: note: possibly meant: $[stmt.x].x, hiding $stmt.x at $4
521    41 | if_stmt10: IF expr[cond] THEN stmt[stmt.x] FI
522       |                                    ^~~~~~
523 test.y:41.36-41: note: possibly meant: $[stmt.x] at $4
524    41 | if_stmt10: IF expr[cond] THEN stmt[stmt.x] FI
525       |                                    ^~~~~~
526 test.y:44.13-22: error: invalid reference: '$if-stmt-a'
527    44 |           { $if-stmt-a = new IfStmt($cond, $then, $else); };
528       |             ^~~~~~~~~~
529 test.y:43.12-44.59: note: symbol not found in production: if
530    43 | if-stmt-a: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
531       |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
532 test.y:43.1-9: note: possibly meant: $[if-stmt-a] at $$
533    43 | if-stmt-a: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
534       | ^~~~~~~~~
535 test.y:46.46-54: error: invalid reference: '$then-a.f'
536    46 |           { $[if-stmt-b] = new IfStmt($cond, $then-a.f, $else); };
537       |                                              ^~~~~~~~~
538 test.y:45.12-46.65: note: symbol not found in production: then
539    45 | if-stmt-b: IF expr[cond] THEN if-stmt-a[then-a] ELSE stmt.list[else] FI
540       |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
541 test.y:45.41-46: note: possibly meant: $[then-a].f at $4
542    45 | if-stmt-b: IF expr[cond] THEN if-stmt-a[then-a] ELSE stmt.list[else] FI
543       |                                         ^~~~~~
546 AT_BISON_OPTION_POPDEFS
547 AT_CLEANUP
549 #######################################################################
551 AT_SETUP([Missing identifiers in brackets])
552 AT_BISON_OPTION_PUSHDEFS
553 AT_DATA_GRAMMAR([test.y],
556 start: foo[] bar
557   { s = $foo; }
559 AT_BISON_CHECK([-o test.c test.y], 1, [],
560 [[test.y:11.12: error: an identifier expected
562 AT_BISON_OPTION_POPDEFS
563 AT_CLEANUP
565 #######################################################################
567 AT_SETUP([Redundant words in brackets])
568 AT_BISON_OPTION_PUSHDEFS
569 AT_DATA_GRAMMAR([test.y],
572 start: foo[ a d ] bar
573   { s = $foo; }
575 AT_BISON_CHECK([-o test.c test.y], 1, [],
576 [[test.y:11.15: error: unexpected identifier in bracketed name: 'd'
578 AT_BISON_OPTION_POPDEFS
579 AT_CLEANUP
581 #######################################################################
583 AT_SETUP([Comments in brackets])
584 AT_BISON_OPTION_PUSHDEFS
585 AT_DATA_GRAMMAR([test.y],
588 start: foo[/* comment */] bar
589   { s = $foo; }
591 AT_BISON_CHECK([-o test.c test.y], 1, [],
592 [[test.y:11.25: error: an identifier expected
594 AT_BISON_OPTION_POPDEFS
595 AT_CLEANUP
597 #######################################################################
599 AT_SETUP([Stray symbols in brackets])
600 AT_BISON_OPTION_PUSHDEFS
601 AT_DATA_GRAMMAR([test.y],
604 start: foo[ % /* aaa */ *&-.+\000\001\002\377 ] bar
605   { s = $foo; }
607 AT_PERL_REQUIRE([[-pi -e 's/\\(\d{3})/chr(oct($1))/ge' test.y]])
608 AT_BISON_CHECK([-o test.c test.y], 1, [],
609 [[test.y:11.13: error: invalid character in bracketed name: '%'
610 test.y:11.25-27: error: invalid characters in bracketed name: '*&-'
611 test.y:11.29-30: error: invalid characters in bracketed name: '+\0\001\002\377'
613 AT_BISON_OPTION_POPDEFS
614 AT_CLEANUP
616 #######################################################################
618 AT_SETUP([Redundant words in LHS brackets])
619 AT_BISON_OPTION_PUSHDEFS
620 AT_DATA_GRAMMAR([test.y],
623 start[a s]: foo;
625 AT_BISON_CHECK([-o test.c test.y], 1, [],
626 [[test.y:11.9: error: unexpected identifier in bracketed name: 's'
628 AT_BISON_OPTION_POPDEFS
629 AT_CLEANUP
631 #######################################################################
633 # Bison used to free twice the named ref for "a", since a single copy
634 # was used in two rules.
635 AT_SETUP([Factored LHS])
636 AT_BISON_OPTION_PUSHDEFS
637 AT_DATA_GRAMMAR([test.y],
640 start[a]: "foo" | "bar";
642 AT_BISON_CHECK([-o test.c test.y])
643 AT_BISON_OPTION_POPDEFS
644 AT_CLEANUP
646 #######################################################################
648 AT_SETUP([Unresolved references])
649 AT_BISON_OPTION_PUSHDEFS
650 AT_DATA_GRAMMAR([test.y],
653 stat:
654   sym_a sym_b { func($sym.field); }
655 | sym_a sym_b { func($<aa>sym.field); }
656 | sym_a sym_b { func($[sym.field]); }
657 | sym_a sym_b { func($<aa>[sym.field]); }
658 | sym_a sym_b { func($sym); }
659 | sym_a sym_b { func($<aa>sym); }
660 | sym_a sym_b { func($[sym]); } sym_a sym_b { func($<aa>[sym]); }
663 stat1:
664   sym_a sym_b { func($sym-field); }
665 | sym_a sym_b { func($<aa>sym-field); }
666 | sym_a sym_b { func($[sym-field]); }
667 | sym_a sym_b { func($<aa>[sym-field]); }
668 | sym_a sym_b { func($sym); }
669 | sym_a sym_b { func($<aa>sym); }
670 | sym_a sym_b { func($[sym]); } sym_a sym_b { func($<aa>[sym]); }
673 sym_a: 'a';
674 sym_b: 'b';
676 AT_BISON_CHECK([-o test.c test.y], 1, [],
677 [[test.y:12.22-31: error: invalid reference: '$sym.field'
678 test.y:12.3-35: note: symbol not found in production: sym
679 test.y:13.22-35: error: invalid reference: '$<aa>sym.field'
680 test.y:13.3-39: note: symbol not found in production: sym
681 test.y:14.22-33: error: invalid reference: '$[sym.field]'
682 test.y:14.3-37: note: symbol not found in production: sym.field
683 test.y:15.22-37: error: invalid reference: '$<aa>[sym.field]'
684 test.y:15.3-41: note: symbol not found in production: sym.field
685 test.y:16.22-25: error: invalid reference: '$sym'
686 test.y:16.3-29: note: symbol not found in production: sym
687 test.y:17.22-29: error: invalid reference: '$<aa>sym'
688 test.y:17.3-33: note: symbol not found in production: sym
689 test.y:18.22-27: error: invalid reference: '$[sym]'
690 test.y:18.3-65: note: symbol not found in production before $3: sym
691 test.y:18.52-61: error: invalid reference: '$<aa>[sym]'
692 test.y:18.3-65: note: symbol not found in production: sym
693 test.y:22.22-31: error: invalid reference: '$sym-field'
694 test.y:22.3-35: note: symbol not found in production: sym
695 test.y:23.22-35: error: invalid reference: '$<aa>sym-field'
696 test.y:23.3-39: note: symbol not found in production: sym
697 test.y:24.22-33: error: invalid reference: '$[sym-field]'
698 test.y:24.3-37: note: symbol not found in production: sym-field
699 test.y:25.22-37: error: invalid reference: '$<aa>[sym-field]'
700 test.y:25.3-41: note: symbol not found in production: sym-field
701 test.y:26.22-25: error: invalid reference: '$sym'
702 test.y:26.3-29: note: symbol not found in production: sym
703 test.y:27.22-29: error: invalid reference: '$<aa>sym'
704 test.y:27.3-33: note: symbol not found in production: sym
705 test.y:28.22-27: error: invalid reference: '$[sym]'
706 test.y:28.3-65: note: symbol not found in production before $3: sym
707 test.y:28.52-61: error: invalid reference: '$<aa>[sym]'
708 test.y:28.3-65: note: symbol not found in production: sym
710 AT_BISON_OPTION_POPDEFS
711 AT_CLEANUP
713 #######################################################################
715 AT_SETUP([[$ or @ followed by . or -]])
716 AT_DATA([[test.y]],
719 start:
720   .field { $.field; }
721 | 'a'    { @.field; }
723 .field: ;
725 AT_BISON_CHECK([[test.y]], [[1]], [],
726 [[test.y:4.12-18: error: invalid reference: '$.field'
727 test.y:4.13: note: syntax error after '$', expecting integer, letter, '_', '@<:@', or '$'
728 test.y:4.3-8: note: possibly meant: $[.field] at $1
729 test.y:5.12-18: error: invalid reference: '@.field'
730 test.y:5.13: note: syntax error after '@', expecting integer, letter, '_', '@<:@', or '$'
732 AT_DATA([[test.y]],
735 start:
736   'a' { $-field; }
737 | 'b' { @-field; }
740 AT_BISON_CHECK([[test.y]], [[0]], [],
741 [[test.y:4.9: warning: stray '$' [-Wother]
742 test.y:5.9: warning: stray '@' [-Wother]
744 AT_CLEANUP