1 # Checking GLR Parsing. -*- Autotest -*-
2 # Copyright (C) 2002, 2003, 2004, 2005 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. */
37 #define YYSTYPE char *
38 ]m4_bmatch([$2], [stmtMerge],
39 [ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[
40 #define YYINITDEPTH 10
41 static char *format (char const *, ...);
45 # define LEX_PARAMETERS YYSTYPE *lvalp, struct YYLTYPE *llocp
46 # define ERROR_PARAMETERS struct YYLTYPE *llocp, char const *s
48 # define LEX_PARAMETERS YYSTYPE *lvalp
51 #ifndef LEX_PARAMETERS
52 # define LEX_PARAMETERS void
54 #ifndef ERROR_PARAMETERS
55 # define ERROR_PARAMETERS char const *s
57 int yylex (LEX_PARAMETERS);
58 int yyerror (ERROR_PARAMETERS);
68 %destructor { free ($$); } TYPENAME ID
75 printf ("%d.%d-%d.%d: ",
76 @2.first_line, @2.first_column,
77 @2.last_line, @2.last_column);])[
78 printf ("%s\n", ]$[2);
82 stmt : expr ';' $2 { $$ = ]$[1; }
84 | error ';' { $$ = "<error>"; }
89 | TYPENAME '(' expr ')' { $$ = format ("<cast>(%s,%s)", ]$[3, ]$[1); }
90 | expr '+' expr { $$ = format ("+(%s,%s)", ]$[1, ]$[3); }
91 | expr '=' expr { $$ = format ("=(%s,%s)", ]$[1, ]$[3); }
94 decl : TYPENAME declarator ';'
95 { $$ = format ("<declare>(%s,%s)", ]$[1, ]$[2); }
96 | TYPENAME declarator '=' expr ';'
97 { $$ = format ("<init-declare>(%s,%s,%s)", ]$[1, ]$[2, ]$[4); }
101 | '(' declarator ')' { $$ = ]$[2; }
112 main (int argc, char **argv)
116 if (!freopen (argv[1], "r", stdin))
122 yylex (LEX_PARAMETERS)
127 static int lineNum = 1;
128 static int colNum = 0;
131 # define yylloc (*llocp)
132 # define yylval (*lvalp)
143 colNum = (colNum + 7) & ~7;
156 yylloc.first_line = yylloc.last_line = lineNum;
157 yylloc.first_column = colNum;
167 if (i == sizeof buffer - 1)
171 while (isalnum (c) || c == '_');
175 tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
176 yylval = strcpy ((char *) malloc (i), buffer);
185 yylloc.last_column = colNum-1;
194 yyerror (ERROR_PARAMETERS)
196 #if YYPURE && YYLSP_NEEDED
197 /* Pacify GCC by using llocp. */
201 fprintf (stderr, "%s\n", s);
207 format (char const *form, ...)
211 va_start (args, form);
212 vsprintf (buffer, form, args);
214 return strcpy ((char *) malloc (strlen (buffer) + 1), buffer);
218 m4_bmatch([$2], [stmtMerge],
220 stmtMerge (YYSTYPE x0, YYSTYPE x1)
222 return format ("<OR>(%s,%s)", x0, x1);
227 AT_DATA([test-input],
250 This is total garbage, but it should be ignored.
253 AT_CHECK([bison -o types.c types.y], 0, [], ignore)
255 AT_BISON_OPTION_POPDEFS
258 m4_define([_AT_RESOLVED_GLR_OUTPUT],
261 <init-declare>(T,x,y)
265 <init-declare>(T,y,+(z,q))
270 m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC],
272 5.0-5.3: <declare>(T,x)
273 7.0-7.7: <init-declare>(T,x,y)
275 11.0-11.9: +(<cast>(x,T),y)
276 13.0-13.5: <declare>(T,x)
277 15.0-15.13: <init-declare>(T,y,+(z,q))
282 m4_define([_AT_AMBIG_GLR_OUTPUT],
285 <init-declare>(T,x,y)
288 <OR>(<declare>(T,x),<cast>(x,T))
289 <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
294 m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC],
296 5.0-5.3: <declare>(T,x)
297 7.0-7.7: <init-declare>(T,x,y)
299 11.0-11.9: +(<cast>(x,T),y)
300 13.0-13.5: <OR>(<declare>(T,x),<cast>(x,T))
301 15.0-15.13: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
306 m4_define([_AT_GLR_STDERR],
310 m4_define([_AT_VERBOSE_GLR_STDERR],
311 [[syntax error, unexpected ID, expecting '=' or '+' or ')'
314 ## ---------------------------------------------------- ##
315 ## Compile the grammar described in the documentation. ##
316 ## ---------------------------------------------------- ##
318 AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
319 _AT_TEST_GLR_CXXTYPES([],
320 [%dprec 1], [%dprec 2])
321 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
322 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
325 AT_SETUP([GLR: Resolve ambiguity, impure, locations])
326 _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
327 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
328 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
331 AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
332 _AT_TEST_GLR_CXXTYPES([%pure-parser],
333 [%dprec 1], [%dprec 2])
334 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
335 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
338 AT_SETUP([GLR: Resolve ambiguity, pure, locations])
339 _AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
340 [%dprec 1], [%dprec 2])
341 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
342 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
345 AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
346 _AT_TEST_GLR_CXXTYPES([],
347 [%merge <stmtMerge>], [%merge <stmtMerge>])
348 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
349 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
352 AT_SETUP([GLR: Merge conflicting parses, impure, locations])
353 _AT_TEST_GLR_CXXTYPES([%locations],
354 [%merge <stmtMerge>], [%merge <stmtMerge>])
355 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
356 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
359 AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
360 _AT_TEST_GLR_CXXTYPES([%pure-parser],
361 [%merge <stmtMerge>], [%merge <stmtMerge>])
362 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
363 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
365 AT_SETUP([GLR: Merge conflicting parses, pure, locations])
366 _AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
367 [%merge <stmtMerge>],[%merge <stmtMerge>])
368 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
369 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
372 AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
373 _AT_TEST_GLR_CXXTYPES([%error-verbose],
374 [%merge <stmtMerge>], [%merge <stmtMerge>])
375 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
376 _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR)