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 #define YYSTACKEXPANDABLE 1
42 static char *format (char const *, ...);
46 # define LEX_PARAMETERS YYSTYPE *lvalp, struct YYLTYPE *llocp
47 # define ERROR_PARAMETERS struct YYLTYPE *llocp, char const *s
49 # define LEX_PARAMETERS YYSTYPE *lvalp
52 #ifndef LEX_PARAMETERS
53 # define LEX_PARAMETERS void
55 #ifndef ERROR_PARAMETERS
56 # define ERROR_PARAMETERS char const *s
58 int yylex (LEX_PARAMETERS);
59 int yyerror (ERROR_PARAMETERS);
69 %destructor { free ($$); } TYPENAME ID
76 printf ("%d.%d-%d.%d: ",
77 @2.first_line, @2.first_column,
78 @2.last_line, @2.last_column);])[
79 printf ("%s\n", ]$[2);
83 stmt : expr ';' $2 { $$ = ]$[1; }
85 | error ';' { $$ = "<error>"; }
90 | TYPENAME '(' expr ')' { $$ = format ("<cast>(%s,%s)", ]$[3, ]$[1); }
91 | expr '+' expr { $$ = format ("+(%s,%s)", ]$[1, ]$[3); }
92 | expr '=' expr { $$ = format ("=(%s,%s)", ]$[1, ]$[3); }
95 decl : TYPENAME declarator ';'
96 { $$ = format ("<declare>(%s,%s)", ]$[1, ]$[2); }
97 | TYPENAME declarator '=' expr ';'
98 { $$ = format ("<init-declare>(%s,%s,%s)", ]$[1, ]$[2, ]$[4); }
102 | '(' declarator ')' { $$ = ]$[2; }
113 main (int argc, char **argv)
117 if (!freopen (argv[1], "r", stdin))
123 yylex (LEX_PARAMETERS)
128 static int lineNum = 1;
129 static int colNum = 0;
132 # define yylloc (*llocp)
133 # define yylval (*lvalp)
144 colNum = (colNum + 7) & ~7;
157 yylloc.first_line = yylloc.last_line = lineNum;
158 yylloc.first_column = colNum;
168 if (i == sizeof buffer - 1)
172 while (isalnum (c) || c == '_');
176 tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
177 yylval = strcpy ((char *) malloc (i), buffer);
186 yylloc.last_column = colNum-1;
195 yyerror (ERROR_PARAMETERS)
197 #if YYPURE && YYLSP_NEEDED
198 /* Pacify GCC by using llocp. */
202 fprintf (stderr, "%s\n", s);
208 format (char const *form, ...)
212 va_start (args, form);
213 vsprintf (buffer, form, args);
215 return strcpy ((char *) malloc (strlen (buffer) + 1), buffer);
219 m4_bmatch([$2], [stmtMerge],
221 stmtMerge (YYSTYPE x0, YYSTYPE x1)
223 return format ("<OR>(%s,%s)", x0, x1);
228 AT_DATA([test-input],
251 This is total garbage, but it should be ignored.
254 AT_CHECK([bison -o types.c types.y], 0, [], ignore)
256 AT_BISON_OPTION_POPDEFS
259 m4_define([_AT_RESOLVED_GLR_OUTPUT],
262 <init-declare>(T,x,y)
266 <init-declare>(T,y,+(z,q))
271 m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC],
273 5.0-5.3: <declare>(T,x)
274 7.0-7.7: <init-declare>(T,x,y)
276 11.0-11.9: +(<cast>(x,T),y)
277 13.0-13.5: <declare>(T,x)
278 15.0-15.13: <init-declare>(T,y,+(z,q))
283 m4_define([_AT_AMBIG_GLR_OUTPUT],
286 <init-declare>(T,x,y)
289 <OR>(<declare>(T,x),<cast>(x,T))
290 <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
295 m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC],
297 5.0-5.3: <declare>(T,x)
298 7.0-7.7: <init-declare>(T,x,y)
300 11.0-11.9: +(<cast>(x,T),y)
301 13.0-13.5: <OR>(<declare>(T,x),<cast>(x,T))
302 15.0-15.13: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
307 m4_define([_AT_GLR_STDERR],
311 m4_define([_AT_VERBOSE_GLR_STDERR],
312 [[syntax error, unexpected ID, expecting '=' or '+' or ')'
315 ## ---------------------------------------------------- ##
316 ## Compile the grammar described in the documentation. ##
317 ## ---------------------------------------------------- ##
319 AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
320 _AT_TEST_GLR_CXXTYPES([],
321 [%dprec 1], [%dprec 2])
322 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
323 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
326 AT_SETUP([GLR: Resolve ambiguity, impure, locations])
327 _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
328 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
329 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
332 AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
333 _AT_TEST_GLR_CXXTYPES([%pure-parser],
334 [%dprec 1], [%dprec 2])
335 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
336 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
339 AT_SETUP([GLR: Resolve ambiguity, pure, locations])
340 _AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
341 [%dprec 1], [%dprec 2])
342 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
343 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
346 AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
347 _AT_TEST_GLR_CXXTYPES([],
348 [%merge <stmtMerge>], [%merge <stmtMerge>])
349 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
350 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
353 AT_SETUP([GLR: Merge conflicting parses, impure, locations])
354 _AT_TEST_GLR_CXXTYPES([%locations],
355 [%merge <stmtMerge>], [%merge <stmtMerge>])
356 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
357 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
360 AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
361 _AT_TEST_GLR_CXXTYPES([%pure-parser],
362 [%merge <stmtMerge>], [%merge <stmtMerge>])
363 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
364 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
366 AT_SETUP([GLR: Merge conflicting parses, pure, locations])
367 _AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
368 [%merge <stmtMerge>],[%merge <stmtMerge>])
369 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
370 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
373 AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
374 _AT_TEST_GLR_CXXTYPES([%error-verbose],
375 [%merge <stmtMerge>], [%merge <stmtMerge>])
376 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
377 _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR)