]> git.saurik.com Git - bison.git/blame - tests/sets.at
tests: calc: minor refactoring.
[bison.git] / tests / sets.at
CommitLineData
ed8e1f68 1# Exercising Bison Grammar Sets. -*- Autotest -*-
1462fcee
JD
2# Copyright (C) 2001-2002, 2005, 2007, 2009-2010 Free Software
3# Foundation, Inc.
ed8e1f68 4
f16b0819 5# This program is free software: you can redistribute it and/or modify
ed8e1f68 6# it under the terms of the GNU General Public License as published by
f16b0819
PE
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
ed8e1f68
AD
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.
f16b0819 14#
ed8e1f68 15# You should have received a copy of the GNU General Public License
f16b0819 16# along with this program. If not, see <http://www.gnu.org/licenses/>.
ed8e1f68 17
1207eeac 18
318b76e9
AD
19# AT_EXTRACT_SETS(INPUT, OUTPUT)
20# ------------------------------
21# Extract the information about the grammar sets from a bison
22# trace output (INPUT), and save it in OUTPUT.
23# And remember, there is no alternation in portable sed.
24m4_define([AT_EXTRACT_SETS],
25[AT_DATA([extract.sed],
26[[#n
27/^NULLABLE$/ {
1207eeac 28 :null
318b76e9
AD
29 p
30 n
04098407 31 /^[ ]*$/ !b null
318b76e9
AD
32}
33/^FIRSTS$/ {
34 :firsts
35 p
36 n
04098407 37 /^[ ]*$/ !b firsts
318b76e9
AD
38}
39/^FDERIVES$/ {
1207eeac 40 :fderiv
318b76e9
AD
41 p
42 n
04098407 43 /^[ ]*$/ !b fderiv
318b76e9
AD
44}
45/^DERIVES$/ {
1207eeac 46 :deriv
318b76e9
AD
47 p
48 n
04098407 49 /^[ ]*$/ !b deriv
318b76e9
AD
50}
51]])
52AT_CHECK([sed -f extract.sed $1], 0, [stdout])
53AT_CHECK([mv stdout $2])
54])
55
56
57
ed8e1f68
AD
58AT_BANNER([[Grammar Sets (Firsts etc.).]])
59
60
61## ---------- ##
62## Nullable. ##
63## ---------- ##
64
65AT_SETUP([Nullable])
66
67# At some point, nullable had been smoking grass, and managed to say:
68#
69# Entering set_nullable
70# NULLABLE
71# 'e': yes
72# (null): no
73# ...
74
75AT_DATA([[input.y]],
76[[%%
77e: 'e' | /* Nothing */;
78]])
79
da730230 80AT_BISON_CHECK([[--trace=sets input.y]], [], [], [stderr])
318b76e9
AD
81AT_EXTRACT_SETS([stderr], [sets])
82AT_CHECK([[cat sets]], [],
83[[DERIVES
88bce5a2
AD
84 $accept derives
85 0 e $end
ed8e1f68 86 e derives
e1a4f3a4
AD
87 1 'e'
88 2 /* empty */
ed8e1f68 89NULLABLE
88bce5a2 90 $accept: no
ed8e1f68 91 e: yes
ed8e1f68 92FIRSTS
88bce5a2
AD
93 $accept firsts
94 $accept
1565b720 95 e
ed8e1f68 96 e firsts
1565b720 97 e
ed8e1f68 98FDERIVES
88bce5a2
AD
99 $accept derives
100 0 e $end
4b3d3a8e
AD
101 1 'e'
102 2 /* empty */
ed8e1f68 103 e derives
4b3d3a8e
AD
104 1 'e'
105 2 /* empty */
ed8e1f68
AD
106]])
107
108AT_CLEANUP
109
110
111## ---------------- ##
112## Broken Closure. ##
113## ---------------- ##
114
115# TC was once broken during a massive `simplification' of the code.
116# It resulted in bison dumping core on the following grammar (the
117# computation of FIRSTS uses TC). It managed to produce a pretty
118# exotic closure:
119#
120# TC: Input
121#
122# 01234567
123# +--------+
124# 0| 1 |
125# 1| 1 |
126# 2| 1 |
127# 3| 1 |
128# 4| 1 |
129# 5| 1 |
130# 6| 1|
131# 7| |
132# +--------+
133#
134# TC: Output
135#
136# 01234567
137# +--------+
138# 0| 1 |
139# 1| 111 |
140# 2| 111 |
141# 3| 1111 |
142# 4| 111 1 |
143# 5| 111 1 |
144# 6| 111 1|
145# 7| 111 |
146# +--------+
147#
148# instead of that below.
149
150AT_SETUP([Broken Closure])
151
152AT_DATA([input.y],
153[[%%
e5352bc7
AD
154a: b;
155b: c;
156c: d;
157d: e;
158e: f;
159f: g;
160g: h;
161h: 'h';
ed8e1f68
AD
162]])
163
da730230 164AT_BISON_CHECK([[--trace=sets input.y]], [], [], [stderr])
30171f79 165
1565b720
AD
166AT_CHECK([[sed -n 's/[ ]*$//;/^RTC: Firsts Output BEGIN/,/^RTC: Firsts Output END/p' stderr]], [],
167[[RTC: Firsts Output BEGIN
30171f79
AD
168
169 012345678
170 .---------.
65ccf9fc
AD
171 0|111111111|
172 1| 11111111|
173 2| 1111111|
174 3| 111111|
175 4| 11111|
176 5| 1111|
177 6| 111|
178 7| 11|
179 8| 1|
30171f79 180 `---------'
1565b720 181RTC: Firsts Output END
ed8e1f68
AD
182]])
183
184AT_CLEANUP
318b76e9
AD
185
186
187
188## -------- ##
189## Firsts. ##
190## -------- ##
191
192AT_SETUP([Firsts])
193
194AT_DATA([input.y],
195[[%nonassoc '<' '>'
196%left '+' '-'
197%right '^' '='
198%%
199exp:
200 exp '<' exp
201 | exp '>' exp
202 | exp '+' exp
203 | exp '-' exp
204 | exp '^' exp
205 | exp '=' exp
206 | "exp"
207 ;
208]])
209
da730230 210AT_BISON_CHECK([[--trace=sets input.y]], [], [], [stderr])
318b76e9
AD
211AT_EXTRACT_SETS([stderr], [sets])
212AT_CHECK([[cat sets]], [],
213[[DERIVES
88bce5a2
AD
214 $accept derives
215 0 exp $end
318b76e9 216 exp derives
e1a4f3a4
AD
217 1 exp '<' exp
218 2 exp '>' exp
219 3 exp '+' exp
220 4 exp '-' exp
221 5 exp '^' exp
222 6 exp '=' exp
223 7 "exp"
318b76e9 224NULLABLE
88bce5a2 225 $accept: no
318b76e9
AD
226 exp: no
227FIRSTS
88bce5a2
AD
228 $accept firsts
229 $accept
1565b720 230 exp
318b76e9 231 exp firsts
1565b720 232 exp
318b76e9 233FDERIVES
88bce5a2
AD
234 $accept derives
235 0 exp $end
4b3d3a8e
AD
236 1 exp '<' exp
237 2 exp '>' exp
238 3 exp '+' exp
239 4 exp '-' exp
240 5 exp '^' exp
241 6 exp '=' exp
242 7 "exp"
318b76e9 243 exp derives
4b3d3a8e
AD
244 1 exp '<' exp
245 2 exp '>' exp
246 3 exp '+' exp
247 4 exp '-' exp
248 5 exp '^' exp
249 6 exp '=' exp
250 7 "exp"
318b76e9
AD
251]])
252
253AT_CLEANUP
317fce92
AD
254
255
256
257
258## -------- ##
259## Accept. ##
260## -------- ##
261
262# In some weird cases Bison could compute an incorrect final state
263# number. This happens only if the $end token is used in the user
264# grammar, which is a very suspicious accidental feature introduced as
265# a side effect of allowing the user to name $end using `%token END 0
266# "end of file"'.
267
268AT_SETUP([Accept])
269
270AT_DATA([input.y],
271[[%token END 0
272%%
273input:
274 'a'
275| '(' input ')'
276| '(' error END
277;
278]])
279
da730230 280AT_BISON_CHECK([[-v -o input.c input.y]])
317fce92
AD
281
282# Get the final state in the parser.
0eca5a39 283AT_CHECK([[sed -n 's/.*define YYFINAL *\([0-9][0-9]*\)/final state \1/p' input.c]],
317fce92
AD
284 0, [stdout])
285mv stdout expout
286
287# Get the final state in the report, from the "accept" action..
288AT_CHECK([sed -n '
289 /^state \(.*\)/{
290 s//final state \1/
291 x
292 }
293 / accept/{
294 x
295 p
296 q
297 }
298 ' input.output],
299 0, [expout])
300
301AT_CLEANUP