summaryrefslogtreecommitdiff
path: root/tests/named-refs.at
blob: 99131b33ff03ef10e44db455f696c9e2ad201f87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
# Named references test.                           -*- Autotest -*-

# Copyright (C) 2009-2015, 2018-2022 Free Software Foundation, Inc.

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

# FIXME: Duplication with calc.at.
AT_BANNER([[Named references tests.]])

# FIXME: large duplication with calc.at.
AT_SETUP([Tutorial calculator])
AT_BISON_OPTION_PUSHDEFS
AT_DATA_GRAMMAR([test.y],
[[
%{
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef int semantic_value;
FILE *input;
static semantic_value global_result = 0;
static int global_count = 0;
static int power (int base, int exponent);
]AT_YYERROR_DECLARE[
]AT_YYLEX_DECLARE[
%}

%union
{
  semantic_value ival;
};

%token CALC_EOF 0 "end of input"
%token <ival> NUM "number"
%type  <ival> exp

%nonassoc '='   /* comparison          */
%left '-' '+'
%left '*' '/'
%precedence NEG /* negation--unary minus */
%right '^'      /* exponentiation        */

%%
input:
  line
| input line         {}
;

line:
  '\n'
| exp '\n'           {}
;

exp:
  NUM                { $$ = $NUM; }
| exp[l] '=' exp[r]
  {
    if ($l != $r)
      fprintf (stderr, "calc: error: %d != %d\n", $l, $r);
   $$ = $l;
  }
| exp[x] '+' { $<ival>$ = $x; } [l] exp[r]  { $$ = $<ival>l + $r;    }
| exp[l] '-' exp[r]  { $$ = $l - $r;        }
| exp[l] '*' exp[r]  { $$ = $l * $r;        }
| exp[l] '/' exp[r]  { $$ = $l / $r;        }
| '-' exp  %prec NEG { $$ = -$2;            }
| exp[l] '^' exp[r]  { $$ = power ($l, $r); }
| '(' exp[e] ')'     { $$ = $e;           }
| '(' error ')'      { $$ = 1111; yyerrok;  }
| '!'                { $$ = 0; YYERROR;     }
| '-' error          { $$ = 0; YYERROR;     }
;
%%
]AT_YYERROR_DEFINE[
static int get_char (void)
{
  return getc (input);
}

static void unget_char (int c)
{
  ungetc (c, input);
}

static int read_signed_integer (void)
{
  int c = get_char ();
  int sign = 1;
  int n = 0;
  if (c == '-')
    {
      c = get_char ();
      sign = -1;
    }
  while (isdigit (c))
    {
      n = 10 * n + (c - '0');
      c = get_char ();
    }
  unget_char ( c);
  return sign * n;
}

static int
yylex (void)
{
  int c;
  /* Skip white space.  */
  while ((c = get_char ()) == ' ' || c == '\t') {}

  /* process numbers   */
  if (c == '.' || isdigit (c))
    {
      unget_char ( c);
      (yylval).ival = read_signed_integer ();
      return NUM;
    }

  /* Return end-of-file.  */
  if (c == EOF)
    return CALC_EOF;

  /* Return single chars. */
  return c;
}

static int power (int base, int exponent)
{
  int res = 1;
  assert (0 <= exponent);
  for (/* Niente */; exponent; --exponent)
    res *= base;
  return res;
}

int main (int argc, const char **argv)
{
  semantic_value result = 0;
  int count = 0;
  int status;
  if (argc == 2)
    input = fopen (argv[1], "r");
  else
    input = stdin;
  if (!input)
    {
      perror (argv[1]);
      return 3;
    }
  status = yyparse ();
  fclose (input);
  assert (global_result == result); (void) global_result; (void) result;
  assert (global_count == count);   (void) global_count;  (void) count;
  return status;
}
]])

AT_DATA([input.txt],
[[
1 + 2 * 3 = 7
1 + 2 * -3 = -5
-1^2 = -1
(-1)^2 = 1
---1 = -1
1 - 2 - 3 = -4
1 - (2 - 3) = 2
2^2^3 = 256
(2^2)^3 = 64
]])

AT_FULL_COMPILE([[test]])
AT_PARSER_CHECK([test input.txt], 0, [], [stderr])
AT_BISON_OPTION_POPDEFS
AT_CLEANUP



## ------------------------------------ ##
## Undefined and ambiguous references.  ##
## ------------------------------------ ##


AT_SETUP([Undefined and ambiguous references])
AT_BISON_OPTION_PUSHDEFS
AT_DATA_GRAMMAR([test.y],
[[
%{
static int power (int base, int exponent);
]AT_YYERROR_DECLARE[
]AT_YYLEX_DECLARE[
%}

%union
{
  int ival;
};

%token CALC_EOF 0 "end of input"
%token <ival> NUM "number"
%type  <ival> exp

%nonassoc '='   /* comparison          */
%left '-' '+'
%left '*' '/'
%precedence NEG /* negation--unary minus */
%right '^'      /* exponentiation        */

%%
input:
  line
| input line         {}
;

line:
  '\n'
| exp '\n'           {}
;

exp:
  NUM { $$ = $NUM; }
| exp[l] '=' exp[r]
  {
    if ($l != $r)
      fprintf (stderr, "calc: error: %d != %d\n", $l, $r);
   $$ = $l;
  }
| exp[x] '+' { $<ival>$ = $x; } [l] exp[r] { $$ = $<ival>lo9 + $r; }
| exp[x] '-' { $<ival>$ = $x; } [l] exp[r] { $$ = $<ival>exp - $r; }
| exp[x] '*' { $<ival>$ = $x; } [l] exp[r] { $$ = $l * $r; }
| exp[l] '/' exp[r]  { $$ = $l / $r;        }
| '-' exp  %prec NEG { $$ = -$2;            }
| exp[l] '^' exp[r]  { $$ = power ($l, $r12); }
| '(' exp ')'        { $$ = $expo;           }
| '(' error ')'      { $$ = 1111; yyerrok;  }
| '!'                { $$ = 0; YYERROR;     }
| '-' error          { $$ = 0; YYERROR;     }
;
%%
]])

AT_BISON_CHECK([-fcaret -o test.c test.y], 1, [],
[[test.y:52.51-60: error: invalid reference: '$<ival>lo9'
   52 | | exp[x] '+' { $<ival>$ = $x; } [l] exp[r] { $$ = $<ival>lo9 + $r; }
      |                                                   ^~~~~~~~~~
test.y:52.3-68: note: symbol not found in production: lo9
   52 | | exp[x] '+' { $<ival>$ = $x; } [l] exp[r] { $$ = $<ival>lo9 + $r; }
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.y:53.51-60: warning: misleading reference: '$<ival>exp' [-Wother]
   53 | | exp[x] '-' { $<ival>$ = $x; } [l] exp[r] { $$ = $<ival>exp - $r; }
      |                                                   ^~~~~~~~~~
test.y:44.1-3: note: refers to: $exp at $$
   44 | exp:
      | ^~~
test.y:53.7: note: possibly meant: $x, hiding $exp at $1
   53 | | exp[x] '-' { $<ival>$ = $x; } [l] exp[r] { $$ = $<ival>exp - $r; }
      |       ^
test.y:53.41: note: possibly meant: $r, hiding $exp at $4
   53 | | exp[x] '-' { $<ival>$ = $x; } [l] exp[r] { $$ = $<ival>exp - $r; }
      |                                         ^
test.y:54.51-52: error: $l of 'exp' has no declared type
   54 | | exp[x] '*' { $<ival>$ = $x; } [l] exp[r] { $$ = $l * $r; }
      |                                                   ^~
test.y:57.40-43: error: invalid reference: '$r12'
   57 | | exp[l] '^' exp[r]  { $$ = power ($l, $r12); }
      |                                        ^~~~
test.y:57.3-47: note: symbol not found in production: r12
   57 | | exp[l] '^' exp[r]  { $$ = power ($l, $r12); }
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.y:58.29-33: error: invalid reference: '$expo'
   58 | | '(' exp ')'        { $$ = $expo;           }
      |                             ^~~~~
test.y:58.3-46: note: symbol not found in production: expo
   58 | | '(' exp ')'        { $$ = $expo;           }
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
]])
AT_BISON_OPTION_POPDEFS
AT_CLEANUP


## ----------------------- ##
## Misleading references.  ##
## ----------------------- ##

AT_SETUP([Misleading references])
AT_BISON_OPTION_PUSHDEFS
AT_DATA_GRAMMAR([test.y],
[[
%%
start: foo foo.bar { $foo.bar; }
foo: '1'
foo.bar: '2'
]])
AT_BISON_CHECK([-o test.c test.y], 0, [],
[[test.y:11.22-29: warning: misleading reference: '$foo.bar' [-Wother]
test.y:11.8-10: note: refers to: $foo at $1
test.y:11.12-18: note: possibly meant: $[foo.bar] at $2
]])
AT_BISON_OPTION_POPDEFS
AT_CLEANUP

#######################################################################

AT_SETUP([Many kinds of errors])
AT_BISON_OPTION_PUSHDEFS
AT_DATA_GRAMMAR([test.y],
[[
%token IDENT
%token NUMBER
%token ASSIGNOP
%token IF
%token IF1
%token THEN
%token ELSE
%token FI
%token WHILE
%token DO
%token OD
%start program
%%
if_stmt1: IF expr[cond] THEN stmt[then] ELSE stmt.list[else] FI
          { $if_stmt1 = new IfStmt($cond1, $then.f1, $else); };
if_stmt2: IF expr[cond] THEN stmt[then] FI
          { $if_stmt2 = new IfStmt($cond, $stmt.field, 0); };
if_stmt3: IF expr[cond] THEN stmt.list FI
          { $if_stmt3 = new IfStmt($cond, $stmt.list, 0); };
if_stmt4: IF expr[cond] THEN stmt[xyz] ELSE stmt[xyz] FI
          { $if_stmt4 = new IfStmt($cond, $xyz, $cond); };
if_stmt5: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
          { $if_stmt5 = new IfStmt($cond, $stmt.list, $else); };
if_stmt6: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
          { $if_stmt6 = new IfStmt($cond, $stmt.list.field, $else); };
if_stmt7: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
          { $if_stmt7 = new IfStmt($cond, $[stmt.list].field, $else); };
if_stmt8: IF expr[cond] THEN stmt.list[then.1] ELSE stmt.list[else] FI
          { $if_stmt8 = new IfStmt($cond, $then.1, $else); };
if_stmt9: IF expr[cond] THEN stmt.list[then.1] ELSE stmt.list[else] FI
          { $if_stmt9 = new IfStmt($cond, $then.1.field, $else); };
if_stmt10: IF expr[cond] THEN stmt[stmt.x] FI
          { $if_stmt10 = new IfStmt($cond, $stmt.x, 0); };
if-stmt-a: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
          { $if-stmt-a = new IfStmt($cond, $then, $else); };
if-stmt-b: IF expr[cond] THEN if-stmt-a[then-a] ELSE stmt.list[else] FI
          { $[if-stmt-b] = new IfStmt($cond, $then-a.f, $else); };
program: stmt.list;
stmt.list:  stmt ';' stmt.list { $3->insert($stmt); $$ = $3; }
        |   stmt ';' { SL = new StmtList();  SL->insert($1); $$ = SL; }
        ;
stmt:  assign_stmt { $$ = $1; }
    |  if_stmt { $$ = $1; }
    |  if_stmt1 { $$ = $1; }
    |  while_stmt { $$ = $1; }
    ;
assign_stmt: IDENT ASSIGNOP expr
       { $$ = new AssignStmt(string($1),$3); };
if_stmt: IF expr[cond] THEN stmt.list FI
       { $if_stmt = new IfStmt($cond, $[stmt.list], 0); };
while_stmt[res]: WHILE expr DO stmt.list OD
       { $res = new WhileStmt($[expr], $[stmt.list]); };
expr: expr '+' term   { $$ = new Plus($1,$3); }
    | expr '-' term   { $$ = new Minus($1,$3); }
    | term            { $$ = $1; }
    ;
term: term '*' factor   { $$ = new Times($1,$3); }
    | factor            { $$ = $1; }
    ;
factor:     '(' expr ')'  { $$ = $2; }
    |       NUMBER { $$ = new Number($1); }
    |       IDENT { $$ = new Ident(string($1)); }
    ;
]])
AT_BISON_CHECK([-o test.c test.y], 1, [],
[[test.y:24.36-41: error: invalid reference: '$cond1'
test.y:23.11-24.62: note: symbol not found in production: cond1
test.y:26.43-53: error: invalid reference: '$stmt.field'
test.y:25.11-26.60: note: symbol not found in production: stmt
test.y:25.35-38: note: possibly meant: $then.field, hiding $stmt.field at $4
test.y:28.43-52: error: invalid reference: '$stmt.list'
test.y:27.11-28.59: note: symbol not found in production: stmt
test.y:27.30-38: note: possibly meant: $[stmt.list] at $4
test.y:30.43-46: error: ambiguous reference: '$xyz'
test.y:29.35-37: note: refers to: $xyz at $4
test.y:29.50-52: note: refers to: $xyz at $6
test.y:32.43-52: error: invalid reference: '$stmt.list'
test.y:31.11-32.63: note: symbol not found in production: stmt
test.y:31.40-43: note: possibly meant: $then, hiding $[stmt.list] at $4
test.y:31.61-64: note: possibly meant: $else, hiding $[stmt.list] at $6
test.y:34.43-58: error: invalid reference: '$stmt.list.field'
test.y:33.11-34.69: note: symbol not found in production: stmt
test.y:33.40-43: note: possibly meant: $then.field, hiding $[stmt.list].field at $4
test.y:33.61-64: note: possibly meant: $else.field, hiding $[stmt.list].field at $6
test.y:36.43-54: error: invalid reference: '$[stmt.list]'
test.y:35.11-36.71: note: symbol not found in production: stmt.list
test.y:35.40-43: note: possibly meant: $then, hiding $[stmt.list] at $4
test.y:35.61-64: note: possibly meant: $else, hiding $[stmt.list] at $6
test.y:38.43-49: error: invalid reference: '$then.1'
test.y:37.11-38.60: note: symbol not found in production: then
test.y:37.40-45: note: possibly meant: $[then.1] at $4
test.y:40.43-55: error: invalid reference: '$then.1.field'
test.y:39.11-40.66: note: symbol not found in production: then
test.y:39.40-45: note: possibly meant: $[then.1].field at $4
test.y:42.44-50: error: invalid reference: '$stmt.x'
test.y:41.12-42.57: note: symbol not found in production: stmt
test.y:41.36-41: note: possibly meant: $[stmt.x].x, hiding $stmt.x at $4
test.y:41.36-41: note: possibly meant: $[stmt.x] at $4
test.y:44.13-22: error: invalid reference: '$if-stmt-a'
test.y:43.12-44.59: note: symbol not found in production: if
test.y:43.1-9: note: possibly meant: $[if-stmt-a] at $$
test.y:46.46-54: error: invalid reference: '$then-a.f'
test.y:45.12-46.65: note: symbol not found in production: then
test.y:45.41-46: note: possibly meant: $[then-a].f at $4
]])

AT_BISON_CHECK([-fcaret -o test.c test.y], 1, [],
[[test.y:24.36-41: error: invalid reference: '$cond1'
   24 |           { $if_stmt1 = new IfStmt($cond1, $then.f1, $else); };
      |                                    ^~~~~~
test.y:23.11-24.62: note: symbol not found in production: cond1
   23 | if_stmt1: IF expr[cond] THEN stmt[then] ELSE stmt.list[else] FI
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.y:26.43-53: error: invalid reference: '$stmt.field'
   26 |           { $if_stmt2 = new IfStmt($cond, $stmt.field, 0); };
      |                                           ^~~~~~~~~~~
test.y:25.11-26.60: note: symbol not found in production: stmt
   25 | if_stmt2: IF expr[cond] THEN stmt[then] FI
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.y:25.35-38: note: possibly meant: $then.field, hiding $stmt.field at $4
   25 | if_stmt2: IF expr[cond] THEN stmt[then] FI
      |                                   ^~~~
test.y:28.43-52: error: invalid reference: '$stmt.list'
   28 |           { $if_stmt3 = new IfStmt($cond, $stmt.list, 0); };
      |                                           ^~~~~~~~~~
test.y:27.11-28.59: note: symbol not found in production: stmt
   27 | if_stmt3: IF expr[cond] THEN stmt.list FI
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.y:27.30-38: note: possibly meant: $[stmt.list] at $4
   27 | if_stmt3: IF expr[cond] THEN stmt.list FI
      |                              ^~~~~~~~~
test.y:30.43-46: error: ambiguous reference: '$xyz'
   30 |           { $if_stmt4 = new IfStmt($cond, $xyz, $cond); };
      |                                           ^~~~
test.y:29.35-37: note: refers to: $xyz at $4
   29 | if_stmt4: IF expr[cond] THEN stmt[xyz] ELSE stmt[xyz] FI
      |                                   ^~~
test.y:29.50-52: note: refers to: $xyz at $6
   29 | if_stmt4: IF expr[cond] THEN stmt[xyz] ELSE stmt[xyz] FI
      |                                                  ^~~
test.y:32.43-52: error: invalid reference: '$stmt.list'
   32 |           { $if_stmt5 = new IfStmt($cond, $stmt.list, $else); };
      |                                           ^~~~~~~~~~
test.y:31.11-32.63: note: symbol not found in production: stmt
   31 | if_stmt5: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.y:31.40-43: note: possibly meant: $then, hiding $[stmt.list] at $4
   31 | if_stmt5: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
      |                                        ^~~~
test.y:31.61-64: note: possibly meant: $else, hiding $[stmt.list] at $6
   31 | if_stmt5: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
      |                                                             ^~~~
test.y:34.43-58: error: invalid reference: '$stmt.list.field'
   34 |           { $if_stmt6 = new IfStmt($cond, $stmt.list.field, $else); };
      |                                           ^~~~~~~~~~~~~~~~
test.y:33.11-34.69: note: symbol not found in production: stmt
   33 | if_stmt6: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.y:33.40-43: note: possibly meant: $then.field, hiding $[stmt.list].field at $4
   33 | if_stmt6: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
      |                                        ^~~~
test.y:33.61-64: note: possibly meant: $else.field, hiding $[stmt.list].field at $6
   33 | if_stmt6: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
      |                                                             ^~~~
test.y:36.43-54: error: invalid reference: '$[stmt.list]'
   36 |           { $if_stmt7 = new IfStmt($cond, $[stmt.list].field, $else); };
      |                                           ^~~~~~~~~~~~
test.y:35.11-36.71: note: symbol not found in production: stmt.list
   35 | if_stmt7: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.y:35.40-43: note: possibly meant: $then, hiding $[stmt.list] at $4
   35 | if_stmt7: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
      |                                        ^~~~
test.y:35.61-64: note: possibly meant: $else, hiding $[stmt.list] at $6
   35 | if_stmt7: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
      |                                                             ^~~~
test.y:38.43-49: error: invalid reference: '$then.1'
   38 |           { $if_stmt8 = new IfStmt($cond, $then.1, $else); };
      |                                           ^~~~~~~
test.y:37.11-38.60: note: symbol not found in production: then
   37 | if_stmt8: IF expr[cond] THEN stmt.list[then.1] ELSE stmt.list[else] FI
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.y:37.40-45: note: possibly meant: $[then.1] at $4
   37 | if_stmt8: IF expr[cond] THEN stmt.list[then.1] ELSE stmt.list[else] FI
      |                                        ^~~~~~
test.y:40.43-55: error: invalid reference: '$then.1.field'
   40 |           { $if_stmt9 = new IfStmt($cond, $then.1.field, $else); };
      |                                           ^~~~~~~~~~~~~
test.y:39.11-40.66: note: symbol not found in production: then
   39 | if_stmt9: IF expr[cond] THEN stmt.list[then.1] ELSE stmt.list[else] FI
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.y:39.40-45: note: possibly meant: $[then.1].field at $4
   39 | if_stmt9: IF expr[cond] THEN stmt.list[then.1] ELSE stmt.list[else] FI
      |                                        ^~~~~~
test.y:42.44-50: error: invalid reference: '$stmt.x'
   42 |           { $if_stmt10 = new IfStmt($cond, $stmt.x, 0); };
      |                                            ^~~~~~~
test.y:41.12-42.57: note: symbol not found in production: stmt
   41 | if_stmt10: IF expr[cond] THEN stmt[stmt.x] FI
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.y:41.36-41: note: possibly meant: $[stmt.x].x, hiding $stmt.x at $4
   41 | if_stmt10: IF expr[cond] THEN stmt[stmt.x] FI
      |                                    ^~~~~~
test.y:41.36-41: note: possibly meant: $[stmt.x] at $4
   41 | if_stmt10: IF expr[cond] THEN stmt[stmt.x] FI
      |                                    ^~~~~~
test.y:44.13-22: error: invalid reference: '$if-stmt-a'
   44 |           { $if-stmt-a = new IfStmt($cond, $then, $else); };
      |             ^~~~~~~~~~
test.y:43.12-44.59: note: symbol not found in production: if
   43 | if-stmt-a: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.y:43.1-9: note: possibly meant: $[if-stmt-a] at $$
   43 | if-stmt-a: IF expr[cond] THEN stmt.list[then] ELSE stmt.list[else] FI
      | ^~~~~~~~~
test.y:46.46-54: error: invalid reference: '$then-a.f'
   46 |           { $[if-stmt-b] = new IfStmt($cond, $then-a.f, $else); };
      |                                              ^~~~~~~~~
test.y:45.12-46.65: note: symbol not found in production: then
   45 | if-stmt-b: IF expr[cond] THEN if-stmt-a[then-a] ELSE stmt.list[else] FI
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.y:45.41-46: note: possibly meant: $[then-a].f at $4
   45 | if-stmt-b: IF expr[cond] THEN if-stmt-a[then-a] ELSE stmt.list[else] FI
      |                                         ^~~~~~
]])

AT_BISON_OPTION_POPDEFS
AT_CLEANUP

#######################################################################

AT_SETUP([Missing identifiers in brackets])
AT_BISON_OPTION_PUSHDEFS
AT_DATA_GRAMMAR([test.y],
[[
%%
start: foo[] bar
  { s = $foo; }
]])
AT_BISON_CHECK([-o test.c test.y], 1, [],
[[test.y:11.12: error: an identifier expected
]])
AT_BISON_OPTION_POPDEFS
AT_CLEANUP

#######################################################################

AT_SETUP([Redundant words in brackets])
AT_BISON_OPTION_PUSHDEFS
AT_DATA_GRAMMAR([test.y],
[[
%%
start: foo[ a d ] bar
  { s = $foo; }
]])
AT_BISON_CHECK([-o test.c test.y], 1, [],
[[test.y:11.15: error: unexpected identifier in bracketed name: 'd'
]])
AT_BISON_OPTION_POPDEFS
AT_CLEANUP

#######################################################################

AT_SETUP([Comments in brackets])
AT_BISON_OPTION_PUSHDEFS
AT_DATA_GRAMMAR([test.y],
[[
%%
start: foo[/* comment */] bar
  { s = $foo; }
]])
AT_BISON_CHECK([-o test.c test.y], 1, [],
[[test.y:11.25: error: an identifier expected
]])
AT_BISON_OPTION_POPDEFS
AT_CLEANUP

#######################################################################

AT_SETUP([Stray symbols in brackets])
AT_BISON_OPTION_PUSHDEFS
AT_DATA_GRAMMAR([test.y],
[[
%%
start: foo[ % /* aaa */ *&-.+\000\001\002\377 ] bar
  { s = $foo; }
]])
AT_PERL_REQUIRE([[-pi -e 's/\\(\d{3})/chr(oct($1))/ge' test.y]])
AT_BISON_CHECK([-o test.c test.y], 1, [],
[[test.y:11.13: error: invalid character in bracketed name: '%'
test.y:11.25-27: error: invalid characters in bracketed name: '*&-'
test.y:11.29-30: error: invalid characters in bracketed name: '+\0\001\002\377'
]])
AT_BISON_OPTION_POPDEFS
AT_CLEANUP

#######################################################################

AT_SETUP([Redundant words in LHS brackets])
AT_BISON_OPTION_PUSHDEFS
AT_DATA_GRAMMAR([test.y],
[[
%%
start[a s]: foo;
]])
AT_BISON_CHECK([-o test.c test.y], 1, [],
[[test.y:11.9: error: unexpected identifier in bracketed name: 's'
]])
AT_BISON_OPTION_POPDEFS
AT_CLEANUP

#######################################################################

# Bison used to free twice the named ref for "a", since a single copy
# was used in two rules.
AT_SETUP([Factored LHS])
AT_BISON_OPTION_PUSHDEFS
AT_DATA_GRAMMAR([test.y],
[[
%%
start[a]: "foo" | "bar";
]])
AT_BISON_CHECK([-o test.c test.y])
AT_BISON_OPTION_POPDEFS
AT_CLEANUP

#######################################################################

