1 # Checking GLR Parsing. -*- Autotest -*-
2 # Copyright (C) 2002, 2003 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., 59 Temple Place - Suite 330, 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 const *
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);
73 printf ("%d.%d-%d.%d: ",
74 @2.first_line, @2.first_column,
75 @2.last_line, @2.last_column);])[
76 printf ("%s\n", ]$[2);
80 stmt : expr ';' $2 { $$ = ]$[1; }
82 | error ';' { $$ = "<error>"; }
87 | TYPENAME '(' expr ')' { $$ = format ("<cast>(%s,%s)", ]$[3, ]$[1); }
88 | expr '+' expr { $$ = format ("+(%s,%s)", ]$[1, ]$[3); }
89 | expr '=' expr { $$ = format ("=(%s,%s)", ]$[1, ]$[3); }
92 decl : TYPENAME declarator ';'
93 { $$ = format ("<declare>(%s,%s)", ]$[1, ]$[2); }
94 | TYPENAME declarator '=' expr ';'
95 { $$ = format ("<init-declare>(%s,%s,%s)", ]$[1, ]$[2, ]$[4); }
99 | '(' declarator ')' { $$ = ]$[2; }
110 main (int argc, char **argv)
114 if (!freopen (argv[1], "r", stdin))
120 yylex (LEX_PARAMETERS)
125 static int lineNum = 1;
126 static int colNum = 1;
130 # define yylloc (*llocp)
131 # define yylval (*lvalp)
142 colNum = 1 + ((colNum + 7) & ~7);
155 yylloc.first_line = yylloc.last_line = lineNum;
156 yylloc.first_column = colNum;
166 if (i == sizeof buffer - 1)
170 while (isalnum (c) || c == '_');
174 tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
175 yylval = strcpy (malloc (i), buffer);
184 yylloc.last_column = colNum-1;
193 yyerror (ERROR_PARAMETERS)
195 #if YYPURE && YYLSP_NEEDED
196 /* Pacify GCC by using llocp. */
200 fprintf (stderr, "%s\n", s);
206 format (char const *form, ...)
210 va_start (args, form);
211 vsprintf (buffer, form, args);
213 return strcpy (malloc (strlen (buffer) + 1), buffer);
217 m4_bmatch([$2], [stmtMerge],
219 stmtMerge (YYSTYPE x0, YYSTYPE x1)
221 return format ("<OR>(%s,%s)", x0, x1);
226 AT_DATA([test-input],
249 This is total garbage, but it should be ignored.
252 AT_CHECK([bison -o types.c types.y], 0, [], ignore)
254 AT_BISON_OPTION_POPDEFS
257 m4_define([_AT_RESOLVED_GLR_OUTPUT],
260 <init-declare>(T,x,y)
264 <init-declare>(T,y,+(z,q))
269 m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC],
271 5.1-5.4: <declare>(T,x)
272 7.1-7.8: <init-declare>(T,x,y)
274 11.1-11.10: +(<cast>(x,T),y)
275 13.1-13.6: <declare>(T,x)
276 15.1-15.14: <init-declare>(T,y,+(z,q))
281 m4_define([_AT_AMBIG_GLR_OUTPUT],
284 <init-declare>(T,x,y)
287 <OR>(<declare>(T,x),<cast>(x,T))
288 <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
293 m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC],
295 5.1-5.4: <declare>(T,x)
296 7.1-7.8: <init-declare>(T,x,y)
298 11.1-11.10: +(<cast>(x,T),y)
299 13.1-13.6: <OR>(<declare>(T,x),<cast>(x,T))
300 15.1-15.14: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
305 m4_define([_AT_GLR_STDERR],
309 m4_define([_AT_VERBOSE_GLR_STDERR],
310 [[syntax error, unexpected ID, expecting '=' or '+' or ')'
313 ## ---------------------------------------------------- ##
314 ## Compile the grammar described in the documentation. ##
315 ## ---------------------------------------------------- ##
317 AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
318 _AT_TEST_GLR_CXXTYPES([],
319 [%dprec 1], [%dprec 2])
320 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
321 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
324 AT_SETUP([GLR: Resolve ambiguity, impure, locations])
325 _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
326 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
327 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
330 AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
331 _AT_TEST_GLR_CXXTYPES([%pure-parser],
332 [%dprec 1], [%dprec 2])
333 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
334 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
337 AT_SETUP([GLR: Resolve ambiguity, pure, locations])
338 _AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
339 [%dprec 1], [%dprec 2])
340 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
341 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
344 AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
345 _AT_TEST_GLR_CXXTYPES([],
346 [%merge <stmtMerge>], [%merge <stmtMerge>])
347 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
348 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
351 AT_SETUP([GLR: Merge conflicting parses, impure, locations])
352 _AT_TEST_GLR_CXXTYPES([%locations],
353 [%merge <stmtMerge>], [%merge <stmtMerge>])
354 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
355 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
358 AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
359 _AT_TEST_GLR_CXXTYPES([%pure-parser],
360 [%merge <stmtMerge>], [%merge <stmtMerge>])
361 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
362 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
364 AT_SETUP([GLR: Merge conflicting parses, pure, locations])
365 _AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
366 [%merge <stmtMerge>],[%merge <stmtMerge>])
367 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
368 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
371 AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
372 _AT_TEST_GLR_CXXTYPES([%error-verbose],
373 [%merge <stmtMerge>], [%merge <stmtMerge>])
374 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
375 _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR)