]> git.saurik.com Git - bison.git/blob - tests/cxx-type.at
e6fd8a85d0dca779f24f777fd42280e30efcf5f8
[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 int
139 yylex (]AT_LEX_FORMALS[)
140 {
141 char buffer[256];
142 int c;
143 unsigned int i;
144 static int lineNum = 1;
145 static int colNum = 0;
146
147 #if YYPURE
148 # undef yylloc
149 # define yylloc (*llocp)
150 # undef yylval
151 # define yylval (*lvalp)
152 #endif
153
154 while (1)
155 {
156 if (feof (stdin))
157 abort ();
158 c = getchar ();
159 switch (c)
160 {
161 case EOF:
162 return 0;
163 case '\t':
164 colNum = (colNum + 7) & ~7;
165 break;
166 case ' ': case '\f':
167 colNum += 1;
168 break;
169 case '\n':
170 lineNum += 1;
171 colNum = 0;
172 break;
173 default:
174 {
175 int tok;]AT_LOCATION_IF([[
176 yylloc.first_line = yylloc.last_line = lineNum;
177 yylloc.first_column = colNum;]])[
178 if (isalpha (c))
179 {
180 i = 0;
181
182 do
183 {
184 buffer[i++] = c;
185 colNum += 1;
186 if (i == sizeof buffer - 1)
187 abort ();
188 c = getchar ();
189 }
190 while (isalnum (c) || c == '_');
191
192 ungetc (c, stdin);
193 buffer[i++] = 0;
194 tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
195 yylval = new_term (strcpy ((char *) malloc (i), buffer));
196 }
197 else
198 {
199 colNum += 1;
200 tok = c;
201 yylval = YY_NULL;
202 }]AT_LOCATION_IF([[
203 yylloc.last_column = colNum-1;]])[
204 return tok;
205 }
206 }
207 }
208 }
209
210 static Node *
211 new_nterm (char const *form, Node *child0, Node *child1, Node *child2)
212 {
213 Node *node = (Node *) malloc (sizeof (Node));
214 node->nterm.isNterm = 1;
215 node->nterm.parents = 0;
216 node->nterm.form = form;
217 node->nterm.children[0] = child0;
218 if (child0)
219 child0->nodeInfo.parents += 1;
220 node->nterm.children[1] = child1;
221 if (child1)
222 child1->nodeInfo.parents += 1;
223 node->nterm.children[2] = child2;
224 if (child2)
225 child2->nodeInfo.parents += 1;
226 return node;
227 }
228
229 static Node *
230 new_term (char *text)
231 {
232 Node *node = (Node *) malloc (sizeof (Node));
233 node->term.isNterm = 0;
234 node->term.parents = 0;
235 node->term.text = text;
236 return node;
237 }
238
239 static void
240 free_node (Node *node)
241 {
242 if (!node)
243 return;
244 node->nodeInfo.parents -= 1;
245 /* Free only if 0 (last parent) or -1 (no parents). */
246 if (node->nodeInfo.parents > 0)
247 return;
248 if (node->nodeInfo.isNterm == 1)
249 {
250 free_node (node->nterm.children[0]);
251 free_node (node->nterm.children[1]);
252 free_node (node->nterm.children[2]);
253 }
254 else
255 free (node->term.text);
256 free (node);
257 }
258
259 static char *
260 node_to_string (Node *node)
261 {
262 char *child0;
263 char *child1;
264 char *child2;
265 char *buffer;
266 if (!node)
267 {
268 buffer = (char *) malloc (1);
269 buffer[0] = 0;
270 }
271 else if (node->nodeInfo.isNterm == 1)
272 {
273 child0 = node_to_string (node->nterm.children[0]);
274 child1 = node_to_string (node->nterm.children[1]);
275 child2 = node_to_string (node->nterm.children[2]);
276 buffer = (char *) malloc (strlen (node->nterm.form) + strlen (child0)
277 + strlen (child1) + strlen (child2) + 1);
278 sprintf (buffer, node->nterm.form, child0, child1, child2);
279 free (child0);
280 free (child1);
281 free (child2);
282 }
283 else
284 buffer = strdup (node->term.text);
285 return buffer;
286 }
287
288 ]]
289 m4_bmatch([$2], [stmtMerge],
290 [[static YYSTYPE
291 stmtMerge (YYSTYPE x0, YYSTYPE x1)
292 {
293 return new_nterm ("<OR>(%s,%s)", x0, x1, YY_NULL);
294 }
295 ]])
296 )
297
298 AT_DATA([test-input],
299 [[
300
301 z + q;
302
303 T x;
304
305 T x = y;
306
307 x = y;
308
309 T (x) + y;
310
311 T (x);
312
313 T (y) = z + q;
314
315 T (y y) = z + q;
316
317 z + q;
318
319 @
320
321 This is total garbage, but it should be ignored.
322 ]])
323
324 AT_BISON_CHECK([-o types.c types.y], 0, [], ignore)
325 AT_COMPILE([types])
326 AT_BISON_OPTION_POPDEFS
327 ])
328
329 m4_define([_AT_RESOLVED_GLR_OUTPUT],
330 [[+(z,q)
331 <declare>(T,x)
332 <init-declare>(T,x,y)
333 =(x,y)
334 +(<cast>(x,T),y)
335 <declare>(T,x)
336 <init-declare>(T,y,+(z,q))
337 <error>
338 +(z,q)
339 ]])
340
341 m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC],
342 [[3.0-3.5: +(z,q)
343 5.0-5.3: <declare>(T,x)
344 7.0-7.7: <init-declare>(T,x,y)
345 9.0-9.5: =(x,y)
346 11.0-11.9: +(<cast>(x,T),y)
347 13.0-13.5: <declare>(T,x)
348 15.0-15.13: <init-declare>(T,y,+(z,q))
349 17.0-17.15: <error>
350 19.0-19.5: +(z,q)
351 ]])
352
353 m4_define([_AT_AMBIG_GLR_OUTPUT],
354 [[+(z,q)
355 <declare>(T,x)
356 <init-declare>(T,x,y)
357 =(x,y)
358 +(<cast>(x,T),y)
359 <OR>(<declare>(T,x),<cast>(x,T))
360 <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
361 <error>
362 +(z,q)
363 ]])
364
365 m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC],
366 [[3.0-3.5: +(z,q)
367 5.0-5.3: <declare>(T,x)
368 7.0-7.7: <init-declare>(T,x,y)
369 9.0-9.5: =(x,y)
370 11.0-11.9: +(<cast>(x,T),y)
371 13.0-13.5: <OR>(<declare>(T,x),<cast>(x,T))
372 15.0-15.13: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
373 17.0-17.15: <error>
374 19.0-19.5: +(z,q)
375 ]])
376
377 m4_define([_AT_GLR_STDERR],
378 [[syntax error
379 ]])
380
381 m4_define([_AT_GLR_STDERR_WITH_LOC],
382 [[17.5-4: syntax error
383 ]])
384
385 m4_define([_AT_VERBOSE_GLR_STDERR],
386 [[syntax error, unexpected ID, expecting '=' or '+' or ')'
387 ]])
388
389 m4_define([_AT_VERBOSE_GLR_STDERR_WITH_LOC],
390 [[17.5-4: syntax error, unexpected ID, expecting '=' or '+' or ')'
391 ]])
392
393 ## ---------------------------------------------------- ##
394 ## Compile the grammar described in the documentation. ##
395 ## ---------------------------------------------------- ##
396
397 AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
398 _AT_TEST_GLR_CXXTYPES([],
399 [%dprec 1], [%dprec 2])
400 AT_PARSER_CHECK([[./types test-input]], 0,
401 [_AT_RESOLVED_GLR_OUTPUT], [_AT_GLR_STDERR])
402 AT_CLEANUP
403
404 AT_SETUP([GLR: Resolve ambiguity, impure, locations])
405 _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
406 AT_PARSER_CHECK([[./types test-input]], 0,
407 [_AT_RESOLVED_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
408 AT_CLEANUP
409
410 AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
411 _AT_TEST_GLR_CXXTYPES([%define api.pure],
412 [%dprec 1], [%dprec 2])
413 AT_PARSER_CHECK([[./types test-input]], 0,
414 [_AT_RESOLVED_GLR_OUTPUT], [_AT_GLR_STDERR])
415 AT_CLEANUP
416
417 AT_SETUP([GLR: Resolve ambiguity, pure, locations])
418 _AT_TEST_GLR_CXXTYPES([%define api.pure %locations],
419 [%dprec 1], [%dprec 2])
420 AT_PARSER_CHECK([[./types test-input]], 0,
421 [_AT_RESOLVED_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
422 AT_CLEANUP
423
424 AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
425 _AT_TEST_GLR_CXXTYPES([],
426 [%merge <stmtMerge>], [%merge <stmtMerge>])
427 AT_PARSER_CHECK([[./types test-input]], 0,
428 [_AT_AMBIG_GLR_OUTPUT], [_AT_GLR_STDERR])
429 AT_CLEANUP
430
431 AT_SETUP([GLR: Merge conflicting parses, impure, locations])
432 _AT_TEST_GLR_CXXTYPES([%locations],
433 [%merge <stmtMerge>], [%merge <stmtMerge>])
434 AT_PARSER_CHECK([[./types test-input]], 0,
435 [_AT_AMBIG_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
436 AT_CLEANUP
437
438 AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
439 _AT_TEST_GLR_CXXTYPES([%define api.pure],
440 [%merge <stmtMerge>], [%merge <stmtMerge>])
441 AT_PARSER_CHECK([[./types test-input]], 0,
442 [_AT_AMBIG_GLR_OUTPUT], [_AT_GLR_STDERR])
443 AT_CLEANUP
444 AT_SETUP([GLR: Merge conflicting parses, pure, locations])
445 _AT_TEST_GLR_CXXTYPES([%define api.pure %locations],
446 [%merge <stmtMerge>],[%merge <stmtMerge>])
447 AT_PARSER_CHECK([[./types test-input]], 0,
448 [_AT_AMBIG_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
449 AT_CLEANUP
450
451 AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
452 _AT_TEST_GLR_CXXTYPES([%error-verbose],
453 [%merge <stmtMerge>], [%merge <stmtMerge>])
454 AT_PARSER_CHECK([[./types test-input]], 0,
455 [_AT_AMBIG_GLR_OUTPUT], [_AT_VERBOSE_GLR_STDERR])
456 AT_CLEANUP