/[bison]/bison/tests/calc.at
ViewVC logotype

Diff of /bison/tests/calc.at

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.49 by eggert, Wed Dec 11 06:59:39 2002 UTC revision 1.50 by akim, Thu Feb 6 10:04:29 2003 UTC
# Line 1  Line 1 
1  # Checking the output filenames.                         -*- Autotest -*-  # Checking the output filenames.                         -*- Autotest -*-
2  # Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.  # Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3    
4  # This program is free software; you can redistribute it and/or modify  # 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  # it under the terms of the GNU General Public License as published by
# Line 55  typedef int value; Line 55  typedef int value;
55  static value global_result = 0;  static value global_result = 0;
56  static int global_count = 0;  static int global_count = 0;
57    
58    ]AT_LALR1_CC_IF([typedef yy::Location YYLTYPE;])[
59  %}  %}
60    
61  /* Exercise %union. */  /* Exercise %union. */
# Line 64  static int global_count = 0; Line 65  static int global_count = 0;
65  };  };
66    
67  %{  %{
 #if YYPURE  
 #  define LOC     (*yylloc)  
 #  define VAL     (*yylval)  
 #else  
 #  define LOC     (yylloc)  
 #  define VAL     (yylval)  
 #endif  
   
 #define YYLLOC_FORMAL   ]AT_LOCATION_IF([, YYLTYPE *yylloc])[  
 #define YYLLOC_ARG      ]AT_LOCATION_IF([, yylloc])[  
 #define USE_YYLLOC      ]AT_LOCATION_IF([(void) yylloc;])[  
   
 #if YYPURE  
 #  define LEX_FORMALS     YYSTYPE *yylval YYLLOC_FORMAL  
 #  define LEX_ARGS        yylval          YYLLOC_ARG  
 #  define USE_LEX_ARGS    (void) yylval;  USE_YYLLOC  
 #  define LEX_PRE_FORMALS   LEX_FORMALS,  
 #  define LEX_PRE_ARGS      LEX_ARGS,  
 #else  
 #  define LEX_FORMALS       void  
 #  define LEX_PRE_FORMALS  
 #  define LEX_ARGS  
 #  define LEX_PRE_ARGS  
 #  define USE_LEX_ARGS  
 #endif  
   
68  static int power (int base, int exponent);  static int power (int base, int exponent);
69  /* yyerror receives the location if:  /* yyerror receives the location if:
70     - %location & %pure & %glr     - %location & %pure & %glr
# Line 98  static void yyerror (]AT_YYERROR_ARG_LOC Line 73  static void yyerror (]AT_YYERROR_ARG_LOC
73                       ]AT_PARAM_IF([value *result, int *count, ])[                       ]AT_PARAM_IF([value *result, int *count, ])[
74                       const char *s                       const char *s
75                       );                       );
76  static int yylex (LEX_FORMALS);  static int yylex (]AT_LEX_FORMALS[);
77  static int yygetc (LEX_FORMALS);  static int yygetc (]AT_LEX_FORMALS[);
78  static void yyungetc (LEX_PRE_FORMALS int c);  static void yyungetc (]AT_LEX_PRE_FORMALS[ int c);
79  %}  %}
80    
81  /* Bison Declarations */  /* Bison Declarations */
# Line 147  exp: Line 122  exp:
122  /* The input. */  /* The input. */
123  static FILE *yyin;  static FILE *yyin;
124    
125  static void  ]AT_LALR1_CC_IF(
126  yyerror (]AT_YYERROR_ARG_LOC_IF([YYLTYPE *yylloc, ])[  [/* Currently, print_ is required in C++. */
127           ]AT_PARAM_IF([value *result, int *count, ])[  void
128           const char *s  yy::Parser::print_ ()
129           )  {
130  {    std::cerr << location;
131  ]AT_PARAM_IF([(void) result; (void) count; ])[  }
132  ]AT_YYERROR_SEES_LOC_IF([  
133    fprintf (stderr, "%d.%d-%d.%d: ",  /* A C++ error reporting function. */
134             LOC.first_line, LOC.first_column,  void
135             LOC.last_line, LOC.last_column);  yy::Parser::error_ ()
136  ])[  {
137    fprintf (stderr, "%s\n", s);    std::cerr << location << ": " << message << std::endl;
138  }  }
139    
140    int
141    yyparse (void)
142    {
143      yy::Parser parser = yy::Parser (!!YYDEBUG[]AT_LOCATION_IF([,
144                                      yy::Location::Location ()]));
145      return parser.parse ();
146    }
147    ],
148    [static void
149    yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *yylloc, ])
150             AT_PARAM_IF([value *result, int *count, ])
151             const char *s)
152    {
153    AT_PARAM_IF([(void) result; (void) count;])
154    AT_YYERROR_SEES_LOC_IF([
155      fprintf (stderr, "%d.%d",
156               AT_LOC.first_line, AT_LOC.first_column);
157      if (AT_LOC.first_line != AT_LOC.last_line)
158        fprintf (stderr, "-%d.%d",
159                 AT_LOC.last_line,  AT_LOC.last_column - 1);
160      else if (AT_LOC.first_column != AT_LOC.last_column - 1)
161        fprintf (stderr, "-%d",
162                 AT_LOC.last_column - 1);
163      fprintf (stderr, ": ");])
164      fprintf (stderr, "%s\n", s);
165    }])[
166    
167    
168  ]AT_LOCATION_IF([  ]AT_LOCATION_IF([
169  static YYLTYPE last_yylloc;  static YYLTYPE last_yylloc;
170  ])[  ])[
171  static int  static int
172  yygetc (LEX_FORMALS)  yygetc (]AT_LEX_FORMALS[)
173  {  {
174    int res = getc (yyin);    int res = getc (yyin);
175    USE_LEX_ARGS;    ]AT_USE_LEX_ARGS[;
176  ]AT_LOCATION_IF([  ]AT_LOCATION_IF([
177    last_yylloc = LOC;    last_yylloc = AT_LOC;
178    if (res == '\n')    if (res == '\n')
179      {      {
180        LOC.last_line++;  AT_LALR1_CC_IF(
181        LOC.last_column = 1;  [     AT_LOC.end.line++;
182          AT_LOC.end.column = 0;],
183    [     AT_LOC.last_line++;
184          AT_LOC.last_column = 0;])
185      }      }
186    else    else
187      LOC.last_column++;  AT_LALR1_CC_IF(
188    [    AT_LOC.end.column++;],
189    [    AT_LOC.last_column++;])
190  ])[  ])[
191    return res;    return res;
192  }  }
193    
194    
195  static void  static void
196  yyungetc (LEX_PRE_FORMALS int c)  yyungetc (]AT_LEX_PRE_FORMALS[ int c)
197  {  {
198    USE_LEX_ARGS;    ]AT_USE_LEX_ARGS[;
199  ]AT_LOCATION_IF([  ]AT_LOCATION_IF([
200    /* Wrong when C == `\n'. */    /* Wrong when C == `\n'. */
201    LOC = last_yylloc;    AT_LOC = last_yylloc;
202  ])[  ])[
203    ungetc (c, yyin);    ungetc (c, yyin);
204  }  }
205    
206  static int  static int
207  read_signed_integer (LEX_FORMALS)  read_signed_integer (]AT_LEX_FORMALS[)
208  {  {
209    int c = yygetc (LEX_ARGS);    int c = yygetc (]AT_LEX_ARGS[);
210    int sign = 1;    int sign = 1;
211    int n = 0;    int n = 0;
212    
213    USE_LEX_ARGS;    ]AT_USE_LEX_ARGS[;
214    if (c == '-')    if (c == '-')
215      {      {
216        c = yygetc (LEX_ARGS);        c = yygetc (]AT_LEX_ARGS[);
217        sign = -1;        sign = -1;
218      }      }
219    
220    while (isdigit (c))    while (isdigit (c))
221      {      {
222        n = 10 * n + (c - '0');        n = 10 * n + (c - '0');
223        c = yygetc (LEX_ARGS);        c = yygetc (]AT_LEX_ARGS[);
224      }      }
225    
226    yyungetc (LEX_PRE_ARGS c);    yyungetc (]AT_LEX_PRE_ARGS[ c);
227    
228    return sign * n;    return sign * n;
229  }  }
# Line 230  read_signed_integer (LEX_FORMALS) Line 237  read_signed_integer (LEX_FORMALS)
237  `---------------------------------------------------------------*/  `---------------------------------------------------------------*/
238    
239  static int  static int
240  yylex (LEX_FORMALS)  yylex (]AT_LEX_FORMALS[)
241  {  {
242    static int init = 1;    static int init = 1;
243    int c;    int c;
# Line 238  yylex (LEX_FORMALS) Line 245  yylex (LEX_FORMALS)
245    if (init)    if (init)
246      {      {
247        init = 0;        init = 0;
248  ]AT_LOCATION_IF([  ]AT_LALR1_CC_IF([],
249        LOC.last_column = 1;  [AT_LOCATION_IF([
250        LOC.last_line = 1;        AT_LOC.last_column = 0;
251  ])[        AT_LOC.last_line = 1;
252    ])])[
253      }      }
254    
255  ]AT_LOCATION_IF([  ]AT_LALR1_CC_IF(
256    LOC.first_column = LOC.last_column;  [ AT_LOC.begin = AT_LOC.end;],
257    LOC.first_line = LOC.last_line;  [AT_LOCATION_IF([
258  ])[    AT_LOC.first_column = AT_LOC.last_column;
259      AT_LOC.first_line   = AT_LOC.last_line;
260    ])])[
261    
262    /* Skip white space.  */    /* Skip white space.  */
263    while ((c = yygetc (LEX_ARGS)) == ' ' || c == '\t')    while ((c = yygetc (]AT_LEX_ARGS[)) == ' ' || c == '\t')
264      {      {
265  ]AT_LOCATION_IF([  ]AT_LALR1_CC_IF(
266        LOC.first_column = LOC.last_column;  [     AT_LOC.begin = AT_LOC.end;],
267        LOC.first_line = LOC.last_line;  [AT_LOCATION_IF([
268  ])[        AT_LOC.first_column = AT_LOC.last_column;
269          AT_LOC.first_line   = AT_LOC.last_line;
270    ])])[
271      }      }
272    
273    /* process numbers   */    /* process numbers   */
274    if (c == '.' || isdigit (c))    if (c == '.' || isdigit (c))
275      {      {
276        yyungetc (LEX_PRE_ARGS c);        yyungetc (]AT_LEX_PRE_ARGS[ c);
277        VAL.ival = read_signed_integer (LEX_ARGS);        ]AT_VAL[.ival = read_signed_integer (]AT_LEX_ARGS[);
278        return NUM;        return NUM;
279      }      }
280    
# Line 285  power (int base, int exponent) Line 297  power (int base, int exponent)
297    return res;    return res;
298  }  }
299    
300    
301  int  int
302  main (int argc, const char **argv)  main (int argc, const char **argv)
303  {  {
# Line 421  AT_CHECK([cat stderr], 0, [expout]) Line 434  AT_CHECK([cat stderr], 0, [expout])
434  m4_define([AT_CHECK_PUSHDEFS],  m4_define([AT_CHECK_PUSHDEFS],
435  [m4_if([$1$2], $[1]$[2], [],  [m4_if([$1$2], $[1]$[2], [],
436         [m4_fatal([$0: Invalid arguments: $@])])dnl         [m4_fatal([$0: Invalid arguments: $@])])dnl
437    m4_pushdef([AT_LALR1_CC_IF],
438    [m4_bmatch([$3], ["lalr1.cc"], [$1], [$2])])
439    m4_pushdef([AT_GLR_IF],
440    [m4_bmatch([$3], [%glr-parser], [$1], [$2])])
441  m4_pushdef([AT_PARAM_IF],  m4_pushdef([AT_PARAM_IF],
442  [m4_bmatch([$3], [%parse-param], [$1], [$2])])  [m4_bmatch([$3], [%parse-param], [$1], [$2])])
443  m4_pushdef([AT_LOCATION_IF],  m4_pushdef([AT_LOCATION_IF],
444  [m4_bmatch([$3], [%locations], [$1], [$2])])  [m4_bmatch([$3], [%locations], [$1], [$2])])
445  m4_pushdef([AT_PURE_IF],  m4_pushdef([AT_PURE_IF],
446  [m4_bmatch([$3], [%pure-parser], [$1], [$2])])  [m4_bmatch([$3], [%pure-parser], [$1], [$2])])
 m4_pushdef([AT_GLR_IF],  
 [m4_bmatch([$3], [%glr-parser], [$1], [$2])])  
447  m4_pushdef([AT_PURE_AND_LOC_IF],  m4_pushdef([AT_PURE_AND_LOC_IF],
448  [m4_bmatch([$3], [%locations.*%pure-parser\|%pure-parser.*%locations],  [m4_bmatch([$3], [%locations.*%pure-parser\|%pure-parser.*%locations],
449             [$1], [$2])])             [$1], [$2])])
# Line 441  m4_pushdef([AT_YYERROR_ARG_LOC_IF], Line 456  m4_pushdef([AT_YYERROR_ARG_LOC_IF],
456                      [$2])])                      [$2])])
457  # yyerror cannot see the locations if !glr & pure & !param.  # yyerror cannot see the locations if !glr & pure & !param.
458  m4_pushdef([AT_YYERROR_SEES_LOC_IF],  m4_pushdef([AT_YYERROR_SEES_LOC_IF],
459  [AT_LOCATION_IF([AT_GLR_IF([$1],  [AT_LALR1_CC_IF([$1],
460                             [AT_PURE_IF([AT_PARAM_IF([$1], [$2])],                  [AT_LOCATION_IF([AT_GLR_IF([$1],
461                                         [$1])])],                                             [AT_PURE_IF([AT_PARAM_IF([$1],
462                  [$2])])                                                                      [$2])],
463                                                           [$1])])],
464                                    [$2])])])
465    
466    # The interface is pure: either because %pure-parser, or because we
467    # are using the C++ parsers.
468    m4_pushdef([AT_PURE_LEX_IF],
469    [AT_PURE_IF([$1],
470                [AT_LALR1_CC_IF([$1], [$2])])])
471    
472    AT_PURE_LEX_IF(
473    [m4_pushdef([AT_LOC], [(*yylloc)])
474     m4_pushdef([AT_VAL], [(*yylval)])
475     m4_pushdef([AT_LEX_FORMALS],
476                [YYSTYPE *yylval[]AT_LOCATION_IF([, YYLTYPE *yylloc])])
477     m4_pushdef([AT_LEX_ARGS],
478                [yylval[]AT_LOCATION_IF([, yylloc])])
479     m4_pushdef([AT_USE_LEX_ARGS],
480                [(void) yylval;AT_LOCATION_IF([(void) yylloc])])
481     m4_pushdef([AT_LEX_PRE_FORMALS],
482                [AT_LEX_FORMALS, ])
483     m4_pushdef([AT_LEX_PRE_ARGS],
484                [AT_LEX_ARGS, ])
485    ],
486    [m4_pushdef([AT_LOC], [(yylloc)])
487     m4_pushdef([AT_VAL], [(yylval)])
488     m4_pushdef([AT_LEX_FORMALS],     [void])
489     m4_pushdef([AT_LEX_ARGS],        [])
490     m4_pushdef([AT_USE_LEX_ARGS],    [])
491     m4_pushdef([AT_LEX_PRE_FORMALS], [])
492     m4_pushdef([AT_LEX_PRE_ARGS],    [])
493  ])  ])
494    ])# AT_CALC_PUSHDEFS
495    
496    
497  # AT_CALC_POPDEFS  # AT_CALC_POPDEFS
498  # ---------------  # ---------------
499  m4_define([AT_CHECK_POPDEFS],  m4_define([AT_CHECK_POPDEFS],
500  [m4_popdef([AT_YYERROR_SEES_LOC_IF])  [m4_popdef([AT_LEX_PRE_ARGS])
501    m4_popdef([AT_LEX_PRE_FORMALS])
502    m4_popdef([AT_USE_LEX_ARGS])
503    m4_popdef([AT_LEX_ARGS])
504    m4_popdef([AT_LEX_FORMALS])
505    m4_popdef([AT_VAL])
506    m4_popdef([AT_LOC])
507    m4_popdef([AT_PURE_LEX_IF])
508    m4_popdef([AT_YYERROR_SEES_LOC_IF])
509  m4_popdef([AT_YYERROR_ARG_LOC_IF])  m4_popdef([AT_YYERROR_ARG_LOC_IF])
510  m4_popdef([AT_GLR_OR_PARAM_IF])  m4_popdef([AT_GLR_OR_PARAM_IF])
511  m4_popdef([AT_PURE_AND_LOC_IF])  m4_popdef([AT_PURE_AND_LOC_IF])
 m4_popdef([AT_GLR_IF])  
512  m4_popdef([AT_LOCATION_IF])  m4_popdef([AT_LOCATION_IF])
513  m4_popdef([AT_PARAM_IF])  m4_popdef([AT_PARAM_IF])
514    m4_popdef([AT_GLR_IF])
515    m4_popdef([AT_LALR1_CC_IF])
516  ])  ])
517    
518    
# Line 478  AT_DATA_CALC_Y([$1]) Line 533  AT_DATA_CALC_Y([$1])
533  AT_CHECK([bison -o calc.c calc.y],  AT_CHECK([bison -o calc.c calc.y],
534           [0], [], [])           [0], [], [])
535    
536  AT_COMPILE([calc])  AT_LALR1_CC_IF(
537    [AT_CHECK([$CXX --version || exit 77], 0, ignore, ignore)
538    AT_COMPILE_CXX([calc])],
539    [AT_COMPILE([calc])])
540    
541  # Test the priorities.  # Test the priorities.
542  _AT_CHECK_CALC([$1],  _AT_CHECK_CALC([$1],
# Line 499  _AT_CHECK_CALC([$1], Line 557  _AT_CHECK_CALC([$1],
557    
558  # Some syntax errors.  # Some syntax errors.
559  _AT_CHECK_CALC_ERROR([$1], [1], [0 0], [11],  _AT_CHECK_CALC_ERROR([$1], [1], [0 0], [11],
560                       [1.3-1.4: syntax error, unexpected "number"])                       [1.2: syntax error, unexpected "number"])
561  _AT_CHECK_CALC_ERROR([$1], [1], [1//2], [15],  _AT_CHECK_CALC_ERROR([$1], [1], [1//2], [15],
562                       [1.3-1.4: syntax error, unexpected '/', expecting "number" or '-' or '('])                       [1.2: syntax error, unexpected '/', expecting "number" or '-' or '('])
563  _AT_CHECK_CALC_ERROR([$1], [1], [error], [4],  _AT_CHECK_CALC_ERROR([$1], [1], [error], [4],
564                       [1.1-1.2: syntax error, unexpected $undefined, expecting "number" or '-' or '\n' or '('])                       [1.0: syntax error, unexpected $undefined, expecting "number" or '-' or '\n' or '('])
565  _AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [22],  _AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [22],
566                       [1.7-1.8: syntax error, unexpected '='])                       [1.6: syntax error, unexpected '='])
567  _AT_CHECK_CALC_ERROR([$1], [1],  _AT_CHECK_CALC_ERROR([$1], [1],
568                       [                       [
569  +1],  +1],
570                       [14],                       [14],
571                       [2.1-2.2: syntax error, unexpected '+'])                       [2.0: syntax error, unexpected '+'])
572  # Exercise error messages with EOF: work on an empty file.  # Exercise error messages with EOF: work on an empty file.
573  _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4],  _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4],
574                       [1.1-1.2: syntax error, unexpected "end of input", expecting "number" or '-' or '\n' or '('])                       [1.0: syntax error, unexpected "end of input", expecting "number" or '-' or '\n' or '('])
575    
576  # Exercise the error token: without it, we die at the first error,  # Exercise the error token: without it, we die at the first error,
577  # hence be sure i. to have several errors, ii. to test the action  # hence be sure i. to have several errors, ii. to test the action
578  # associated to `error'.  # associated to `error'.
579  _AT_CHECK_CALC_ERROR([$1], [0], [(1 ++ 2) + (0 0) = 1], [82],  _AT_CHECK_CALC_ERROR([$1], [0], [(1 ++ 2) + (0 0) = 1], [82],
580  [1.5-1.6: syntax error, unexpected '+', expecting "number" or '-' or '('  [1.4: syntax error, unexpected '+', expecting "number" or '-' or '('
581  1.15-1.16: syntax error, unexpected "number"  1.14: syntax error, unexpected "number"
582  calc: error: 0 != 1])  calc: error: 0 != 1])
583    
584  AT_CHECK_POPDEFS  AT_CHECK_POPDEFS
# Line 598  AT_CHECK_CALC_GLR([%error-verbose %debug Line 656  AT_CHECK_CALC_GLR([%error-verbose %debug
656  AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])  AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
657    
658  AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc %parse-param {value *result} %parse-param {int *count}])  AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc %parse-param {value *result} %parse-param {int *count}])
659    
660    
661    # ----------------------------- #
662    # Simple LALR1 C++ Calculator.  #
663    # ----------------------------- #
664    
665    AT_BANNER([[Simple LALR1 C++ Calculator.]])
666    
667    # AT_CHECK_CALC_LALR1_CC([BISON-OPTIONS])
668    # ---------------------------------------
669    # Start a testing chunk which compiles `calc' grammar with
670    # BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
671    m4_define([AT_CHECK_CALC_LALR1_CC],
672    [AT_CHECK_CALC([%skeleton "lalr1.cc"] $@)])
673    
674    # AT_CHECK_CALC_LALR1_CC()
675    
676    AT_CHECK_CALC_LALR1_CC([%defines %pure-parser %locations])
677    # AT_CHECK_CALC_LALR1_CC([%defines])
678    # AT_CHECK_CALC_LALR1_CC([%locations])
679    # AT_CHECK_CALC_LALR1_CC([%name-prefix="calc"])
680    # AT_CHECK_CALC_LALR1_CC([%verbose])
681    # AT_CHECK_CALC_LALR1_CC([%yacc])
682    # AT_CHECK_CALC_LALR1_CC([%error-verbose])
683    
684    # AT_CHECK_CALC_LALR1_CC([%error-verbose %locations])
685    
686    # AT_CHECK_CALC_LALR1_CC([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
687    
688    # AT_CHECK_CALC_LALR1_CC([%debug])
689    # AT_CHECK_CALC_LALR1_CC([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
690    
691    # AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
692    
693    # AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc %parse-param {value *result} %parse-param {int *count}])

Legend:
Removed from v.1.49  
changed lines
  Added in v.1.50

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26