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],
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_NULL, YY_NULL, YY_NULL); }
102 | TYPENAME '(' expr ')'
103 { $$ = new_nterm ("<cast>(%s,%s)", ]$[3, ]$[1, YY_NULL); }
104 | expr '+' expr { $$ = new_nterm ("+(%s,%s)", ]$[1, ]$[3, YY_NULL); }
105 | expr '=' expr { $$ = new_nterm ("=(%s,%s)", ]$[1, ]$[3, YY_NULL); }
108 decl : TYPENAME declarator ';'
109 { $$ = new_nterm ("<declare>(%s,%s)", ]$[1, ]$[2, YY_NULL); }
110 | TYPENAME declarator '=' expr ';'
111 { $$ = new_nterm ("<init-declare>(%s,%s,%s)", ]$[1,
116 | '(' declarator ')' { $$ = ]$[2; }
127 main (int argc, char **argv)
131 if (!freopen (argv[1], "r", stdin))
139 yylex (]AT_LEX_FORMALS[)
144 static int lineNum = 1;
145 static int colNum = 0;
149 # define yylloc (*llocp)
151 # define yylval (*lvalp)
164 colNum = (colNum + 7) & ~7;
175 int tok;]AT_LOCATION_IF([[
176 yylloc.first_line = yylloc.last_line = lineNum;
177 yylloc.first_column = colNum;]])[
186 if (i == sizeof buffer - 1)
190 while (isalnum (c) || c == '_');
194 tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
195 yylval = new_term (strcpy ((char *) malloc (i), buffer));
203 yylloc.last_column = colNum-1;]])[
211 new_nterm (char const *form, Node *child0, Node *child1, Node *child2)
213 Node *node = (Node *) malloc (sizeof (Node));
214 node->nterm.isNterm = 1;
215 node->nterm.parents = 0;
216 node->nterm.form = form;
217 node->nterm.children[0] = child0;
219 child0->nodeInfo.parents += 1;
220 node->nterm.children[1] = child1;
222 child1->nodeInfo.parents += 1;
223 node->nterm.children[2] = child2;
225 child2->nodeInfo.parents += 1;
230 new_term (char *text)
232 Node *node = (Node *) malloc (sizeof (Node));
233 node->term.isNterm = 0;
234 node->term.parents = 0;
235 node->term.text = text;
240 free_node (Node *node)
244 node->nodeInfo.parents -= 1;
245 /* Free only if 0 (last parent) or -1 (no parents). */
246 if (node->nodeInfo.parents > 0)
248 if (node->nodeInfo.isNterm == 1)
250 free_node (node->nterm.children[0]);
251 free_node (node->nterm.children[1]);
252 free_node (node->nterm.children[2]);
255 free (node->term.text);
260 node_to_string (Node *node)
268 buffer = (char *) malloc (1);
271 else if (node->nodeInfo.isNterm == 1)
273 child0 = node_to_string (node->nterm.children[0]);
274 child1 = node_to_string (node->nterm.children[1]);
275 child2 = node_to_string (node->nterm.children[2]);
276 buffer = (char *) malloc (strlen (node->nterm.form) + strlen (child0)
277 + strlen (child1) + strlen (child2) + 1);
278 sprintf (buffer, node->nterm.form, child0, child1, child2);
284 buffer = strdup (node->term.text);
289 m4_bmatch([$2], [stmtMerge],
291 stmtMerge (YYSTYPE x0, YYSTYPE x1)
293 return new_nterm ("<OR>(%s,%s)", x0, x1, YY_NULL);
298 AT_DATA([test-input],
321 This is total garbage, but it should be ignored.
324 AT_BISON_CHECK([-o types.c types.y], 0, [], ignore)
326 AT_BISON_OPTION_POPDEFS
329 m4_define([_AT_RESOLVED_GLR_OUTPUT],
332 <init-declare>(T,x,y)
336 <init-declare>(T,y,+(z,q))
341 m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC],
343 5.0-5.3: <declare>(T,x)
344 7.0-7.7: <init-declare>(T,x,y)
346 11.0-11.9: +(<cast>(x,T),y)
347 13.0-13.5: <declare>(T,x)
348 15.0-15.13: <init-declare>(T,y,+(z,q))
353 m4_define([_AT_AMBIG_GLR_OUTPUT],
356 <init-declare>(T,x,y)
359 <OR>(<declare>(T,x),<cast>(x,T))
360 <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
365 m4_define([_AT_AMBIG_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: <OR>(<declare>(T,x),<cast>(x,T))
372 15.0-15.13: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
377 m4_define([_AT_GLR_STDERR],
381 m4_define([_AT_GLR_STDERR_WITH_LOC],
382 [[17.5-4: syntax error
385 m4_define([_AT_VERBOSE_GLR_STDERR],
386 [[syntax error, unexpected ID, expecting '=' or '+' or ')'
389 m4_define([_AT_VERBOSE_GLR_STDERR_WITH_LOC],
390 [[17.5-4: syntax error, unexpected ID, expecting '=' or '+' or ')'
393 ## ---------------------------------------------------- ##
394 ## Compile the grammar described in the documentation. ##
395 ## ---------------------------------------------------- ##
397 AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
398 _AT_TEST_GLR_CXXTYPES([],
399 [%dprec 1], [%dprec 2])
400 AT_PARSER_CHECK([[./types test-input]], 0,
401 [_AT_RESOLVED_GLR_OUTPUT], [_AT_GLR_STDERR])
404 AT_SETUP([GLR: Resolve ambiguity, impure, locations])
405 _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
406 AT_PARSER_CHECK([[./types test-input]], 0,
407 [_AT_RESOLVED_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
410 AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
411 _AT_TEST_GLR_CXXTYPES([%define api.pure],
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, pure, locations])
418 _AT_TEST_GLR_CXXTYPES([%define api.pure %locations],
419 [%dprec 1], [%dprec 2])
420 AT_PARSER_CHECK([[./types test-input]], 0,
421 [_AT_RESOLVED_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
424 AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
425 _AT_TEST_GLR_CXXTYPES([],
426 [%merge <stmtMerge>], [%merge <stmtMerge>])
427 AT_PARSER_CHECK([[./types test-input]], 0,
428 [_AT_AMBIG_GLR_OUTPUT], [_AT_GLR_STDERR])
431 AT_SETUP([GLR: Merge conflicting parses, impure, locations])
432 _AT_TEST_GLR_CXXTYPES([%locations],
433 [%merge <stmtMerge>], [%merge <stmtMerge>])
434 AT_PARSER_CHECK([[./types test-input]], 0,
435 [_AT_AMBIG_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
438 AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
439 _AT_TEST_GLR_CXXTYPES([%define api.pure],
440 [%merge <stmtMerge>], [%merge <stmtMerge>])
441 AT_PARSER_CHECK([[./types test-input]], 0,
442 [_AT_AMBIG_GLR_OUTPUT], [_AT_GLR_STDERR])
444 AT_SETUP([GLR: Merge conflicting parses, pure, locations])
445 _AT_TEST_GLR_CXXTYPES([%define api.pure %locations],
446 [%merge <stmtMerge>],[%merge <stmtMerge>])
447 AT_PARSER_CHECK([[./types test-input]], 0,
448 [_AT_AMBIG_GLR_OUTPUT_WITH_LOC], [_AT_GLR_STDERR_WITH_LOC])
451 AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
452 _AT_TEST_GLR_CXXTYPES([%error-verbose],
453 [%merge <stmtMerge>], [%merge <stmtMerge>])
454 AT_PARSER_CHECK([[./types test-input]], 0,
455 [_AT_AMBIG_GLR_OUTPUT], [_AT_VERBOSE_GLR_STDERR])