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 const char*
38 ]m4_bmatch([$2], [stmtMerge],
39 [ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[
40 #define YYINITDEPTH 10
41 static char* format (const char*, ...);
57 printf ("%d.%d-%d.%d: ",
58 @2.first_line, @2.first_column,
59 @2.last_line, @2.last_column);])[
60 printf ("%s\n", ]$[2);
64 stmt : expr ';' $2 { $$ = ]$[1; }
66 | error ';' { $$ = "<error>"; }
71 | TYPENAME '(' expr ')' { $$ = format ("<cast>(%s,%s)", ]$[3, ]$[1); }
72 | expr '+' expr { $$ = format ("+(%s,%s)", ]$[1, ]$[3); }
73 | expr '=' expr { $$ = format ("=(%s,%s)", ]$[1, ]$[3); }
76 decl : TYPENAME declarator ';'
77 { $$ = format ("<declare>(%s,%s)", ]$[1, ]$[2); }
78 | TYPENAME declarator '=' expr ';'
79 { $$ = format ("<init-declare>(%s,%s,%s)", ]$[1, ]$[2, ]$[4); }
83 | '(' declarator ')' { $$ = ]$[2; }
94 main (int argc, char** argv)
98 if (!freopen (argv[1], "r", stdin))
104 #if YYPURE && YYLSP_NEEDED
105 yylex (YYSTYPE *lvalp, YYLTYPE *llocp)
107 yylex (YYSTYPE *lvalp)
115 static int lineNum = 1;
116 static int colNum = 1;
120 # define yylloc (*llocp)
121 # define yylval (*lvalp)
132 colNum = 1 + ((colNum + 7) & ~7);
145 yylloc.first_line = yylloc.last_line = lineNum;
146 yylloc.first_column = colNum;
156 if (i == sizeof buffer - 1)
160 while (isalnum (c) || c == '_');
164 tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
165 yylval = strcpy (malloc (i), buffer);
174 yylloc.last_column = colNum-1;
184 #if YYPURE && YYLSP_NEEDED
190 fprintf (stderr, "%s\n", s);
195 static char* format (const char* form, ...)
199 va_start (args, form);
200 vsprintf (buffer, form, args);
202 return strcpy (malloc (strlen (buffer) + 1), buffer);
206 m4_bmatch([$2], [stmtMerge],
208 stmtMerge (YYSTYPE x0, YYSTYPE x1)
210 return format ("<OR>(%s,%s)", x0, x1);
215 AT_DATA([test-input],
238 This is total garbage, but it should be ignored.
241 AT_CHECK([bison -o types.c types.y], 0, [], ignore)
243 AT_BISON_OPTION_POPDEFS
246 m4_define([_AT_RESOLVED_GLR_OUTPUT],
249 <init-declare>(T,x,y)
253 <init-declare>(T,y,+(z,q))
258 m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC],
260 5.1-5.4: <declare>(T,x)
261 7.1-7.8: <init-declare>(T,x,y)
263 11.1-11.10: +(<cast>(x,T),y)
264 13.1-13.6: <declare>(T,x)
265 15.1-15.14: <init-declare>(T,y,+(z,q))
270 m4_define([_AT_AMBIG_GLR_OUTPUT],
273 <init-declare>(T,x,y)
276 <OR>(<declare>(T,x),<cast>(x,T))
277 <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
282 m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC],
284 5.1-5.4: <declare>(T,x)
285 7.1-7.8: <init-declare>(T,x,y)
287 11.1-11.10: +(<cast>(x,T),y)
288 13.1-13.6: <OR>(<declare>(T,x),<cast>(x,T))
289 15.1-15.14: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
294 m4_define([_AT_GLR_STDERR],
298 m4_define([_AT_VERBOSE_GLR_STDERR],
299 [[syntax error, unexpected ID, expecting '=' or '+' or ')'
302 ## ---------------------------------------------------- ##
303 ## Compile the grammar described in the documentation. ##
304 ## ---------------------------------------------------- ##
306 AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
307 _AT_TEST_GLR_CXXTYPES([],
308 [%dprec 1], [%dprec 2])
309 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
310 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
313 AT_SETUP([GLR: Resolve ambiguity, impure, locations])
314 _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
315 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
316 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
319 AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
320 _AT_TEST_GLR_CXXTYPES([%pure-parser],
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, pure, locations])
327 _AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
328 [%dprec 1], [%dprec 2])
329 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
330 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
333 AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
334 _AT_TEST_GLR_CXXTYPES([],
335 [%merge <stmtMerge>], [%merge <stmtMerge>])
336 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
337 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
340 AT_SETUP([GLR: Merge conflicting parses, impure, locations])
341 _AT_TEST_GLR_CXXTYPES([%locations],
342 [%merge <stmtMerge>], [%merge <stmtMerge>])
343 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
344 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
347 AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
348 _AT_TEST_GLR_CXXTYPES([%pure-parser],
349 [%merge <stmtMerge>], [%merge <stmtMerge>])
350 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
351 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
353 AT_SETUP([GLR: Merge conflicting parses, pure, locations])
354 _AT_TEST_GLR_CXXTYPES([%pure-parser %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: Verbose messages, resolve ambiguity, impure, no locations])
361 _AT_TEST_GLR_CXXTYPES([%error-verbose],
362 [%merge <stmtMerge>], [%merge <stmtMerge>])
363 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
364 _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR)