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