1 # Checking GLR Parsing. -*- Autotest -*-
3 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 # Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 AT_BANNER([[C++ Type Syntax (GLR).]])
21 # _AT_TEST_GLR_CXXTYPES(DECL, RESOLVE1, RESOLVE2)
22 # -----------------------------------------------
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.
26 m4_define([_AT_TEST_GLR_CXXTYPES],
28 AT_BISON_OPTION_PUSHDEFS([$1])
30 AT_DATA_GRAMMAR([types.y],
31 [[/* Simplified C++ Type and Expression Grammar. */
46 union Node *children[3];
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 *
60 ]m4_bmatch([$2], [stmtMerge],
61 [ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[
62 #define YYINITDEPTH 10
63 #define YYSTACKEXPANDABLE 1
67 # define LEX_PARAMETERS YYSTYPE *lvalp, struct YYLTYPE *llocp
68 # define ERROR_PARAMETERS struct YYLTYPE *llocp, char const *s
70 # define LEX_PARAMETERS YYSTYPE *lvalp
73 #ifndef LEX_PARAMETERS
74 # define LEX_PARAMETERS void
76 #ifndef ERROR_PARAMETERS
77 # define ERROR_PARAMETERS char const *s
79 int yylex (LEX_PARAMETERS);
80 void yyerror (ERROR_PARAMETERS);
90 %destructor { free_node ($$); } stmt expr decl declarator TYPENAME ID
96 char *output;]AT_LOCATION_IF([
97 printf ("%d.%d-%d.%d: ",
98 @2.first_line, @2.first_column,
99 @2.last_line, @2.last_column);])[
100 output = node_to_string (]$[2);
101 printf ("%s\n", output);
107 stmt : expr ';' $2 { $$ = ]$[1; }
109 | error ';' { $$ = new_nterm ("<error>", 0, 0, 0); }
114 | TYPENAME '(' expr ')'
115 { $$ = new_nterm ("<cast>(%s,%s)", ]$[3, ]$[1, 0); }
116 | expr '+' expr { $$ = new_nterm ("+(%s,%s)", ]$[1, ]$[3, 0); }
117 | expr '=' expr { $$ = new_nterm ("=(%s,%s)", ]$[1, ]$[3, 0); }
120 decl : TYPENAME declarator ';'
121 { $$ = new_nterm ("<declare>(%s,%s)", ]$[1, ]$[2, 0); }
122 | TYPENAME declarator '=' expr ';'
123 { $$ = new_nterm ("<init-declare>(%s,%s,%s)", ]$[1,
128 | '(' declarator ')' { $$ = ]$[2; }
139 main (int argc, char **argv)
143 if (!freopen (argv[1], "r", stdin))
149 yylex (LEX_PARAMETERS)
154 static int lineNum = 1;
155 static int colNum = 0;
159 # define yylloc (*llocp)
161 # define yylval (*lvalp)
174 colNum = (colNum + 7) & ~7;
187 yylloc.first_line = yylloc.last_line = lineNum;
188 yylloc.first_column = colNum;
198 if (i == sizeof buffer - 1)
202 while (isalnum (c) || c == '_');
206 tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
207 yylval = new_term (strcpy ((char *) malloc (i), buffer));
216 yylloc.last_column = colNum-1;
225 yyerror (ERROR_PARAMETERS)
227 #if YYPURE && YYLSP_NEEDED
228 /* Pacify GCC by using llocp. */
232 fprintf (stderr, "%s\n", s);
236 new_nterm (char const *form, Node *child0, Node *child1, Node *child2)
238 Node *node = (Node *) malloc (sizeof (Node));
239 node->nterm.isNterm = 1;
240 node->nterm.parents = 0;
241 node->nterm.form = form;
242 node->nterm.children[0] = child0;
244 child0->nodeInfo.parents += 1;
245 node->nterm.children[1] = child1;
247 child1->nodeInfo.parents += 1;
248 node->nterm.children[2] = child2;
250 child2->nodeInfo.parents += 1;
255 new_term (char *text)
257 Node *node = (Node *) malloc (sizeof (Node));
258 node->term.isNterm = 0;
259 node->term.parents = 0;
260 node->term.text = text;
265 free_node (Node *node)
269 node->nodeInfo.parents -= 1;
270 /* Free only if 0 (last parent) or -1 (no parents). */
271 if (node->nodeInfo.parents > 0)
273 if (node->nodeInfo.isNterm == 1)
275 free_node (node->nterm.children[0]);
276 free_node (node->nterm.children[1]);
277 free_node (node->nterm.children[2]);
280 free (node->term.text);
285 node_to_string (Node *node)
293 buffer = (char *) malloc (1);
296 else if (node->nodeInfo.isNterm == 1)
298 child0 = node_to_string (node->nterm.children[0]);
299 child1 = node_to_string (node->nterm.children[1]);
300 child2 = node_to_string (node->nterm.children[2]);
301 buffer = (char *) malloc (strlen (node->nterm.form) + strlen (child0)
302 + strlen (child1) + strlen (child2) + 1);
303 sprintf (buffer, node->nterm.form, child0, child1, child2);
309 buffer = strdup (node->term.text);
314 m4_bmatch([$2], [stmtMerge],
316 stmtMerge (YYSTYPE x0, YYSTYPE x1)
318 return new_nterm ("<OR>(%s,%s)", x0, x1, 0);
323 AT_DATA([test-input],
346 This is total garbage, but it should be ignored.
349 AT_BISON_CHECK([-o types.c types.y], 0, [], ignore)
351 AT_BISON_OPTION_POPDEFS
354 m4_define([_AT_RESOLVED_GLR_OUTPUT],
357 <init-declare>(T,x,y)
361 <init-declare>(T,y,+(z,q))
366 m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC],
368 5.0-5.3: <declare>(T,x)
369 7.0-7.7: <init-declare>(T,x,y)
371 11.0-11.9: +(<cast>(x,T),y)
372 13.0-13.5: <declare>(T,x)
373 15.0-15.13: <init-declare>(T,y,+(z,q))
378 m4_define([_AT_AMBIG_GLR_OUTPUT],
381 <init-declare>(T,x,y)
384 <OR>(<declare>(T,x),<cast>(x,T))
385 <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
390 m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC],
392 5.0-5.3: <declare>(T,x)
393 7.0-7.7: <init-declare>(T,x,y)
395 11.0-11.9: +(<cast>(x,T),y)
396 13.0-13.5: <OR>(<declare>(T,x),<cast>(x,T))
397 15.0-15.13: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
402 m4_define([_AT_GLR_STDERR],
406 m4_define([_AT_VERBOSE_GLR_STDERR],
407 [[[syntax error, unexpected ID, expecting '=' or '+' or ')'
410 ## ---------------------------------------------------- ##
411 ## Compile the grammar described in the documentation. ##
412 ## ---------------------------------------------------- ##
414 AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
415 _AT_TEST_GLR_CXXTYPES([],
416 [%dprec 1], [%dprec 2])
417 AT_PARSER_CHECK([[./types test-input]], 0,
418 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
421 AT_SETUP([GLR: Resolve ambiguity, impure, locations])
422 _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
423 AT_PARSER_CHECK([[./types test-input]], 0,
424 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
427 AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
428 _AT_TEST_GLR_CXXTYPES([%define api.pure],
429 [%dprec 1], [%dprec 2])
430 AT_PARSER_CHECK([[./types test-input]], 0,
431 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
434 AT_SETUP([GLR: Resolve ambiguity, pure, locations])
435 _AT_TEST_GLR_CXXTYPES([%define api.pure %locations],
436 [%dprec 1], [%dprec 2])
437 AT_PARSER_CHECK([[./types test-input]], 0,
438 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
441 AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
442 _AT_TEST_GLR_CXXTYPES([],
443 [%merge <stmtMerge>], [%merge <stmtMerge>])
444 AT_PARSER_CHECK([[./types test-input]], 0,
445 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
448 AT_SETUP([GLR: Merge conflicting parses, impure, locations])
449 _AT_TEST_GLR_CXXTYPES([%locations],
450 [%merge <stmtMerge>], [%merge <stmtMerge>])
451 AT_PARSER_CHECK([[./types test-input]], 0,
452 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
455 AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
456 _AT_TEST_GLR_CXXTYPES([%define api.pure],
457 [%merge <stmtMerge>], [%merge <stmtMerge>])
458 AT_PARSER_CHECK([[./types test-input]], 0,
459 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
461 AT_SETUP([GLR: Merge conflicting parses, pure, locations])
462 _AT_TEST_GLR_CXXTYPES([%define api.pure %locations],
463 [%merge <stmtMerge>],[%merge <stmtMerge>])
464 AT_PARSER_CHECK([[./types test-input]], 0,
465 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
468 AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
469 _AT_TEST_GLR_CXXTYPES([%error-verbose],
470 [%merge <stmtMerge>], [%merge <stmtMerge>])
471 AT_PARSER_CHECK([[./types test-input]], 0,
472 _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR)