1 # Checking GLR Parsing. -*- Autotest -*-
3 # Copyright (C) 2002-2012 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 <http://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],
27 AT_BISON_OPTION_PUSHDEFS([$1])
29 AT_DATA_GRAMMAR([types.y],
30 [[/* Simplified C++ Type and Expression Grammar. */
45 union Node *children[3];
53 typedef union Node Node;
54 static Node *new_nterm (char const *, Node *, Node *, Node *);
55 static Node *new_term (char *);
56 static void free_node (Node *);
57 static char *node_to_string (Node *);
58 #define YYSTYPE Node *
59 ]m4_bmatch([$2], [stmtMerge],
60 [ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[
61 #define YYINITDEPTH 10
62 #define YYSTACKEXPANDABLE 1
66 # define LEX_PARAMETERS YYSTYPE *lvalp, struct YYLTYPE *llocp
67 # define ERROR_PARAMETERS struct YYLTYPE *llocp, char const *s
69 # define LEX_PARAMETERS YYSTYPE *lvalp
72 #ifndef LEX_PARAMETERS
73 # define LEX_PARAMETERS void
75 #ifndef ERROR_PARAMETERS
76 # define ERROR_PARAMETERS char const *s
78 int yylex (LEX_PARAMETERS);
79 void yyerror (ERROR_PARAMETERS);
89 %destructor { free_node ($$); } stmt expr decl declarator TYPENAME ID
95 char *output;]AT_LOCATION_IF([
96 printf ("%d.%d-%d.%d: ",
97 @2.first_line, @2.first_column,
98 @2.last_line, @2.last_column);])[
99 output = node_to_string (]$[2);
100 printf ("%s\n", output);
106 stmt : expr ';' $2 { $$ = ]$[1; }
108 | error ';' { $$ = new_nterm ("<error>", 0, 0, 0); }
113 | TYPENAME '(' expr ')'
114 { $$ = new_nterm ("<cast>(%s,%s)", ]$[3, ]$[1, 0); }
115 | expr '+' expr { $$ = new_nterm ("+(%s,%s)", ]$[1, ]$[3, 0); }
116 | expr '=' expr { $$ = new_nterm ("=(%s,%s)", ]$[1, ]$[3, 0); }
119 decl : TYPENAME declarator ';'
120 { $$ = new_nterm ("<declare>(%s,%s)", ]$[1, ]$[2, 0); }
121 | TYPENAME declarator '=' expr ';'
122 { $$ = new_nterm ("<init-declare>(%s,%s,%s)", ]$[1,
127 | '(' declarator ')' { $$ = ]$[2; }
138 main (int argc, char **argv)
142 if (!freopen (argv[1], "r", stdin))
148 yylex (LEX_PARAMETERS)
153 static int lineNum = 1;
154 static int colNum = 0;
158 # define yylloc (*llocp)
160 # define yylval (*lvalp)
173 colNum = (colNum + 7) & ~7;
186 yylloc.first_line = yylloc.last_line = lineNum;
187 yylloc.first_column = colNum;
197 if (i == sizeof buffer - 1)
201 while (isalnum (c) || c == '_');
205 tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
206 yylval = new_term (strcpy ((char *) malloc (i), buffer));
215 yylloc.last_column = colNum-1;
224 yyerror (ERROR_PARAMETERS)
226 #if YYPURE && YYLSP_NEEDED
227 /* Pacify GCC by using llocp. */
231 fprintf (stderr, "%s\n", s);
235 new_nterm (char const *form, Node *child0, Node *child1, Node *child2)
237 Node *node = (Node *) malloc (sizeof (Node));
238 node->nterm.isNterm = 1;
239 node->nterm.parents = 0;
240 node->nterm.form = form;
241 node->nterm.children[0] = child0;
243 child0->nodeInfo.parents += 1;
244 node->nterm.children[1] = child1;
246 child1->nodeInfo.parents += 1;
247 node->nterm.children[2] = child2;
249 child2->nodeInfo.parents += 1;
254 new_term (char *text)
256 Node *node = (Node *) malloc (sizeof (Node));
257 node->term.isNterm = 0;
258 node->term.parents = 0;
259 node->term.text = text;
264 free_node (Node *node)
268 node->nodeInfo.parents -= 1;
269 /* Free only if 0 (last parent) or -1 (no parents). */
270 if (node->nodeInfo.parents > 0)
272 if (node->nodeInfo.isNterm == 1)
274 free_node (node->nterm.children[0]);
275 free_node (node->nterm.children[1]);
276 free_node (node->nterm.children[2]);
279 free (node->term.text);
284 node_to_string (Node *node)
292 buffer = (char *) malloc (1);
295 else if (node->nodeInfo.isNterm == 1)
297 child0 = node_to_string (node->nterm.children[0]);
298 child1 = node_to_string (node->nterm.children[1]);
299 child2 = node_to_string (node->nterm.children[2]);
300 buffer = (char *) malloc (strlen (node->nterm.form) + strlen (child0)
301 + strlen (child1) + strlen (child2) + 1);
302 sprintf (buffer, node->nterm.form, child0, child1, child2);
308 buffer = strdup (node->term.text);
313 m4_bmatch([$2], [stmtMerge],
315 stmtMerge (YYSTYPE x0, YYSTYPE x1)
317 return new_nterm ("<OR>(%s,%s)", x0, x1, 0);
322 AT_DATA([test-input],
345 This is total garbage, but it should be ignored.
348 AT_BISON_CHECK([-o types.c types.y], 0, [], ignore)
350 AT_BISON_OPTION_POPDEFS
353 m4_define([_AT_RESOLVED_GLR_OUTPUT],
356 <init-declare>(T,x,y)
360 <init-declare>(T,y,+(z,q))
365 m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC],
367 5.0-5.3: <declare>(T,x)
368 7.0-7.7: <init-declare>(T,x,y)
370 11.0-11.9: +(<cast>(x,T),y)
371 13.0-13.5: <declare>(T,x)
372 15.0-15.13: <init-declare>(T,y,+(z,q))
377 m4_define([_AT_AMBIG_GLR_OUTPUT],
380 <init-declare>(T,x,y)
383 <OR>(<declare>(T,x),<cast>(x,T))
384 <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
389 m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC],
391 5.0-5.3: <declare>(T,x)
392 7.0-7.7: <init-declare>(T,x,y)
394 11.0-11.9: +(<cast>(x,T),y)
395 13.0-13.5: <OR>(<declare>(T,x),<cast>(x,T))
396 15.0-15.13: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
401 m4_define([_AT_GLR_STDERR],
405 m4_define([_AT_VERBOSE_GLR_STDERR],
406 [[[syntax error, unexpected ID, expecting '=' or '+' or ')'
409 ## ---------------------------------------------------- ##
410 ## Compile the grammar described in the documentation. ##
411 ## ---------------------------------------------------- ##
413 AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
414 _AT_TEST_GLR_CXXTYPES([],
415 [%dprec 1], [%dprec 2])
416 AT_PARSER_CHECK([[./types test-input]], 0,
417 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
420 AT_SETUP([GLR: Resolve ambiguity, impure, locations])
421 _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
422 AT_PARSER_CHECK([[./types test-input]], 0,
423 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
426 AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
427 _AT_TEST_GLR_CXXTYPES([%define api.pure],
428 [%dprec 1], [%dprec 2])
429 AT_PARSER_CHECK([[./types test-input]], 0,
430 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
433 AT_SETUP([GLR: Resolve ambiguity, pure, locations])
434 _AT_TEST_GLR_CXXTYPES([%define api.pure %locations],
435 [%dprec 1], [%dprec 2])
436 AT_PARSER_CHECK([[./types test-input]], 0,
437 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
440 AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
441 _AT_TEST_GLR_CXXTYPES([],
442 [%merge <stmtMerge>], [%merge <stmtMerge>])
443 AT_PARSER_CHECK([[./types test-input]], 0,
444 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
447 AT_SETUP([GLR: Merge conflicting parses, impure, locations])
448 _AT_TEST_GLR_CXXTYPES([%locations],
449 [%merge <stmtMerge>], [%merge <stmtMerge>])
450 AT_PARSER_CHECK([[./types test-input]], 0,
451 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
454 AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
455 _AT_TEST_GLR_CXXTYPES([%define api.pure],
456 [%merge <stmtMerge>], [%merge <stmtMerge>])
457 AT_PARSER_CHECK([[./types test-input]], 0,
458 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
460 AT_SETUP([GLR: Merge conflicting parses, pure, locations])
461 _AT_TEST_GLR_CXXTYPES([%define api.pure %locations],
462 [%merge <stmtMerge>],[%merge <stmtMerge>])
463 AT_PARSER_CHECK([[./types test-input]], 0,
464 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
467 AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
468 _AT_TEST_GLR_CXXTYPES([%error-verbose],
469 [%merge <stmtMerge>], [%merge <stmtMerge>])
470 AT_PARSER_CHECK([[./types test-input]], 0,
471 _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR)