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 3 of the License, or
7 # (at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
17 AT_BANNER([[C++ Type Syntax (GLR).]])
19 # _AT_TEST_GLR_CXXTYPES(DECL, RESOLVE1, RESOLVE2)
20 # -----------------------------------------------
21 # Store into types.y the calc program, with DECL inserted as a declaration,
22 # and with RESOLVE1 and RESOLVE2 as annotations on the conflicted rule for
23 # stmt. Then compile the result.
24 m4_define([_AT_TEST_GLR_CXXTYPES],
26 AT_BISON_OPTION_PUSHDEFS([$1])
28 AT_DATA_GRAMMAR([types.y],
29 [[/* Simplified C++ Type and Expression Grammar. */
44 union Node *children[3];
52 typedef union Node Node;
53 static Node *new_nterm (char const *, Node *, Node *, Node *);
54 static Node *new_term (char *);
55 static void free_node (Node *);
56 static char *node_to_string (Node *);
57 #define YYSTYPE Node *
58 ]m4_bmatch([$2], [stmtMerge],
59 [ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[
60 #define YYINITDEPTH 10
61 #define YYSTACKEXPANDABLE 1
65 # define LEX_PARAMETERS YYSTYPE *lvalp, struct YYLTYPE *llocp
66 # define ERROR_PARAMETERS struct YYLTYPE *llocp, char const *s
68 # define LEX_PARAMETERS YYSTYPE *lvalp
71 #ifndef LEX_PARAMETERS
72 # define LEX_PARAMETERS void
74 #ifndef ERROR_PARAMETERS
75 # define ERROR_PARAMETERS char const *s
77 int yylex (LEX_PARAMETERS);
78 void yyerror (ERROR_PARAMETERS);
88 %destructor { free_node ($$); } stmt expr decl declarator TYPENAME ID
94 char *output;]AT_LOCATION_IF([
95 printf ("%d.%d-%d.%d: ",
96 @2.first_line, @2.first_column,
97 @2.last_line, @2.last_column);])[
98 output = node_to_string (]$[2);
99 printf ("%s\n", output);
105 stmt : expr ';' $2 { $$ = ]$[1; }
107 | error ';' { $$ = new_nterm ("<error>", 0, 0, 0); }
112 | TYPENAME '(' expr ')'
113 { $$ = new_nterm ("<cast>(%s,%s)", ]$[3, ]$[1, 0); }
114 | expr '+' expr { $$ = new_nterm ("+(%s,%s)", ]$[1, ]$[3, 0); }
115 | expr '=' expr { $$ = new_nterm ("=(%s,%s)", ]$[1, ]$[3, 0); }
118 decl : TYPENAME declarator ';'
119 { $$ = new_nterm ("<declare>(%s,%s)", ]$[1, ]$[2, 0); }
120 | TYPENAME declarator '=' expr ';'
121 { $$ = new_nterm ("<init-declare>(%s,%s,%s)", ]$[1,
126 | '(' declarator ')' { $$ = ]$[2; }
137 main (int argc, char **argv)
141 if (!freopen (argv[1], "r", stdin))
147 yylex (LEX_PARAMETERS)
152 static int lineNum = 1;
153 static int colNum = 0;
157 # define yylloc (*llocp)
159 # define yylval (*lvalp)
172 colNum = (colNum + 7) & ~7;
185 yylloc.first_line = yylloc.last_line = lineNum;
186 yylloc.first_column = colNum;
196 if (i == sizeof buffer - 1)
200 while (isalnum (c) || c == '_');
204 tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
205 yylval = new_term (strcpy ((char *) malloc (i), buffer));
214 yylloc.last_column = colNum-1;
223 yyerror (ERROR_PARAMETERS)
225 #if YYPURE && YYLSP_NEEDED
226 /* Pacify GCC by using llocp. */
230 fprintf (stderr, "%s\n", s);
234 new_nterm (char const *form, Node *child0, Node *child1, Node *child2)
236 Node *node = (Node *) malloc (sizeof (Node));
237 node->nterm.isNterm = 1;
238 node->nterm.parents = 0;
239 node->nterm.form = form;
240 node->nterm.children[0] = child0;
242 child0->nodeInfo.parents += 1;
243 node->nterm.children[1] = child1;
245 child1->nodeInfo.parents += 1;
246 node->nterm.children[2] = child2;
248 child2->nodeInfo.parents += 1;
253 new_term (char *text)
255 Node *node = (Node *) malloc (sizeof (Node));
256 node->term.isNterm = 0;
257 node->term.parents = 0;
258 node->term.text = text;
263 free_node (Node *node)
267 node->nodeInfo.parents -= 1;
268 /* Free only if 0 (last parent) or -1 (no parents). */
269 if (node->nodeInfo.parents > 0)
271 if (node->nodeInfo.isNterm == 1)
273 free_node (node->nterm.children[0]);
274 free_node (node->nterm.children[1]);
275 free_node (node->nterm.children[2]);
278 free (node->term.text);
283 node_to_string (Node *node)
291 buffer = (char *) malloc (1);
294 else if (node->nodeInfo.isNterm == 1)
296 child0 = node_to_string (node->nterm.children[0]);
297 child1 = node_to_string (node->nterm.children[1]);
298 child2 = node_to_string (node->nterm.children[2]);
299 buffer = (char *) malloc (strlen (node->nterm.form) + strlen (child0)
300 + strlen (child1) + strlen (child2) + 1);
301 sprintf (buffer, node->nterm.form, child0, child1, child2);
307 buffer = strdup (node->term.text);
312 m4_bmatch([$2], [stmtMerge],
314 stmtMerge (YYSTYPE x0, YYSTYPE x1)
316 return new_nterm ("<OR>(%s,%s)", x0, x1, 0);
321 AT_DATA([test-input],
344 This is total garbage, but it should be ignored.
347 AT_CHECK([bison -o types.c types.y], 0, [], ignore)
349 AT_BISON_OPTION_POPDEFS
352 m4_define([_AT_RESOLVED_GLR_OUTPUT],
355 <init-declare>(T,x,y)
359 <init-declare>(T,y,+(z,q))
364 m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC],
366 5.0-5.3: <declare>(T,x)
367 7.0-7.7: <init-declare>(T,x,y)
369 11.0-11.9: +(<cast>(x,T),y)
370 13.0-13.5: <declare>(T,x)
371 15.0-15.13: <init-declare>(T,y,+(z,q))
376 m4_define([_AT_AMBIG_GLR_OUTPUT],
379 <init-declare>(T,x,y)
382 <OR>(<declare>(T,x),<cast>(x,T))
383 <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
388 m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC],
390 5.0-5.3: <declare>(T,x)
391 7.0-7.7: <init-declare>(T,x,y)
393 11.0-11.9: +(<cast>(x,T),y)
394 13.0-13.5: <OR>(<declare>(T,x),<cast>(x,T))
395 15.0-15.13: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
400 m4_define([_AT_GLR_STDERR],
404 m4_define([_AT_VERBOSE_GLR_STDERR],
405 [[syntax error, unexpected ID, expecting '=' or '+' or ')'
408 ## ---------------------------------------------------- ##
409 ## Compile the grammar described in the documentation. ##
410 ## ---------------------------------------------------- ##
412 AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
413 _AT_TEST_GLR_CXXTYPES([],
414 [%dprec 1], [%dprec 2])
415 AT_PARSER_CHECK([[./types test-input]], 0,
416 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
419 AT_SETUP([GLR: Resolve ambiguity, impure, locations])
420 _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
421 AT_PARSER_CHECK([[./types test-input]], 0,
422 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
425 AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
426 _AT_TEST_GLR_CXXTYPES([%pure-parser],
427 [%dprec 1], [%dprec 2])
428 AT_PARSER_CHECK([[./types test-input]], 0,
429 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
432 AT_SETUP([GLR: Resolve ambiguity, pure, locations])
433 _AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
434 [%dprec 1], [%dprec 2])
435 AT_PARSER_CHECK([[./types test-input]], 0,
436 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
439 AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
440 _AT_TEST_GLR_CXXTYPES([],
441 [%merge <stmtMerge>], [%merge <stmtMerge>])
442 AT_PARSER_CHECK([[./types test-input]], 0,
443 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
446 AT_SETUP([GLR: Merge conflicting parses, impure, locations])
447 _AT_TEST_GLR_CXXTYPES([%locations],
448 [%merge <stmtMerge>], [%merge <stmtMerge>])
449 AT_PARSER_CHECK([[./types test-input]], 0,
450 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
453 AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
454 _AT_TEST_GLR_CXXTYPES([%pure-parser],
455 [%merge <stmtMerge>], [%merge <stmtMerge>])
456 AT_PARSER_CHECK([[./types test-input]], 0,
457 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
459 AT_SETUP([GLR: Merge conflicting parses, pure, locations])
460 _AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
461 [%merge <stmtMerge>],[%merge <stmtMerge>])
462 AT_PARSER_CHECK([[./types test-input]], 0,
463 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
466 AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
467 _AT_TEST_GLR_CXXTYPES([%error-verbose],
468 [%merge <stmtMerge>], [%merge <stmtMerge>])
469 AT_PARSER_CHECK([[./types test-input]], 0,
470 _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR)