summaryrefslogtreecommitdiff
path: root/tests/sets.at
blob: 8b9f602947dffafc6e3c7194ff84c30a0596c4c0 (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
# Exercising Bison Grammar Sets.                      -*- Autotest -*-

# Copyright (C) 2001-2002, 2005, 2007, 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/>.


AT_BANNER([[Grammar Sets (Firsts etc.).]])


## ---------- ##
## Nullable.  ##
## ---------- ##

AT_SETUP([Nullable])

# At some point, nullable had been smoking grass, and managed to say:
#
# Entering set_nullable
# NULLABLE
#         'e': yes
#         (null): no
# ...

AT_DATA([[input.y]],
[[%%
e: 'e' | /* Nothing */;
]])

AT_BISON_CHECK([[--trace=sets input.y]], [], [], [stderr])
AT_SETS_CHECK([stderr], [[DERIVES], [NULLABLE], [FIRSTS], [FDERIVES]],
[[DERIVES
  $accept derives
      0  e $end
  e derives
      1  'e'
      2  %empty
NULLABLE
  $accept: no
  e: yes
FIRSTS
  $accept firsts
    $accept
    e
  e firsts
    e
FDERIVES
  $accept derives
      0  e $end
      1  'e'
      2  %empty
  e derives
      1  'e'
      2  %empty
]])

AT_CLEANUP


## ---------------- ##
## Broken Closure.  ##
## ---------------- ##

# TC was once broken during a massive 'simplification' of the code.
# It resulted in bison dumping core on the following grammar (the
# computation of FIRSTS uses TC).  It managed to produce a pretty
# exotic closure:
#
# TC: Input
#
#    01234567
#   +--------+
#  0| 1      |
#  1|  1     |
#  2|   1    |
#  3|    1   |
#  4|     1  |
#  5|      1 |
#  6|       1|
#  7|        |
#   +--------+
#
# TC: Output
#
#    01234567
#   +--------+
#  0| 1      |
#  1| 111    |
#  2| 111    |
#  3| 1111   |
#  4| 111 1  |
#  5| 111  1 |
#  6| 111   1|
#  7| 111    |
#   +--------+
#
# instead of that below.

AT_SETUP([Broken Closure])

AT_DATA([input.y],
[[%%
a: b;
b: c;
c: d;
d: e;
e: f;
f: g;
g: h;
h: 'h';
]])

AT_BISON_CHECK([[--trace=sets input.y]], [], [], [stderr])

AT_CHECK([[sed -n 's/[   ]*$//;/^RTC: Firsts Output BEGIN/,/^RTC: Firsts Output END/p' stderr]], [],
[[RTC: Firsts Output BEGIN

   012345678
  .---------.
 0|111111111|
 1| 11111111|
 2|  1111111|
 3|   111111|
 4|    11111|
 5|     1111|
 6|      111|
 7|       11|
 8|        1|
  `---------'
RTC: Firsts Output END
]])

AT_CLEANUP



## -------- ##
## Firsts.  ##
## -------- ##

AT_SETUP([Firsts])

AT_DATA([input.y],
[[%nonassoc '<' '>'
%left '+' '-'
%right '^' '='
%%
exp:
   exp '<' exp
 | exp '>' exp
 | exp '+' exp
 | exp '-' exp
 | exp '^' exp
 | exp '=' exp
 | "exp"
 ;
]])

AT_BISON_CHECK([[--trace=sets input.y]], [], [], [stderr])
AT_SETS_CHECK([stderr], [[DERIVES], [NULLABLE], [FIRSTS], [FDERIVES]],
[[DERIVES
  $accept derives
      0  exp $end
  exp derives
      1  exp '<' exp
      2  exp '>' exp
      3  exp '+' exp
      4  exp '-' exp
      5  exp '^' exp
      6  exp '=' exp
      7  "exp"
NULLABLE
  $accept: no
  exp: no
FIRSTS
  $accept firsts
    $accept
    exp
  exp firsts
    exp
FDERIVES
  $accept derives
      0  exp $end
      1  exp '<' exp
      2  exp '>' exp
      3  exp '+' exp
      4  exp '-' exp
      5  exp '^' exp
      6  exp '=' exp
      7  "exp"
  exp derives
      1  exp '<' exp
      2  exp '>' exp
      3  exp '+' exp
      4  exp '-' exp
      5  exp '^' exp
      6  exp '=' exp
      7  "exp"
]])

AT_CLEANUP




## -------- ##
## Accept.  ##
## -------- ##

# In some weird cases Bison could compute an incorrect final state
# number.  This happens only if the $end token is used in the user
# grammar, which is a very suspicious accidental feature introduced as
# a side effect of allowing the user to name $end using '%token END 0
# "end of file"'.

AT_SETUP([Accept])

AT_DATA([input.y],
[[%token END 0
%%
input:
  'a'
| '(' input ')'
| '(' error END
;
]])

AT_BISON_CHECK([[-v -o input.c input.y]])

# Get the final state in the parser.
AT_CHECK([[sed -n 's/.*define YYFINAL *\([0-9][0-9]*\)/final state \1/p' input.c]],
         0, [stdout])
mv stdout expout

# Get the final state in the report, from the "accept" action..
AT_CHECK([sed -n '
           /^State \(.*\)/{
             s//final state \1/
             x
           }
           / accept/{
             x
             p
             q
           }
        ' input.output],
        0, [expout])

AT_CLEANUP



## ----------------- ##
## Build relations.  ##
## ----------------- ##

AT_SETUP([Build relations])

# The "includes" relation [DeRemer 1982] is between gotos, so of
# course, for a given goto, there cannot be more that ngotos (number
# of gotos) images.  But we manipulate the set of images of a goto as
# a list, without checking that an image was not already introduced.
# So we can "register" way more images than ngotos, leading to a crash
# (heap buffer overflow).
#
# https://lists.gnu.org/r/bug-bison/2019-03/msg00007.html

AT_DATA([input.y],
[[%%
expr: term | term | term | term | term | term
term: 'n'
]])

AT_BISON_CHECK([[-fcaret input.y]], [], [],
[[input.y: warning: 5 reduce/reduce conflicts [-Wconflicts-rr]
input.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples
input.y:2.14-17: warning: rule useless in parser due to conflicts [-Wother]
    2 | expr: term | term | term | term | term | term
      |              ^~~~
input.y:2.21-24: warning: rule useless in parser due to conflicts [-Wother]
    2 | expr: term | term | term | term | term | term
      |                     ^~~~
input.y:2.28-31: warning: rule useless in parser due to conflicts [-Wother]
    2 | expr: term | term | term | term | term | term
      |                            ^~~~
input.y:2.35-38: warning: rule useless in parser due to conflicts [-Wother]
    2 | expr: term | term | term | term | term | term
      |                                   ^~~~
input.y:2.42-45: warning: rule useless in parser due to conflicts [-Wother]
    2 | expr: term | term | term | term | term | term
      |                                          ^~~~
]])

AT_CLEANUP


## ----------------- ##
## Reduced Grammar.  ##
## ----------------- ##

# Check information about the grammar, once reduced.

AT_SETUP([Reduced Grammar])

AT_DATA([input.y],
[[%%
expr: expr "+" term | term
term: term "*" fact | fact
useless: "useless"
fact: "num"
]])

AT_BISON_CHECK([[--trace=grammar -o input.c input.y]], [], [],
[[bison (GNU Bison) ]AT_PACKAGE_VERSION[
input.y: warning: 1 nonterminal useless in grammar [-Wother]
input.y: warning: 1 rule useless in grammar [-Wother]
input.y:4.1-7: warning: nonterminal useless in grammar: useless [-Wother]
Reduced Grammar

ntokens = 7, nnterms = 4, nsyms = 11, nrules = 6, nritems = 17

Tokens
------

Value  Sprec  Sassoc  Tag
    0      0       0  $end
    1      0       0  error
    2      0       0  $undefined
    3      0       0  "+"
    4      0       0  "*"
    5      0       0  "useless"
    6      0       0  "num"


Nonterminals
------------

Value  Tag
    7  $accept
    8  expr
    9  term
   10  fact


Rules
-----

Num (Prec, Assoc, Useful, UselessChain) Lhs -> (Ritem Range) Rhs
  0 ( 0,  0,  t,  f)    7 -> ( 0- 1)   8   0
  1 ( 0,  0,  t,  f)    8 -> ( 3- 5)   8   3   9
  2 ( 0,  0,  t,  t)    8 -> ( 7- 7)   9
  3 ( 0,  0,  t,  f)    9 -> ( 9-11)   9   4  10
  4 ( 0,  0,  t,  t)    9 -> (13-13)  10
  5 ( 0,  0,  t,  t)   10 -> (17-17)   6
  6 ( 0,  0,  f,  t)   11 -> (15-15)   5


Rules interpreted
-----------------

0      $accept: expr $end
1      expr: expr "+" term
2      expr: term
3      term: term "*" fact
4      term: fact
5      fact: "num"
6      useless: "useless"


reduced input.y defines 7 terminals, 4 nonterminals, and 6 productions.
]])

AT_CLEANUP


## ------------------------------------- ##
## Reduced Grammar with prec and assoc.  ##
## ------------------------------------- ##

# Check information about the grammar, once reduced.

AT_SETUP([Reduced Grammar with prec and assoc])

AT_DATA([input.y],
[[%nonassoc '<' '>'
%left '+' '-'
%right '^' '='
%%
exp:
   exp '<' exp
 | exp '>' exp
 | exp '+' exp
 | exp '-' exp
 | exp '^' exp
 | exp '=' exp
 | "exp"
 ;
]])

AT_BISON_CHECK([[--trace=grammar -o input.c input.y]], [], [],
[[bison (GNU Bison) ]AT_PACKAGE_VERSION[
Reduced Grammar

ntokens = 10, nnterms = 2, nsyms = 12, nrules = 8, nritems = 29

Tokens
------

Value  Sprec  Sassoc  Tag
    0      0       0  $end
    1      0       0  error
    2      0       0  $undefined
    3      1       3  '<'
    4      1       3  '>'
    5      2       2  '+'
    6      2       2  '-'
    7      3       1  '^'
    8      3       1  '='
    9      0       0  "exp"


Nonterminals
------------

Value  Tag
   10  $accept
   11  exp


Rules
-----

Num (Prec, Assoc, Useful, UselessChain) Lhs -> (Ritem Range) Rhs
  0 ( 0,  0,  t,  f)   10 -> ( 0- 1)  11   0
  1 ( 1,  3,  t,  f)   11 -> ( 3- 5)  11   3  11
  2 ( 1,  3,  t,  f)   11 -> ( 7- 9)  11   4  11
  3 ( 2,  2,  t,  f)   11 -> (11-13)  11   5  11
  4 ( 2,  2,  t,  f)   11 -> (15-17)  11   6  11
  5 ( 3,  1,  t,  f)   11 -> (19-21)  11   7  11
  6 ( 3,  1,  t,  f)   11 -> (23-25)  11   8  11
  7 ( 0,  0,  t,  t)   11 -> (27-27)   9


Rules interpreted
-----------------

0      $accept: exp $end
1      exp: exp '<' exp
2      exp: exp '>' exp
3      exp: exp '+' exp
4      exp: exp '-' exp
5      exp: exp '^' exp
6      exp: exp '=' exp
7      exp: "exp"


reduced input.y defines 10 terminals, 2 nonterminals, and 8 productions.
]])

AT_CLEANUP