]> git.saurik.com Git - bison.git/blame - tests/glr-regression.at
(maintainer-check-g++): Fix a stray
[bison.git] / tests / glr-regression.at
CommitLineData
ede3d3bc 1# Checking GLR Parsing: Regression Tests -*- Autotest -*-
d6d67dbd 2# Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc.
ede3d3bc
PH
3
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2, or (at your option)
7# any later version.
8
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
0fb669f9
PE
16# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17# 02110-1301, USA.
ede3d3bc
PH
18
19AT_BANNER([[GLR Regression Tests]])
20
21## --------------------------- ##
22## Badly Collapsed GLR States. ##
23## --------------------------- ##
24
25AT_SETUP([Badly Collapsed GLR States])
26
27AT_DATA_GRAMMAR([glr-regr1.y],
28[[/* Regression Test: Improper state compression */
29/* Reported by Scott McPeak */
30
31%{
32#include <stdio.h>
33
34#define YYSTYPE int
35static YYSTYPE exprMerge (YYSTYPE x0, YYSTYPE x1);
36int yylex (void);
37int yyerror (char const *msg);
38%}
39
40
41%glr-parser
42
43
44/* -------- productions ------ */
45%%
46
47StartSymbol: E { $$=0; } %merge <exprMerge>
48 ;
49
50E: E 'P' E { $$=1; printf("E -> E 'P' E\n"); } %merge <exprMerge>
51 | 'B' { $$=2; printf("E -> 'B'\n"); } %merge <exprMerge>
52 ;
53
54
55
56/* ---------- C code ----------- */
57%%
58
59static YYSTYPE exprMerge (YYSTYPE x0, YYSTYPE x1)
60{
61 (void) x0;
62 (void) x1;
63 printf ("<OR>\n");
64 return 0;
65}
66
67int
68main (void)
69{
70 return yyparse ();
71}
72
73int
74yyerror (char const *msg)
75{
76 fprintf (stderr, "%s\n", msg);
77 exit (4);
78}
79
80
81int
82yylex (void)
83{
84 for (;;)
85 {
86 int ch = getchar ();
87 if (ch == EOF)
88 return 0;
89 else if (ch == 'B' || ch == 'P')
90 return ch;
91 }
92}
93]])
94
95AT_CHECK([[bison -o glr-regr1.c glr-regr1.y]], 0, [],
96[glr-regr1.y: conflicts: 1 shift/reduce
97])
98AT_COMPILE([glr-regr1])
99AT_CHECK([[echo BPBPB | ./glr-regr1]], 0,
100[[E -> 'B'
101E -> 'B'
102E -> E 'P' E
103E -> 'B'
104E -> E 'P' E
105E -> 'B'
106E -> E 'P' E
107E -> E 'P' E
108<OR>
109]], [])
110
111AT_CLEANUP
112
113## ------------------------------------------------------------ ##
114## Improper handling of embedded actions and $-N in GLR parsers ##
115## ------------------------------------------------------------ ##
116
d6d67dbd 117AT_SETUP([Improper handling of embedded actions and dollar(-N) in GLR parsers])
ede3d3bc
PH
118
119AT_DATA_GRAMMAR([glr-regr2a.y],
120[[/* Regression Test: Improper handling of embedded actions and $-N */
121/* Reported by S. Eken */
122
123%{
124 #define YYSTYPE char const *
125 #define yyfalse 0
126 #define yytrue 1
127
128 #include <ctype.h>
129 #include <stdio.h>
f508a6a0 130 #include <stdlib.h>
ede3d3bc
PH
131 #include <string.h>
132 int yylex (void);
133 void yyerror (char const *);
134%}
135
136%glr-parser
137
138%%
139
140command:
141 's' var 't'
d6d67dbd 142 { printf ("Variable: '%s'\n", $2); }
ede3d3bc
PH
143 'v' 'x' 'q'
144 | 's' var_list 't' 'e'
145 { printf ("Varlist: '%s'\n", $2); }
146 | 's' var 't' var_printer 'x'
147 ;
148
149var:
150 'V'
151 { $$ = $1; }
152 ;
153
154var_list:
155 var
156 { $$ = $1; }
157 | var ',' var_list
158 {
f508a6a0
PE
159 $$ = malloc (strlen ($1) + 1 + strlen ($3) + 1);
160 strcpy ($$, $1);
161 strcat ($$, ",");
162 strcat ($$, $3);
d6d67dbd 163 }
ede3d3bc
PH
164 ;
165
166var_printer: 'v'
167 { printf ("Variable: '%s'\n", $-1); }
168
169%%
170
171FILE *yyin = NULL;
172
173int
174yylex (void)
d6d67dbd 175{
ede3d3bc
PH
176 char buf[50];
177 switch (fscanf (yyin, " %1[a-z,]", buf)) {
178 case 1:
179 return buf[0];
180 case EOF:
181 return 0;
182 default:
183 break;
184 }
f508a6a0
PE
185 if (fscanf (yyin, "%49s", buf) != 1)
186 abort ();
187 if (sizeof buf - 1 <= strlen (buf))
188 abort ();
189 yylval = malloc (strlen (buf) + 1);
190 strcpy (yylval, buf);
ede3d3bc
PH
191 return 'V';
192}
193
194void
195yyerror (char const *s)
196{ printf ("%s\n", s);
197}
198
199int
200main (int argc, char **argv)
d6d67dbd 201{
ede3d3bc
PH
202 yyin = stdin;
203 if (argc == 2 && !(yyin = fopen (argv[1], "r"))) return 1;
204 return yyparse ();
205}
206]])
207
208AT_CHECK([[bison -o glr-regr2a.c glr-regr2a.y]], 0, [],
209[glr-regr2a.y: conflicts: 2 shift/reduce
210])
211AT_COMPILE([glr-regr2a])
212
213AT_CHECK([[echo s VARIABLE_1 t v x q | ./glr-regr2a]], 0,
214[[Variable: 'VARIABLE_1'
215]], [])
216AT_CHECK([[echo s VARIABLE_1 , ANOTHER_VARIABLE_2 t e | ./glr-regr2a]], 0,
217[[Varlist: 'VARIABLE_1,ANOTHER_VARIABLE_2'
218]])
219AT_CHECK([[echo s VARIABLE_3 t v x | ./glr-regr2a]], 0,
220[[Variable: 'VARIABLE_3'
221]], [])
222
223
5e6f62f2
PH
224AT_CLEANUP
225
226## ------------------------------------------------------------ ##
227## Improper merging of GLR delayed action sets ##
228## ------------------------------------------------------------ ##
229
230AT_SETUP([Improper merging of GLR delayed action sets])
231
232AT_DATA_GRAMMAR([glr-regr3.y],
233[[/* Regression Test: Improper merging of GLR delayed action sets. */
234/* Reported by M. Rosien */
235
236%{
237#include <stdio.h>
238#include <stdarg.h>
239
240static int MergeRule (int x0, int x1);
241static void yyerror(char const * s);
242
243#define RULE(x) (1 << (x))
244
245%}
246
247%glr-parser
248
249%token BAD_CHAR
250%token P1 P2 T1 T2 T3 T4 O1 O2
251
252%%
253
254S : P1 T4 O2 NT6 P2 { printf ("Result: %x\n", $4); }
255;
256
257NT1 : P1 T1 O1 T2 P2 { $$ = RULE(2); } %merge<MergeRule>
258;
259
260NT2 : NT1 { $$ = RULE(3); } %merge<MergeRule>
261 | P1 NT1 O1 T3 P2 { $$ = RULE(4); } %merge<MergeRule>
262;
263
264NT3 : T3 { $$ = RULE(5); } %merge<MergeRule>
265 | P1 NT1 O1 T3 P2 { $$ = RULE(6); } %merge<MergeRule>
266;
267
268NT4 : NT3 { $$ = RULE(7); } %merge<MergeRule>
269 | NT2 { $$ = RULE(8); } %merge<MergeRule>
270 | P1 NT2 O1 NT3 P2 { $$ = RULE(9); } %merge<MergeRule>
271;
272
273NT5 : NT4 { $$ = RULE(10); } %merge<MergeRule>
274;
275
276NT6 : P1 NT1 O1 T3 P2 { $$ = RULE(11) | $2; } %merge<MergeRule>
277 | NT5 { $$ = RULE(12) | $1; } %merge<MergeRule>
278;
279
280%%
281
282static int MergeRule (int x0, int x1) {
283 return x0 | x1;
284}
285
286static void yyerror(char const * s) {
287 fprintf(stderr,"error: %s\n",s);
288}
289
290FILE *yyin = NULL;
291
292int P[] = { P1, P2 };
293int O[] = { O1, O2 };
294int T[] = { T1, T2, T3, T4 };
295
296int yylex (void)
297{
298 char inp[3];
299 if (fscanf (yyin, "%2s", inp) == EOF)
300 return 0;
301 switch (inp[0])
302 {
303 case 'p': return P[inp[1] - '1'];
304 case 't': return T[inp[1] - '1'];
305 case 'o': return O[inp[1] - '1'];
306 }
307 return BAD_CHAR;
308}
309
310int main(int argc, char* argv[]) {
311 yyin = stdin;
312 if (argc == 2 && !(yyin = fopen (argv[1], "r"))) return 1;
313 return yyparse ();
314}
315]])
316
317AT_CHECK([[bison -o glr-regr3.c glr-regr3.y]], 0, [],
318[glr-regr3.y: conflicts: 1 shift/reduce, 1 reduce/reduce
319])
320AT_COMPILE([glr-regr3])
321
322AT_CHECK([[echo p1 t4 o2 p1 p1 t1 o1 t2 p2 o1 t3 p2 p2 | ./glr-regr3]], 0,
323[[Result: 1c04
324]], [])
325
ede3d3bc 326AT_CLEANUP