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. */
46 union Node *children[3];
54 typedef union Node Node;
55 #define YYSTYPE Node *
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], [stmt_merge],
65 [ static YYSTYPE stmt_merge (YYSTYPE x0, YYSTYPE x1);])[
66 #define YYINITDEPTH 10
67 #define YYSTACKEXPANDABLE 1
79 %destructor { free_node ($$); } stmt expr decl declarator TYPENAME ID
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);
96 stmt : expr ';' $2 { $$ = ]$[1; }
98 | error ';' { $$ = new_nterm ("<error>", YY_NULLPTR, YY_NULLPTR, YY_NULLPTR); }
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); }
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,
117 | '(' declarator ')' { $$ = ]$[2; }
129 main (int argc, char **argv)
131 if (getenv ("YYDEBUG"))
133 for (int i = 1; i < argc; ++i)
134 // Enable parse traces on option -p.
135 if (strcmp (argv[i], "-p") == 0)
139 if (!freopen (argv[i], "r", stdin))
141 int status = yyparse ();
152 static int lineNum = 1;
153 static int colNum = 0;
157 # define yylloc (*llocp)
159 # define yylval (*lvalp)
165 assert (!feof (stdin));
172 colNum = (colNum + 7) & ~7;
183 int tok;]AT_LOCATION_IF([[
184 yylloc.first_line = yylloc.last_line = lineNum;
185 yylloc.first_column = colNum;]])[
193 buffer[i++] = YY_CAST (char, c);
195 assert (i != sizeof buffer - 1);
198 while (isalnum (c) || c == '_');
202 tok = isupper (YY_CAST (unsigned char, buffer[0])) ? TYPENAME : ID;
203 yylval = new_term (strcpy (YY_CAST (char *, malloc (i)), buffer));
211 yylloc.last_column = colNum;]])[
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;
227 child0->nodeInfo.parents += 1;
228 node->nterm.children[1] = child1;
230 child1->nodeInfo.parents += 1;
231 node->nterm.children[2] = child2;
233 child2->nodeInfo.parents += 1;
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;
248 free_node (Node *node)
252 node->nodeInfo.parents -= 1;
253 /* Free only if 0 (last parent) or -1 (no parents). */
254 if (node->nodeInfo.parents > 0)
256 if (node->nodeInfo.isNterm == 1)
258 free_node (node->nterm.children[0]);
259 free_node (node->nterm.children[1]);
260 free_node (node->nterm.children[2]);
263 free (node->term.text);
268 node_to_string (Node *node)
273 res = YY_CAST (char *, malloc (1));
276 else if (node->nodeInfo.isNterm == 1)
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);
289 res = strdup (node->term.text);
294 m4_bmatch([$2], [stmt_merge],
296 stmt_merge (YYSTYPE x0, YYSTYPE x1)
298 return new_nterm ("<OR>(%s,%s)", x0, x1, YY_NULLPTR);
303 AT_DATA([test-input],
326 This is total garbage, but it should be ignored.
329 AT_BISON_CHECK([-o types.c types.y], 0, [], ignore)
331 AT_BISON_OPTION_POPDEFS
334 m4_define([_AT_RESOLVED_GLR_OUTPUT],
337 <init-declare>(T,x,y)
341 <init-declare>(T,y,+(z,q))
346 m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC],
348 5.0-5.4: <declare>(T,x)
349 7.0-7.8: <init-declare>(T,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))
358 m4_define([_AT_AMBIG_GLR_OUTPUT],
361 <init-declare>(T,x,y)
364 <OR>(<declare>(T,x),<cast>(x,T))
365 <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
370 m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC],
372 5.0-5.4: <declare>(T,x)
373 7.0-7.8: <init-declare>(T,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)))
382 m4_define([_AT_GLR_STDERR],
386 m4_define([_AT_GLR_STDERR_WITH_LOC],
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])
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])
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])
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])
432 AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
433 _AT_TEST_GLR_CXXTYPES([],
434 [%merge <stmt_merge>], [%merge <stmt_merge>])
435 AT_TEST([_AT_AMBIG_GLR_OUTPUT], [_AT_GLR_STDERR])
438 AT_SETUP([GLR: Merge conflicting parses, impure, locations])
439 _AT_TEST_GLR_CXXTYPES([%locations],
440 [%merge <stmt_merge>], [%merge <stmt_merge>])
441 AT_TEST([_AT_AMBIG_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
444 AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
445 _AT_TEST_GLR_CXXTYPES([%define api.pure],
446 [%merge <stmt_merge>], [%merge <stmt_merge>])
447 AT_TEST([_AT_AMBIG_GLR_OUTPUT], [_AT_GLR_STDERR])
449 AT_SETUP([GLR: Merge conflicting parses, pure, locations])
450 _AT_TEST_GLR_CXXTYPES([%define api.pure %locations],
451 [%merge <stmt_merge>],[%merge <stmt_merge>])
452 AT_TEST([_AT_AMBIG_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
455 AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
456 _AT_TEST_GLR_CXXTYPES([%define parse.error verbose],
457 [%merge <stmt_merge>], [%merge <stmt_merge>])
458 AT_TEST([_AT_AMBIG_GLR_OUTPUT], [_AT_VERBOSE_GLR_STDERR])