gnulib: update
[bison.git] / tests / cxx-type.at
blob9aa8f709fd11de19e23d76227b8104d5a5f2add6
1 # Checking GLR Parsing.                         -*- Autotest -*-
3 # Copyright (C) 2002-2015, 2018-2021 Free Software Foundation, Inc.
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.
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.
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
18 AT_BANNER([[C++ Type Syntax (GLR).]])
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])
28 AT_DATA_GRAMMAR([types.y],
29 [[/* Simplified C++ Type and Expression Grammar.  */
31 %define parse.trace
34 %code requires
36   #include <stdio.h>
37   union Node {
38     struct {
39       int isNterm;
40       int parents;
41     } nodeInfo;
42     struct {
43       int isNterm; /* 1 */
44       int parents;
45       char const *form;
46       union Node *children[3];
47     } nterm;
48     struct {
49       int isNterm; /* 0 */
50       int parents;
51       char *text;
52     } term;
53   };
54   typedef union Node Node;
55   #define YYSTYPE Node *
58 %code
60   static Node *new_nterm (char const *, Node *, Node *, Node *);
61   static Node *new_term (char *);
62   static void free_node (Node *);
63   static char *node_to_string (Node *);
64 ]m4_bmatch([$2], [stmtMerge],
65 [ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[
66   #define YYINITDEPTH 10
67   #define YYSTACKEXPANDABLE 1
68   ]AT_YYERROR_DECLARE[
69   ]AT_YYLEX_DECLARE[
72 %token TYPENAME ID
74 %right '='
75 %left '+'
77 %glr-parser
79 %destructor { free_node ($$); } stmt expr decl declarator TYPENAME ID
83 prog :
84      | prog stmt   {
85                         char *output;]AT_LOCATION_IF([
86                         printf ("%d.%d-%d.%d: ",
87                              @2.first_line, @2.first_column,
88                              @2.last_line, @2.last_column);])[
89                         output = node_to_string (]$[2);
90                         printf ("%s\n", output);
91                         free (output);
92                         free_node (]$[2);
93                    }
94      ;
96 stmt : expr ';'  $2     { $$ = ]$[1; }
97      | decl      $3
98      | error ';'        { $$ = new_nterm ("<error>", YY_NULLPTR, YY_NULLPTR, YY_NULLPTR); }
99      | '@'              { YYACCEPT; }
100      ;
102 expr : ID
103      | TYPENAME '(' expr ')'
104                         { $$ = new_nterm ("<cast>(%s,%s)", ]$[3, ]$[1, YY_NULLPTR); }
105      | expr '+' expr    { $$ = new_nterm ("+(%s,%s)", ]$[1, ]$[3, YY_NULLPTR); }
106      | expr '=' expr    { $$ = new_nterm ("=(%s,%s)", ]$[1, ]$[3, YY_NULLPTR); }
107      ;
109 decl : TYPENAME declarator ';'
110                         { $$ = new_nterm ("<declare>(%s,%s)", ]$[1, ]$[2, YY_NULLPTR); }
111      | TYPENAME declarator '=' expr ';'
112                         { $$ = new_nterm ("<init-declare>(%s,%s,%s)", ]$[1,
113                                           ]$[2, ]$[4); }
114      ;
116 declarator : ID
117      | '(' declarator ')' { $$ = ]$[2; }
118      ;
122 #include <ctype.h>
123 #include <stdlib.h>
124 #include <string.h>
125 #include <stdarg.h>
126 #include <assert.h>
129 main (int argc, char **argv)
131   if (getenv ("YYDEBUG"))
132     yydebug = 1;
133   for (int i = 1; i < argc; ++i)
134     // Enable parse traces on option -p.
135     if (strcmp (argv[i], "-p") == 0)
136       yydebug = 1;
137     else
138       {
139         if (!freopen (argv[i], "r", stdin))
140           return 3;
141         int status = yyparse ();
142         if (!status)
143           return status;
144       }
145   return 0;
148 ]AT_YYERROR_DEFINE[
150 ]AT_YYLEX_PROTOTYPE[
152   static int lineNum = 1;
153   static int colNum = 0;
155 #if YYPURE
156 # undef yylloc
157 # define yylloc (*llocp)
158 # undef yylval
159 # define yylval (*lvalp)
160 #endif
162   while (1)
163     {
164       int c;
165       assert (!feof (stdin));
166       c = getchar ();
167       switch (c)
168         {
169         case EOF:
170           return 0;
171         case '\t':
172           colNum = (colNum + 7) & ~7;
173           break;
174         case ' ': case '\f':
175           colNum += 1;
176           break;
177         case '\n':
178           lineNum += 1;
179           colNum = 0;
180           break;
181         default:
182           {
183             int tok;]AT_LOCATION_IF([[
184             yylloc.first_line = yylloc.last_line = lineNum;
185             yylloc.first_column = colNum;]])[
186             if (isalpha (c))
187               {
188                 char buffer[256];
189                 unsigned i = 0;
191                 do
192                   {
193                     buffer[i++] = YY_CAST (char, c);
194                     colNum += 1;
195                     assert (i != sizeof buffer - 1);
196                     c = getchar ();
197                   }
198                 while (isalnum (c) || c == '_');
200                 ungetc (c, stdin);
201                 buffer[i++] = 0;
202                 tok = isupper (YY_CAST (unsigned char, buffer[0])) ? TYPENAME : ID;
203                 yylval = new_term (strcpy (YY_CAST (char *, malloc (i)), buffer));
204               }
205             else
206               {
207                 colNum += 1;
208                 tok = c;
209                 yylval = YY_NULLPTR;
210               }]AT_LOCATION_IF([[
211             yylloc.last_column = colNum;]])[
212             return tok;
213           }
214         }
215     }
218 static Node *
219 new_nterm (char const *form, Node *child0, Node *child1, Node *child2)
221   Node *node = YY_CAST (Node *, malloc (sizeof (Node)));
222   node->nterm.isNterm = 1;
223   node->nterm.parents = 0;
224   node->nterm.form = form;
225   node->nterm.children[0] = child0;
226   if (child0)
227     child0->nodeInfo.parents += 1;
228   node->nterm.children[1] = child1;
229   if (child1)
230     child1->nodeInfo.parents += 1;
231   node->nterm.children[2] = child2;
232   if (child2)
233     child2->nodeInfo.parents += 1;
234   return node;
237 static Node *
238 new_term (char *text)
240   Node *node = YY_CAST (Node *, malloc (sizeof (Node)));
241   node->term.isNterm = 0;
242   node->term.parents = 0;
243   node->term.text = text;
244   return node;
247 static void
248 free_node (Node *node)
250   if (!node)
251     return;
252   node->nodeInfo.parents -= 1;
253   /* Free only if 0 (last parent) or -1 (no parents).  */
254   if (node->nodeInfo.parents > 0)
255     return;
256   if (node->nodeInfo.isNterm == 1)
257     {
258       free_node (node->nterm.children[0]);
259       free_node (node->nterm.children[1]);
260       free_node (node->nterm.children[2]);
261     }
262   else
263     free (node->term.text);
264   free (node);
267 static char *
268 node_to_string (Node *node)
270   char *res;
271   if (!node)
272     {
273       res = YY_CAST (char *, malloc (1));
274       res[0] = 0;
275     }
276   else if (node->nodeInfo.isNterm == 1)
277     {
278       char *child0 = node_to_string (node->nterm.children[0]);
279       char *child1 = node_to_string (node->nterm.children[1]);
280       char *child2 = node_to_string (node->nterm.children[2]);
281       res = YY_CAST (char *, malloc (strlen (node->nterm.form) + strlen (child0)
282                                      + strlen (child1) + strlen (child2) + 1));
283       sprintf (res, node->nterm.form, child0, child1, child2);
284       free (child2);
285       free (child1);
286       free (child0);
287     }
288   else
289     res = strdup (node->term.text);
290   return res;
294 m4_bmatch([$2], [stmtMerge],
295 [[static YYSTYPE
296 stmtMerge (YYSTYPE x0, YYSTYPE x1)
298   return new_nterm ("<OR>(%s,%s)", x0, x1, YY_NULLPTR);
303 AT_DATA([test-input],
306 z + q;
308 T x;
310 T x = y;
312 x = y;
314 T (x) + y;
316 T (x);
318 T (y) = z + q;
320 T (y y) = z + q;
322 z + q;
326 This is total garbage, but it should be ignored.
329 AT_BISON_CHECK([-o types.c types.y], 0, [], ignore)
330 AT_COMPILE([types])
331 AT_BISON_OPTION_POPDEFS
334 m4_define([_AT_RESOLVED_GLR_OUTPUT],
335 [[+(z,q)
336 <declare>(T,x)
337 <init-declare>(T,x,y)
338 =(x,y)
339 +(<cast>(x,T),y)
340 <declare>(T,x)
341 <init-declare>(T,y,+(z,q))
342 <error>
343 +(z,q)
346 m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC],
347 [[3.0-3.6: +(z,q)
348 5.0-5.4: <declare>(T,x)
349 7.0-7.8: <init-declare>(T,x,y)
350 9.0-9.6: =(x,y)
351 11.0-11.10: +(<cast>(x,T),y)
352 13.0-13.6: <declare>(T,x)
353 15.0-15.14: <init-declare>(T,y,+(z,q))
354 17.0-17.16: <error>
355 19.0-19.6: +(z,q)
358 m4_define([_AT_AMBIG_GLR_OUTPUT],
359 [[+(z,q)
360 <declare>(T,x)
361 <init-declare>(T,x,y)
362 =(x,y)
363 +(<cast>(x,T),y)
364 <OR>(<declare>(T,x),<cast>(x,T))
365 <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
366 <error>
367 +(z,q)
370 m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC],
371 [[3.0-3.6: +(z,q)
372 5.0-5.4: <declare>(T,x)
373 7.0-7.8: <init-declare>(T,x,y)
374 9.0-9.6: =(x,y)
375 11.0-11.10: +(<cast>(x,T),y)
376 13.0-13.6: <OR>(<declare>(T,x),<cast>(x,T))
377 15.0-15.14: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
378 17.0-17.16: <error>
379 19.0-19.6: +(z,q)
382 m4_define([_AT_GLR_STDERR],
383 [[syntax error
386 m4_define([_AT_GLR_STDERR_WITH_LOC],
387 [[17.5: syntax error
390 m4_define([_AT_VERBOSE_GLR_STDERR],
391 [[syntax error, unexpected ID, expecting '=' or '+' or ')'
394 m4_define([_AT_VERBOSE_GLR_STDERR_WITH_LOC],
395 [[17.5: syntax error, unexpected ID, expecting '=' or '+' or ')'
399 ## ---------------------------------------------------- ##
400 ## Compile the grammar described in the documentation.  ##
401 ## ---------------------------------------------------- ##
403 # AT_TEST([STDOUT], [STDERR])
404 m4_pushdef([AT_TEST],
405 [AT_PARSER_CHECK([[types test-input]],   0, [$1], [$2])
406 AT_PARSER_CHECK([[types -p test-input]], 0, [$1], [ignore])
409 AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
410 _AT_TEST_GLR_CXXTYPES([],
411                       [%dprec 1], [%dprec 2])
412 AT_TEST([_AT_RESOLVED_GLR_OUTPUT], [_AT_GLR_STDERR])
413 AT_CLEANUP
415 AT_SETUP([GLR: Resolve ambiguity, impure, locations])
416 _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
417 AT_TEST([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
418 AT_CLEANUP
420 AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
421 _AT_TEST_GLR_CXXTYPES([%define api.pure],
422                       [%dprec 1], [%dprec 2])
423 AT_TEST([_AT_RESOLVED_GLR_OUTPUT], [_AT_GLR_STDERR])
424 AT_CLEANUP
426 AT_SETUP([GLR: Resolve ambiguity, pure, locations])
427 _AT_TEST_GLR_CXXTYPES([%define api.pure %locations],
428                       [%dprec 1], [%dprec 2])
429 AT_TEST([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
430 AT_CLEANUP
432 AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
433 _AT_TEST_GLR_CXXTYPES([],
434                       [%merge <stmtMerge>], [%merge <stmtMerge>])
435 AT_TEST([_AT_AMBIG_GLR_OUTPUT], [_AT_GLR_STDERR])
436 AT_CLEANUP
438 AT_SETUP([GLR: Merge conflicting parses, impure, locations])
439 _AT_TEST_GLR_CXXTYPES([%locations],
440                       [%merge <stmtMerge>], [%merge <stmtMerge>])
441 AT_TEST([_AT_AMBIG_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
442 AT_CLEANUP
444 AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
445 _AT_TEST_GLR_CXXTYPES([%define api.pure],
446                       [%merge <stmtMerge>], [%merge <stmtMerge>])
447 AT_TEST([_AT_AMBIG_GLR_OUTPUT], [_AT_GLR_STDERR])
448 AT_CLEANUP
449 AT_SETUP([GLR: Merge conflicting parses, pure, locations])
450 _AT_TEST_GLR_CXXTYPES([%define api.pure %locations],
451                       [%merge <stmtMerge>],[%merge <stmtMerge>])
452 AT_TEST([_AT_AMBIG_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
453 AT_CLEANUP
455 AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
456 _AT_TEST_GLR_CXXTYPES([%define parse.error verbose],
457                       [%merge <stmtMerge>], [%merge <stmtMerge>])
458 AT_TEST([_AT_AMBIG_GLR_OUTPUT], [_AT_VERBOSE_GLR_STDERR])
459 AT_CLEANUP
461 m4_popdef([AT_TEST])