AT_SETUP([Unresolved references])
AT_BISON_OPTION_PUSHDEFS
AT_DATA_GRAMMAR([test.y],
[[
%%
stat:
  sym_a sym_b { func($sym.field); }
| sym_a sym_b { func($<aa>sym.field); }
| sym_a sym_b { func($[sym.field]); }
| sym_a sym_b { func($<aa>[sym.field]); }
| sym_a sym_b { func($sym); }
| sym_a sym_b { func($<aa>sym); }
| sym_a sym_b { func($[sym]); } sym_a sym_b { func($<aa>[sym]); }
;

stat1:
  sym_a sym_b { func($sym-field); }
| sym_a sym_b { func($<aa>sym-field); }
| sym_a sym_b { func($[sym-field]); }
| sym_a sym_b { func($<aa>[sym-field]); }
| sym_a sym_b { func($sym); }
| sym_a sym_b { func($<aa>sym); }
| sym_a sym_b { func($[sym]); } sym_a sym_b { func($<aa>[sym]); }
;

sym_a: 'a';
sym_b: 'b';
]])
AT_BISON_CHECK([-o test.c test.y], 1, [],
[[test.y:12.22-31: error: invalid reference: '$sym.field'
test.y:12.3-35: note: symbol not found in production: sym
test.y:13.22-35: error: invalid reference: '$<aa>sym.field'
test.y:13.3-39: note: symbol not found in production: sym
test.y:14.22-33: error: invalid reference: '$[sym.field]'
test.y:14.3-37: note: symbol not found in production: sym.field
test.y:15.22-37: error: invalid reference: '$<aa>[sym.field]'
test.y:15.3-41: note: symbol not found in production: sym.field
test.y:16.22-25: error: invalid reference: '$sym'
test.y:16.3-29: note: symbol not found in production: sym
test.y:17.22-29: error: invalid reference: '$<aa>sym'
test.y:17.3-33: note: symbol not found in production: sym
test.y:18.22-27: error: invalid reference: '$[sym]'
test.y:18.3-65: note: symbol not found in production before $3: sym
test.y:18.52-61: error: invalid reference: '$<aa>[sym]'
test.y:18.3-65: note: symbol not found in production: sym
test.y:22.22-31: error: invalid reference: '$sym-field'
test.y:22.3-35: note: symbol not found in production: sym
test.y:23.22-35: error: invalid reference: '$<aa>sym-field'
test.y:23.3-39: note: symbol not found in production: sym
test.y:24.22-33: error: invalid reference: '$[sym-field]'
test.y:24.3-37: note: symbol not found in production: sym-field
test.y:25.22-37: error: invalid reference: '$<aa>[sym-field]'
test.y:25.3-41: note: symbol not found in production: sym-field
test.y:26.22-25: error: invalid reference: '$sym'
test.y:26.3-29: note: symbol not found in production: sym
test.y:27.22-29: error: invalid reference: '$<aa>sym'
test.y:27.3-33: note: symbol not found in production: sym
test.y:28.22-27: error: invalid reference: '$[sym]'
test.y:28.3-65: note: symbol not found in production before $3: sym
test.y:28.52-61: error: invalid reference: '$<aa>[sym]'
test.y:28.3-65: note: symbol not found in production: sym
]])
AT_BISON_OPTION_POPDEFS
AT_CLEANUP

#######################################################################

AT_SETUP([[$ or @ followed by . or -]])
AT_DATA([[test.y]],
[[
%%
start:
  .field { $.field; }
| 'a'    { @.field; }
;
.field: ;
]])
AT_BISON_CHECK([[test.y]], [[1]], [],
[[test.y:4.12-18: error: invalid reference: '$.field'
test.y:4.13: note: syntax error after '$', expecting integer, letter, '_', '@<:@', or '$'
test.y:4.3-8: note: possibly meant: $[.field] at $1
test.y:5.12-18: error: invalid reference: '@.field'
test.y:5.13: note: syntax error after '@', expecting integer, letter, '_', '@<:@', or '$'
]])
AT_DATA([[test.y]],
[[
%%
start:
  'a' { $-field; }
| 'b' { @-field; }
;
]])
AT_BISON_CHECK([[test.y]], [[0]], [],
[[test.y:4.9: warning: stray '$' [-Wother]
test.y:5.9: warning: stray '@' [-Wother]
]])
AT_CLEANUP