]> git.saurik.com Git - bison.git/blob - tests/cxx-type.at
yacc: work around the ylwrap limitation.
[bison.git] / tests / cxx-type.at
1 # Checking GLR Parsing. -*- Autotest -*-
2
3 # Copyright (C) 2002-2012 Free Software Foundation, Inc.
4
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.
9 #
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.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 AT_BANNER([[C++ Type Syntax (GLR).]])
19
20 # _AT_TEST_GLR_CXXTYPES(DECL, RESOLVE1, RESOLVE2)
21 # -----------------------------------------------
22 # Store into types.y the calc program, with DECL inserted as a declaration,
23 # and with RESOLVE1 and RESOLVE2 as annotations on the conflicted rule for
24 # stmt. Then compile the result.
25 m4_define([_AT_TEST_GLR_CXXTYPES],
26 [AT_BISON_OPTION_PUSHDEFS([%glr-parser $1])
27
28 AT_DATA_GRAMMAR([types.y],
29 [[/* Simplified C++ Type and Expression Grammar. */
30
31 $1
32
33 %code requires
34 {
35 #include <stdio.h>
36 union Node {
37 struct {
38 int isNterm;
39 int parents;
40 } nodeInfo;
41 struct {
42 int isNterm; /* 1 */
43 int parents;
44 char const *form;
45 union Node *children[3];
46 } nterm;
47 struct {
48 int isNterm; /* 0 */
49 int parents;
50 char *text;
51 } term;
52 };
53 typedef union Node Node;
54 #define YYSTYPE Node *
55 }
56
57 %code
58 {
59 static Node *new_nterm (char const *, Node *, Node *, Node *);
60 static Node *new_term (char *);
61 static void free_node (Node *);
62 static char *node_to_string (Node *);
63 ]m4_bmatch([$2], [stmtMerge],
64 [ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[
65 #define YYINITDEPTH 10
66 #define YYSTACKEXPANDABLE 1
67 ]AT_YYERROR_DECLARE[
68 ]AT_YYLEX_DECLARE[
69 }
70
71 %token TYPENAME ID
72
73 %right '='
74 %left '+'
75
76 %glr-parser
77
78 %destructor { free_node ($$); } stmt expr decl declarator TYPENAME ID
79
80 %%
81
82 prog :
83 | prog stmt {
84 char *output;]AT_LOCATION_IF([
85 printf ("%d.%d-%d.%d: ",
86 @2.first_line, @2.first_column,
87 @2.last_line, @2.last_column);])[
88 output = node_to_string (]$[2);
89 printf ("%s\n", output);
90 free (output);
91 free_node (]$[2);
92 }
93 ;
94
95 stmt : expr ';' $2 { $$ = ]$[1; }
96 | decl $3
97 | error ';' { $$ = new_nterm ("<error>", YY_NULL, YY_NULL, YY_NULL); }
98 | '@' { YYACCEPT; }
99 ;
100
101 expr : ID
102 | TYPENAME '(' expr ')'
103 { $$ = new_nterm ("<cast>(%s,%s)", ]$[3, ]$[1, YY_NULL); }
104 | expr '+' expr { $$ = new_nterm ("+(%s,%s)", ]$[1, ]$[3, YY_NULL); }
105 | expr '=' expr { $$ = new_nterm ("=(%s,%s)", ]$[1, ]$[3, YY_NULL); }
106 ;
107
108 decl : TYPENAME declarator ';'
109 { $$ = new_nterm ("<declare>(%s,%s)", ]$[1, ]$[2, YY_NULL); }
110 | TYPENAME declarator '=' expr ';'
111 { $$ = new_nterm ("<init-declare>(%s,%s,%s)", ]$[1,
112 ]$[2, ]$[4); }
113 ;
114
115 declarator : ID
116 | '(' declarator ')' { $$ = ]$[2; }
117 ;
118
119 %%
120
121 #include <ctype.h>
122 #include <stdlib.h>
123 #include <string.h>
124 #include <stdarg.h>
125
126 int
127 main (int argc, char **argv)
128 {
129 if (argc != 2)
130 abort ();
131 if (!freopen (argv[1], "r", stdin))
132 return 3;
133 return yyparse ();
134 }
135
136 ]AT_YYERROR_DEFINE[
137
138 ]AT_YYLEX_PROTOTYPE[
139 {
140 char buffer[256];
141 int c;
142 unsigned int i;
143 static int lineNum = 1;
144 static int colNum = 0;
145
146 #if YYPURE
147 # undef yylloc
148 # define yylloc (*llocp)
149 # undef yylval
150 # define yylval (*lvalp)
151 #endif
152
153 while (1)
154 {
155 if (feof (stdin))
156 abort ();
157 c = getchar ();
158 switch (c)
159 {
160 case EOF:
161 return 0;
162 case '\t':
163 colNum = (colNum + 7) & ~7;
164 break;
165 case ' ': case '\f':
166 colNum += 1;
167 break;
168 case '\n':
169 lineNum += 1;
170 colNum = 0;
171 break;
172 default:
173 {
174 int tok;]AT_LOCATION_IF([[
175 yylloc.first_line = yylloc.last_line = lineNum;
176 yylloc.first_column = colNum;]])[
177 if (isalpha (c))
178 {
179 i = 0;
180
181 do
182 {
183 buffer[i++] = c;
184 colNum += 1;
185 if (i == sizeof buffer - 1)
186 abort ();
187 c = getchar ();
188 }
189 while (isalnum (c) || c == '_');
190
191 ungetc (c, stdin);
192 buffer[i++] = 0;
193 tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
194 yylval = new_term (strcpy ((char *) malloc (i), buffer));
195 }
196 else
197 {
198 colNum += 1;
199 tok = c;
200 yylval = YY_NULL;
201 }]AT_LOCATION_IF([[
202 yylloc.last_column = colNum-1;]])[
203 return tok;
204 }
205 }
206 }
207 }
208
209 static Node *
210 new_nterm (char const *form, Node *child0, Node *child1, Node *child2)
211 {
212 Node *node = (Node *) malloc (sizeof (Node));
213 node->nterm.isNterm = 1;
214 node->nterm.parents = 0;
215 node->nterm.form = form;
216 node->nterm.children[0] = child0;
217 if (child0)
218 child0->nodeInfo.parents += 1;
219 node->nterm.children[1] = child1;
220 if (child1)
221 child1->nodeInfo.parents += 1;
222 node->nterm.children[2] = child2;
223 if (child2)
224 child2->nodeInfo.parents += 1;
225 return node;
226 }
227
228 static Node *
229 new_term (char *text)
230 {
231 Node *node = (Node *) malloc (sizeof (Node));
232 node->term.isNterm = 0;
233 node->term.parents = 0;
234 node->term.text = text;
235 return node;
236 }
237
238 static void
239 free_node (Node *node)
240 {
241 if (!node)
242 return;
243 node->nodeInfo.parents -= 1;
244 /* Free only if 0 (last parent) or -1 (no parents). */
245 if (node->nodeInfo.parents > 0)
246 return;
247 if (node->nodeInfo.isNterm == 1)
248 {
249 free_node (node->nterm.children[0]);
250 free_node (node->nterm.children[1]);
251 free_node (node->nterm.children[2]);
252 }
253 else
254 free (node->term.text);
255 free (node);
256 }
257
258 static char *
259 node_to_string (Node *node)
260 {
261 char *child0;
262 char *child1;
263 char *child2;
264 char *buffer;
265 if (!node)
266 {
267 buffer = (char *) malloc (1);
268 buffer[0] = 0;
269 }
270 else if (node->nodeInfo.isNterm == 1)
271 {
272 child0 = node_to_string (node->nterm.children[0]);
273 child1 = node_to_string (node->nterm.children[1]);
274 child2 = node_to_string (node->nterm.children[2]);
275 buffer = (char *) malloc (strlen (node->nterm.form) + strlen (child0)
276 + strlen (child1) + strlen (child2) + 1);
277 sprintf (buffer, node->nterm.form, child0, child1, child2);
278 free (child0);
279 free (child1);
280 free (child2);
281 }
282 else
283 buffer = strdup (node->term.text);
284 return buffer;
285 }
286
287 ]]
288 m4_bmatch([$2], [stmtMerge],
289 [[static YYSTYPE
290 stmtMerge (YYSTYPE x0, YYSTYPE x1)
291 {
292 return new_nterm ("<OR>(%s,%s)", x0, x1, YY_NULL);
293 }
294 ]])
295 )
296
297 AT_DATA([test-input],
298 [[
299
300 z + q;
301
302 T x;
303
304 T x = y;
305
306 x = y;
307
308 T (x) + y;
309
310 T (x);
311
312 T (y) = z + q;
313
314 T (y y) = z + q;
315
316 z + q;
317
318 @
319
320 This is total garbage, but it should be ignored.
321 ]])
322
323 AT_BISON_CHECK([-o types.c types.y], 0, [], ignore)
324 AT_COMPILE([types])
325 AT_BISON_OPTION_POPDEFS
326 ])
327
328 m4_define([_AT_RESOLVED_GLR_OUTPUT],
329 [[+(z,q)
330 <declare>(T,x)
331 <init-declare>(T,x,y)
332 =(x,y)
333 +(<cast>(x,T),y)
334 <declare>(T,x)
335 <init-declare>(T,y,+(z,q))
336 <error>
337 +(z,q)
338 ]])
339
340 m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC],
341 [[3.0-3.5: +(z,q)
342 5.0-5.3: <declare>(T,x)
343 7.0-7.7: <init-declare>(T,x,y)
344 9.0-9.5: =(x,y)
345 11.0-11.9: +(<cast>(x,T),y)
346 13.0-13.5: <declare>(T,x)
347 15.0-15.13: <init-declare>(T,y,+(z,q))
348 17.0-17.15: <error>
349 19.0-19.5: +(z,q)
350 ]])
351
352 m4_define([_AT_AMBIG_GLR_OUTPUT],
353 [[+(z,q)
354 <declare>(T,x)
355 <init-declare>(T,x,y)
356 =(x,y)
357 +(<cast>(x,T),y)
358 <OR>(<declare>(T,x),<cast>(x,T))
359 <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
360 <error>
361 +(z,q)
362 ]])
363
364 m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC],
365 [[3.0-3.5: +(z,q)
366 5.0-5.3: <declare>(T,x)
367 7.0-7.7: <init-declare>(T,x,y)
368 9.0-9.5: =(x,y)
369 11.0-11.9: +(<cast>(x,T),y)
370 13.0-13.5: <OR>(<declare>(T,x),<cast>(x,T))
371 15.0-15.13: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
372 17.0-17.15: <error>
373 19.0-19.5: +(z,q)
374 ]])
375
376 m4_define([_AT_GLR_STDERR],
377 [[syntax error
378 ]])
379
380 m4_define([_AT_GLR_STDERR_WITH_LOC],
381 [[17.5-4: syntax error
382 ]])
383
384 m4_define([_AT_VERBOSE_GLR_STDERR],
385 [[syntax error, unexpected ID, expecting '=' or '+' or ')'
386 ]])
387
388 m4_define([_AT_VERBOSE_GLR_STDERR_WITH_LOC],
389 [[17.5-4: syntax error, unexpected ID, expecting '=' or '+' or ')'
390 ]])
391
392 ## ---------------------------------------------------- ##
393 ## Compile the grammar described in the documentation. ##
394 ## ---------------------------------------------------- ##
395
396 AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
397 _AT_TEST_GLR_CXXTYPES([],
398 [%dprec 1], [%dprec 2])
399 AT_PARSER_CHECK([[./types test-input]], 0,
400 [_AT_RESOLVED_GLR_OUTPUT], [_AT_GLR_STDERR])
401 AT_CLEANUP
402
403 AT_SETUP([GLR: Resolve ambiguity, impure, locations])
404 _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
405 AT_PARSER_CHECK([[./types test-input]], 0,
406 [_AT_RESOLVED_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
407 AT_CLEANUP
408
409 AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
410 _AT_TEST_GLR_CXXTYPES([%define api.pure],
411 [%dprec 1], [%dprec 2])
412 AT_PARSER_CHECK([[./types test-input]], 0,
413 [_AT_RESOLVED_GLR_OUTPUT], [_AT_GLR_STDERR])
414 AT_CLEANUP
415
416 AT_SETUP([GLR: Resolve ambiguity, pure, locations])
417 _AT_TEST_GLR_CXXTYPES([%define api.pure %locations],
418 [%dprec 1], [%dprec 2])
419 AT_PARSER_CHECK([[./types test-input]], 0,
420 [_AT_RESOLVED_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
421 AT_CLEANUP
422
423 AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
424 _AT_TEST_GLR_CXXTYPES([],
425 [%merge <stmtMerge>], [%merge <stmtMerge>])
426 AT_PARSER_CHECK([[./types test-input]], 0,
427 [_AT_AMBIG_GLR_OUTPUT], [_AT_GLR_STDERR])
428 AT_CLEANUP
429
430 AT_SETUP([GLR: Merge conflicting parses, impure, locations])
431 _AT_TEST_GLR_CXXTYPES([%locations],
432 [%merge <stmtMerge>], [%merge <stmtMerge>])
433 AT_PARSER_CHECK([[./types test-input]], 0,
434 [_AT_AMBIG_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
435 AT_CLEANUP
436
437 AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
438 _AT_TEST_GLR_CXXTYPES([%define api.pure],
439 [%merge <stmtMerge>], [%merge <stmtMerge>])
440 AT_PARSER_CHECK([[./types test-input]], 0,
441 [_AT_AMBIG_GLR_OUTPUT], [_AT_GLR_STDERR])
442 AT_CLEANUP
443 AT_SETUP([GLR: Merge conflicting parses, pure, locations])
444 _AT_TEST_GLR_CXXTYPES([%define api.pure %locations],
445 [%merge <stmtMerge>],[%merge <stmtMerge>])
446 AT_PARSER_CHECK([[./types test-input]], 0,
447 [_AT_AMBIG_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
448 AT_CLEANUP
449
450 AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
451 _AT_TEST_GLR_CXXTYPES([%error-verbose],
452 [%merge <stmtMerge>], [%merge <stmtMerge>])
453 AT_PARSER_CHECK([[./types test-input]], 0,
454 [_AT_AMBIG_GLR_OUTPUT], [_AT_VERBOSE_GLR_STDERR])
455 AT_CLEANUP