1 # Checking GLR Parsing. -*- Autotest -*-
3 # Copyright (C) 2002-2015 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],
26 [AT_BISON_OPTION_PUSHDEFS([%glr-parser $1])
28 AT_DATA_GRAMMAR([types.y],
29 [[/* Simplified C++ Type and Expression Grammar. */
45 union Node *children[3];
53 typedef union Node Node;
54 #define YYSTYPE Node *
59 static Node *new_nterm (char const *, Node *, Node *, Node *);
60 static Node *new_term (char *);
61 static void free_node (Node *);
62 static char *node_to_string (Node *);
63 ]m4_bmatch([$2], [stmtMerge],
64 [ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[
65 #define YYINITDEPTH 10
66 #define YYSTACKEXPANDABLE 1
78 %destructor { free_node ($$); } stmt expr decl declarator TYPENAME ID
84 char *output;]AT_LOCATION_IF([
85 printf ("%d.%d-%d.%d: ",
86 @2.first_line, @2.first_column,
87 @2.last_line, @2.last_column);])[
88 output = node_to_string (]$[2);
89 printf ("%s\n", output);
95 stmt : expr ';' $2 { $$ = ]$[1; }
97 | error ';' { $$ = new_nterm ("<error>", YY_NULLPTR, YY_NULLPTR, YY_NULLPTR); }
102 | TYPENAME '(' expr ')'
103 { $$ = new_nterm ("<cast>(%s,%s)", ]$[3, ]$[1, YY_NULLPTR); }
104 | expr '+' expr { $$ = new_nterm ("+(%s,%s)", ]$[1, ]$[3, YY_NULLPTR); }
105 | expr '=' expr { $$ = new_nterm ("=(%s,%s)", ]$[1, ]$[3, YY_NULLPTR); }
108 decl : TYPENAME declarator ';'
109 { $$ = new_nterm ("<declare>(%s,%s)", ]$[1, ]$[2, YY_NULLPTR); }
110 | TYPENAME declarator '=' expr ';'
111 { $$ = new_nterm ("<init-declare>(%s,%s,%s)", ]$[1,
116 | '(' declarator ')' { $$ = ]$[2; }
128 main (int argc, char **argv)
130 assert (argc == 2); (void) argc;
131 if (!freopen (argv[1], "r", stdin))
143 static int lineNum = 1;
144 static int colNum = 0;
148 # define yylloc (*llocp)
150 # define yylval (*lvalp)
155 assert (!feof (stdin));
162 colNum = (colNum + 7) & ~7;
173 int tok;]AT_LOCATION_IF([[
174 yylloc.first_line = yylloc.last_line = lineNum;
175 yylloc.first_column = colNum;]])[
184 assert (i != sizeof buffer - 1);
187 while (isalnum (c) || c == '_');
191 tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
192 yylval = new_term (strcpy ((char *) malloc (i), buffer));
200 yylloc.last_column = colNum-1;]])[
208 new_nterm (char const *form, Node *child0, Node *child1, Node *child2)
210 Node *node = (Node *) malloc (sizeof (Node));
211 node->nterm.isNterm = 1;
212 node->nterm.parents = 0;
213 node->nterm.form = form;
214 node->nterm.children[0] = child0;
216 child0->nodeInfo.parents += 1;
217 node->nterm.children[1] = child1;
219 child1->nodeInfo.parents += 1;
220 node->nterm.children[2] = child2;
222 child2->nodeInfo.parents += 1;
227 new_term (char *text)
229 Node *node = (Node *) malloc (sizeof (Node));
230 node->term.isNterm = 0;
231 node->term.parents = 0;
232 node->term.text = text;
237 free_node (Node *node)
241 node->nodeInfo.parents -= 1;
242 /* Free only if 0 (last parent) or -1 (no parents). */
243 if (node->nodeInfo.parents > 0)
245 if (node->nodeInfo.isNterm == 1)
247 free_node (node->nterm.children[0]);
248 free_node (node->nterm.children[1]);
249 free_node (node->nterm.children[2]);
252 free (node->term.text);
257 node_to_string (Node *node)
265 buffer = (char *) malloc (1);
268 else if (node->nodeInfo.isNterm == 1)
270 child0 = node_to_string (node->nterm.children[0]);
271 child1 = node_to_string (node->nterm.children[1]);
272 child2 = node_to_string (node->nterm.children[2]);
273 buffer = (char *) malloc (strlen (node->nterm.form) + strlen (child0)
274 + strlen (child1) + strlen (child2) + 1);
275 sprintf (buffer, node->nterm.form, child0, child1, child2);
281 buffer = strdup (node->term.text);
286 m4_bmatch([$2], [stmtMerge],
288 stmtMerge (YYSTYPE x0, YYSTYPE x1)
290 return new_nterm ("<OR>(%s,%s)", x0, x1, YY_NULLPTR);
295 AT_DATA([test-input],
318 This is total garbage, but it should be ignored.
321 AT_BISON_CHECK([-o types.c types.y], 0, [], ignore)
323 AT_BISON_OPTION_POPDEFS
326 m4_define([_AT_RESOLVED_GLR_OUTPUT],
329 <init-declare>(T,x,y)
333 <init-declare>(T,y,+(z,q))
338 m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC],
340 5.0-5.3: <declare>(T,x)
341 7.0-7.7: <init-declare>(T,x,y)
343 11.0-11.9: +(<cast>(x,T),y)
344 13.0-13.5: <declare>(T,x)
345 15.0-15.13: <init-declare>(T,y,+(z,q))
350 m4_define([_AT_AMBIG_GLR_OUTPUT],
353 <init-declare>(T,x,y)
356 <OR>(<declare>(T,x),<cast>(x,T))
357 <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
362 m4_define([_AT_AMBIG_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: <OR>(<declare>(T,x),<cast>(x,T))
369 15.0-15.13: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
374 m4_define([_AT_GLR_STDERR],
378 m4_define([_AT_GLR_STDERR_WITH_LOC],
382 m4_define([_AT_VERBOSE_GLR_STDERR],
383 [[syntax error, unexpected ID, expecting '=' or '+' or ')'
386 m4_define([_AT_VERBOSE_GLR_STDERR_WITH_LOC],
387 [[17.5: syntax error, unexpected ID, expecting '=' or '+' or ')'
390 ## ---------------------------------------------------- ##
391 ## Compile the grammar described in the documentation. ##
392 ## ---------------------------------------------------- ##
394 AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
395 _AT_TEST_GLR_CXXTYPES([],
396 [%dprec 1], [%dprec 2])
397 AT_PARSER_CHECK([[./types test-input]], 0,
398 [_AT_RESOLVED_GLR_OUTPUT], [_AT_GLR_STDERR])
401 AT_SETUP([GLR: Resolve ambiguity, impure, locations])
402 _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
403 AT_PARSER_CHECK([[./types test-input]], 0,
404 [_AT_RESOLVED_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
407 AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
408 _AT_TEST_GLR_CXXTYPES([%define api.pure],
409 [%dprec 1], [%dprec 2])
410 AT_PARSER_CHECK([[./types test-input]], 0,
411 [_AT_RESOLVED_GLR_OUTPUT], [_AT_GLR_STDERR])
414 AT_SETUP([GLR: Resolve ambiguity, pure, locations])
415 _AT_TEST_GLR_CXXTYPES([%define api.pure %locations],
416 [%dprec 1], [%dprec 2])
417 AT_PARSER_CHECK([[./types test-input]], 0,
418 [_AT_RESOLVED_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
421 AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
422 _AT_TEST_GLR_CXXTYPES([],
423 [%merge <stmtMerge>], [%merge <stmtMerge>])
424 AT_PARSER_CHECK([[./types test-input]], 0,
425 [_AT_AMBIG_GLR_OUTPUT], [_AT_GLR_STDERR])
428 AT_SETUP([GLR: Merge conflicting parses, impure, locations])
429 _AT_TEST_GLR_CXXTYPES([%locations],
430 [%merge <stmtMerge>], [%merge <stmtMerge>])
431 AT_PARSER_CHECK([[./types test-input]], 0,
432 [_AT_AMBIG_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
435 AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
436 _AT_TEST_GLR_CXXTYPES([%define api.pure],
437 [%merge <stmtMerge>], [%merge <stmtMerge>])
438 AT_PARSER_CHECK([[./types test-input]], 0,
439 [_AT_AMBIG_GLR_OUTPUT], [_AT_GLR_STDERR])
441 AT_SETUP([GLR: Merge conflicting parses, pure, locations])
442 _AT_TEST_GLR_CXXTYPES([%define api.pure %locations],
443 [%merge <stmtMerge>],[%merge <stmtMerge>])
444 AT_PARSER_CHECK([[./types test-input]], 0,
445 [_AT_AMBIG_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
448 AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
449 _AT_TEST_GLR_CXXTYPES([%error-verbose],
450 [%merge <stmtMerge>], [%merge <stmtMerge>])
451 AT_PARSER_CHECK([[./types test-input]], 0,
452 [_AT_AMBIG_GLR_OUTPUT], [_AT_VERBOSE_GLR_STDERR])