1 # Checking GLR Parsing. -*- Autotest -*-
2 # Copyright (C) 2002, 2003, 2004 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 = 0;
129 # define yylloc (*llocp)
130 # define yylval (*lvalp)
141 colNum = (colNum + 7) & ~7;
154 yylloc.first_line = yylloc.last_line = lineNum;
155 yylloc.first_column = colNum;
165 if (i == sizeof buffer - 1)
169 while (isalnum (c) || c == '_');
173 tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
174 yylval = strcpy (malloc (i), buffer);
183 yylloc.last_column = colNum-1;
192 yyerror (ERROR_PARAMETERS)
194 #if YYPURE && YYLSP_NEEDED
195 /* Pacify GCC by using llocp. */
199 fprintf (stderr, "%s\n", s);
205 format (char const *form, ...)
209 va_start (args, form);
210 vsprintf (buffer, form, args);
212 return strcpy (malloc (strlen (buffer) + 1), buffer);
216 m4_bmatch([$2], [stmtMerge],
218 stmtMerge (YYSTYPE x0, YYSTYPE x1)
220 return format ("<OR>(%s,%s)", x0, x1);
225 AT_DATA([test-input],
248 This is total garbage, but it should be ignored.
251 AT_CHECK([bison -o types.c types.y], 0, [], ignore)
253 AT_BISON_OPTION_POPDEFS
256 m4_define([_AT_RESOLVED_GLR_OUTPUT],
259 <init-declare>(T,x,y)
263 <init-declare>(T,y,+(z,q))
268 m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC],
270 5.0-5.3: <declare>(T,x)
271 7.0-7.7: <init-declare>(T,x,y)
273 11.0-11.9: +(<cast>(x,T),y)
274 13.0-13.5: <declare>(T,x)
275 15.0-15.13: <init-declare>(T,y,+(z,q))
280 m4_define([_AT_AMBIG_GLR_OUTPUT],
283 <init-declare>(T,x,y)
286 <OR>(<declare>(T,x),<cast>(x,T))
287 <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
292 m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC],
294 5.0-5.3: <declare>(T,x)
295 7.0-7.7: <init-declare>(T,x,y)
297 11.0-11.9: +(<cast>(x,T),y)
298 13.0-13.5: <OR>(<declare>(T,x),<cast>(x,T))
299 15.0-15.13: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
304 m4_define([_AT_GLR_STDERR],
308 m4_define([_AT_VERBOSE_GLR_STDERR],
309 [[syntax error, unexpected ID, expecting '=' or '+' or ')'
312 ## ---------------------------------------------------- ##
313 ## Compile the grammar described in the documentation. ##
314 ## ---------------------------------------------------- ##
316 AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
317 _AT_TEST_GLR_CXXTYPES([],
318 [%dprec 1], [%dprec 2])
319 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
320 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
323 AT_SETUP([GLR: Resolve ambiguity, impure, locations])
324 _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
325 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
326 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
329 AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
330 _AT_TEST_GLR_CXXTYPES([%pure-parser],
331 [%dprec 1], [%dprec 2])
332 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
333 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
336 AT_SETUP([GLR: Resolve ambiguity, pure, locations])
337 _AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
338 [%dprec 1], [%dprec 2])
339 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
340 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
343 AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
344 _AT_TEST_GLR_CXXTYPES([],
345 [%merge <stmtMerge>], [%merge <stmtMerge>])
346 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
347 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
350 AT_SETUP([GLR: Merge conflicting parses, impure, locations])
351 _AT_TEST_GLR_CXXTYPES([%locations],
352 [%merge <stmtMerge>], [%merge <stmtMerge>])
353 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
354 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
357 AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
358 _AT_TEST_GLR_CXXTYPES([%pure-parser],
359 [%merge <stmtMerge>], [%merge <stmtMerge>])
360 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
361 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
363 AT_SETUP([GLR: Merge conflicting parses, pure, locations])
364 _AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
365 [%merge <stmtMerge>],[%merge <stmtMerge>])
366 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
367 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
370 AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
371 _AT_TEST_GLR_CXXTYPES([%error-verbose],
372 [%merge <stmtMerge>], [%merge <stmtMerge>])
373 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
374 _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR)