1 # Checking GLR Parsing. -*- Autotest -*-
2 # Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
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)
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.
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
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;
158 # define yylloc (*llocp)
159 # define yylval (*lvalp)
170 colNum = (colNum + 7) & ~7;
183 yylloc.first_line = yylloc.last_line = lineNum;
184 yylloc.first_column = colNum;
194 if (i == sizeof buffer - 1)
198 while (isalnum (c) || c == '_');
202 tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
203 yylval = new_term (strcpy ((char *) malloc (i), buffer));
212 yylloc.last_column = colNum-1;
221 yyerror (ERROR_PARAMETERS)
223 #if YYPURE && YYLSP_NEEDED
224 /* Pacify GCC by using llocp. */
228 fprintf (stderr, "%s\n", s);
232 new_nterm (char const *form, Node *child0, Node *child1, Node *child2)
234 Node *node = (Node *) malloc (sizeof (Node));
235 node->nterm.isNterm = 1;
236 node->nterm.parents = 0;
237 node->nterm.form = form;
238 node->nterm.children[0] = child0;
240 child0->nodeInfo.parents += 1;
241 node->nterm.children[1] = child1;
243 child1->nodeInfo.parents += 1;
244 node->nterm.children[2] = child2;
246 child2->nodeInfo.parents += 1;
251 new_term (char *text)
253 Node *node = (Node *) malloc (sizeof (Node));
254 node->term.isNterm = 0;
255 node->term.parents = 0;
256 node->term.text = text;
261 free_node (Node *node)
265 node->nodeInfo.parents -= 1;
266 /* Free only if 0 (last parent) or -1 (no parents). */
267 if (node->nodeInfo.parents > 0)
269 if (node->nodeInfo.isNterm == 1)
271 free_node (node->nterm.children[0]);
272 free_node (node->nterm.children[1]);
273 free_node (node->nterm.children[2]);
276 free (node->term.text);
281 node_to_string (Node *node)
289 buffer = (char *) malloc (1);
292 else if (node->nodeInfo.isNterm == 1)
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]);
297 buffer = (char *) malloc (strlen (node->nterm.form) + strlen (child0)
298 + strlen (child1) + strlen (child2) + 1);
299 sprintf (buffer, node->nterm.form, child0, child1, child2);
305 buffer = strdup (node->term.text);
310 m4_bmatch([$2], [stmtMerge],
312 stmtMerge (YYSTYPE x0, YYSTYPE x1)
314 return new_nterm ("<OR>(%s,%s)", x0, x1, 0);
319 AT_DATA([test-input],
342 This is total garbage, but it should be ignored.
345 AT_CHECK([bison -o types.c types.y], 0, [], ignore)
347 AT_BISON_OPTION_POPDEFS
350 m4_define([_AT_RESOLVED_GLR_OUTPUT],
353 <init-declare>(T,x,y)
357 <init-declare>(T,y,+(z,q))
362 m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC],
364 5.0-5.3: <declare>(T,x)
365 7.0-7.7: <init-declare>(T,x,y)
367 11.0-11.9: +(<cast>(x,T),y)
368 13.0-13.5: <declare>(T,x)
369 15.0-15.13: <init-declare>(T,y,+(z,q))
374 m4_define([_AT_AMBIG_GLR_OUTPUT],
377 <init-declare>(T,x,y)
380 <OR>(<declare>(T,x),<cast>(x,T))
381 <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
386 m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC],
388 5.0-5.3: <declare>(T,x)
389 7.0-7.7: <init-declare>(T,x,y)
391 11.0-11.9: +(<cast>(x,T),y)
392 13.0-13.5: <OR>(<declare>(T,x),<cast>(x,T))
393 15.0-15.13: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
398 m4_define([_AT_GLR_STDERR],
402 m4_define([_AT_VERBOSE_GLR_STDERR],
403 [[syntax error, unexpected ID, expecting '=' or '+' or ')'
406 ## ---------------------------------------------------- ##
407 ## Compile the grammar described in the documentation. ##
408 ## ---------------------------------------------------- ##
410 AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
411 _AT_TEST_GLR_CXXTYPES([],
412 [%dprec 1], [%dprec 2])
413 AT_PARSER_CHECK([[./types test-input]], 0,
414 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
417 AT_SETUP([GLR: Resolve ambiguity, impure, locations])
418 _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
419 AT_PARSER_CHECK([[./types test-input]], 0,
420 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
423 AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
424 _AT_TEST_GLR_CXXTYPES([%pure-parser],
425 [%dprec 1], [%dprec 2])
426 AT_PARSER_CHECK([[./types test-input]], 0,
427 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
430 AT_SETUP([GLR: Resolve ambiguity, pure, locations])
431 _AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
432 [%dprec 1], [%dprec 2])
433 AT_PARSER_CHECK([[./types test-input]], 0,
434 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
437 AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
438 _AT_TEST_GLR_CXXTYPES([],
439 [%merge <stmtMerge>], [%merge <stmtMerge>])
440 AT_PARSER_CHECK([[./types test-input]], 0,
441 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
444 AT_SETUP([GLR: Merge conflicting parses, impure, locations])
445 _AT_TEST_GLR_CXXTYPES([%locations],
446 [%merge <stmtMerge>], [%merge <stmtMerge>])
447 AT_PARSER_CHECK([[./types test-input]], 0,
448 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
451 AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
452 _AT_TEST_GLR_CXXTYPES([%pure-parser],
453 [%merge <stmtMerge>], [%merge <stmtMerge>])
454 AT_PARSER_CHECK([[./types test-input]], 0,
455 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
457 AT_SETUP([GLR: Merge conflicting parses, pure, locations])
458 _AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
459 [%merge <stmtMerge>],[%merge <stmtMerge>])
460 AT_PARSER_CHECK([[./types test-input]], 0,
461 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
464 AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
465 _AT_TEST_GLR_CXXTYPES([%error-verbose],
466 [%merge <stmtMerge>], [%merge <stmtMerge>])
467 AT_PARSER_CHECK([[./types test-input]], 0,
468 _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR)