1 # Simple calculator. -*- Autotest -*-
3 # Copyright (C) 2000-2015, 2018-2020 Free Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 ## ---------------------------------------------------- ##
19 ## Compile the grammar described in the documentation. ##
20 ## ---------------------------------------------------- ##
26 m4_pushdef([AT_CALC_MAIN], [AT_LANG_DISPATCH([$0], $@)])
28 # Whether token translation is supported.
29 m4_pushdef([AT_TOKEN_TRANSLATE_IF],
30 [AT_ERROR_CUSTOM_IF([$1], [AT_ERROR_DETAILED_IF([$1], [$2])])])
32 m4_define([AT_CALC_MAIN(c)],
39 /* A C++ ]AT_NAME_PREFIX[parse that simulates the C signature. */
41 ]AT_NAME_PREFIX[parse (]AT_PARAM_IF([semantic_value *result, int *count, int *nerrs]))[
43 ]AT_NAME_PREFIX[::parser parser]AT_PARAM_IF([ (result, count, nerrs)])[;
44 #if ]AT_API_PREFIX[DEBUG
45 parser.set_debug_level (1);
47 return parser.parse ();
52 /* Value of the last computation. */
53 semantic_value global_result = 0;
54 /* Total number of computations. */
56 /* Total number of errors. */
59 /* A C main function. */
61 main (int argc, const char **argv)
63 semantic_value result = 0;
68 /* This used to be alarm (10), but that isn't enough time for a July
69 1995 vintage DEC Alphastation 200 4/100 system, according to
70 Nelson H. F. Beebe. 100 seconds was enough for regular users,
71 but the Hydra build farm, which is heavily loaded needs more. */
76 input = fopen (argv[1], "r");
86 ]AT_CXX_IF([], [AT_DEBUG_IF([ ]AT_NAME_PREFIX[debug = 1;])])[
87 status = ]AT_NAME_PREFIX[parse (]AT_PARAM_IF([[&result, &count, &nerrs]])[);
89 perror ("fclose");]AT_PARAM_IF([[
90 assert (global_result == result); (void) result;
91 assert (global_count == count); (void) count;
92 assert (global_nerrs == nerrs); (void) nerrs;
93 printf ("final: %d %d %d\n", global_result, global_count, global_nerrs);]])[
98 m4_copy([AT_CALC_MAIN(c)], [AT_CALC_MAIN(c++)])
100 m4_define([AT_CALC_MAIN(d)],
101 [[int main (string[] args)
103 semantic_value result = 0;
106 File input = args.length == 2 ? File (args[1], "r") : stdin;
107 auto l = calcLexer (input);
108 auto p = new YYParser (l);]AT_DEBUG_IF([[
109 p.setDebugLevel (1);]])[
115 m4_define([AT_CALC_MAIN(java)],
116 [[public static void main (String[] args) throws IOException
118 Calc p = new Calc (System.in);]], [[
119 CalcLexer l = new CalcLexer (System.in);
120 Calc p = new Calc (l);]])AT_DEBUG_IF([[
121 p.setDebugLevel (1);]])[
122 boolean success = p.parse ();
133 m4_pushdef([AT_CALC_YYLEX], [AT_LANG_DISPATCH([$0], $@)])
136 m4_define([AT_CALC_YYLEX(c)],
139 ]AT_YYLEX_DECLARE_EXTERN[
142 static AT_YYLTYPE last_yylloc;
145 get_char (]AT_YYLEX_FORMALS[)
147 int res = getc (input);
150 last_yylloc = AT_LOC;
154 AT_LOC_LAST_COLUMN = 1;
157 AT_LOC_LAST_COLUMN++;
163 unget_char (]AT_YYLEX_PRE_FORMALS[ int c)
167 /* Wrong when C == '\n'. */
168 AT_LOC = last_yylloc;
174 read_integer (]AT_YYLEX_FORMALS[)
176 int c = get_char (]AT_YYLEX_ARGS[);
182 res = 10 * res + (c - '0');
183 c = get_char (]AT_YYLEX_ARGS[);
186 unget_char (]AT_YYLEX_PRE_ARGS[ c);
192 /*---------------------------------------------------------------.
193 | Lexical analyzer returns an integer on the stack and the token |
194 | NUM, or the ASCII character read if not a number. Skips all |
195 | blanks and tabs, returns 0 for EOF. |
196 `---------------------------------------------------------------*/
201 /* Skip white spaces. */
205 AT_LOC_FIRST_COLUMN = AT_LOC_LAST_COLUMN;
206 AT_LOC_FIRST_LINE = AT_LOC_LAST_LINE;
209 while ((c = get_char (]AT_YYLEX_ARGS[)) == ' ' || c == '\t');
211 /* Process numbers. */
214 unget_char (]AT_YYLEX_PRE_ARGS[ c);
215 ]AT_VAL[.ival = read_integer (]AT_YYLEX_ARGS[);
216 return ]AT_CXX_IF([AT_NAMESPACE::parser::token::])[]AT_TOKEN_PREFIX[NUM;
219 /* Return end-of-file. */
221 return ]AT_CXX_IF([AT_NAMESPACE::parser::token::])[]AT_TOKEN_PREFIX[CALC_EOF;
223 /* An explicit error raised by the scanner. */
226 fprintf (stderr, "%d.%d: ",
227 AT_LOC_FIRST_LINE, AT_LOC_FIRST_COLUMN);])[
228 fputs ("syntax error: invalid character: '#'\n", stderr);
229 return ]AT_CXX_IF([AT_NAMESPACE::parser::token::])[]AT_TOKEN_PREFIX[]AT_API_PREFIX[error;
232 /* Return single chars. */
237 m4_copy([AT_CALC_YYLEX(c)], [AT_CALC_YYLEX(c++)])
239 m4_define([AT_CALC_YYLEX(d)],
240 [[import std.range.primitives;
243 auto calcLexer(R)(R range)
244 if (isInputRange!R && is (ElementType!R : dchar))
246 return new CalcLexer!R(range);
249 auto calcLexer (File f)
251 import std.algorithm : map, joiner;
252 import std.utf : byDchar;
254 return f.byChunk(1024) // avoid making a syscall roundtrip per char
255 .map!(chunk => cast(char[]) chunk) // because byChunk returns ubyte[]
256 .joiner // combine chunks into a single virtual range of char
257 .calcLexer; // forward to other overload
260 class CalcLexer(R) : Lexer
261 if (isInputRange!R && is (ElementType!R : dchar))
271 YYSemanticType semanticVal_;]AT_LOCATION_IF([[
272 YYLocation location = new YYLocation;
274 public final @property YYPosition startPos()
276 return location.begin;
279 public final @property YYPosition endPos()
284 public final @property YYSemanticType semanticVal()
292 import std.uni : isNumber;
293 while (input.front.isNumber)
295 res = res * 10 + (input.front - '0');]AT_LOCATION_IF([[
296 location.end.column += 1;]])[
304 location.begin = location.end;]])[
306 import std.uni : isWhite, isNumber;
308 // Skip initial spaces
309 while (!input.empty && input.front != '\n' && isWhite (input.front))
311 input.popFront;]AT_LOCATION_IF([[
312 location.begin.column += 1;
313 location.end.column += 1;]])[
318 return TokenKind.CALC_EOF;
321 if (input.front.isNumber)
323 semanticVal_.ival = parseInt;
324 return TokenKind.NUM;
327 // Individual characters
328 auto c = input.front;]AT_LOCATION_IF([[
331 location.end.line += 1;
332 location.end.column = 1;
335 location.end.column += 1;]])[
338 // An explicit error raised by the scanner. */
341 stderr.writeln (]AT_LOCATION_IF([location, ": ", ])["syntax error: invalid character: '#'");
342 return TokenKind.YYerror;
351 m4_define([AT_CALC_YYLEX(java)],
352 [AT_LEXPARAM_IF([[%code lexer {]],
353 [[%code epilogue { class CalcLexer implements Calc.Lexer {]])[
354 StreamTokenizer st;]AT_LOCATION_IF([[
355 PositionReader reader;]])[
357 public ]AT_LEXPARAM_IF([[YYLexer]], [[CalcLexer]])[ (InputStream is)
359 reader = new PositionReader (new InputStreamReader (is));
360 st = new StreamTokenizer (reader);]], [[
361 st = new StreamTokenizer (new InputStreamReader (is));]])[
363 st.eolIsSignificant (true);
364 st.wordChars ('0', '9');
368 Position start = new Position (1, 0);
369 Position end = new Position (1, 0);
371 public Position getStartPos () {
372 return new Position (start);
375 public Position getEndPos () {
376 return new Position (end);
384 public Object getLVal () {
388 public int yylex () throws IOException {;]AT_LOCATION_IF([[
389 start.set (reader.getPosition ());]])[
390 int tkind = st.nextToken ();]AT_LOCATION_IF([[
391 end.set (reader.getPosition ());]])[
394 case StreamTokenizer.TT_EOF:
396 case StreamTokenizer.TT_EOL:;]AT_LOCATION_IF([[
400 case StreamTokenizer.TT_WORD:
401 yylval = new Integer (st.sval);]AT_LOCATION_IF([[
402 end.set (reader.getPreviousPosition ());]])[
407 System.err.println(]AT_LOCATION_IF([[start + ": " + ]])["syntax error: invalid character: '#'");
413 ]AT_LEXPARAM_IF([], [[}]])[
423 # _AT_DATA_CALC_Y($1, $2, $3, [BISON-DIRECTIVES])
424 # -----------------------------------------------
425 # Produce 'calc.y' and, if %defines was specified, 'calc-lex.c' or
428 # Don't call this macro directly, because it contains some occurrences
429 # of '$1' etc. which will be interpreted by m4. So you should call it
430 # with $1, $2, and $3 as arguments, which is what AT_DATA_CALC_Y does.
432 # When %defines is not passed, generate a single self-contained file.
433 # Otherwise, generate three: calc.y with the parser, calc-lex.c with
434 # the scanner, and calc-main.c with "main()". This is in order to
435 # stress the use of the generated parser header. To avoid code
436 # duplication, AT_CALC_YYLEX and AT_CALC_MAIN contain the body of these
438 m4_pushdef([_AT_DATA_CALC_Y],
439 [m4_if([$1$2$3], $[1]$[2]$[3], [],
440 [m4_fatal([$0: Invalid arguments: $@])])dnl
441 AT_LANG_DISPATCH([$0], $@)])
443 m4_define([_AT_DATA_CALC_Y(c)],
444 [AT_DATA_GRAMMAR([calc.y],
445 [[/* Infix notation calculator--calc */
450 alias semantic_value = int;
456 ]AT_LOCATION_TYPE_SPAN_IF([[
469 # define YYLLOC_DEFAULT(Current, Rhs, N) \
473 (Current).first = YYRHSLOC (Rhs, 1).first; \
474 (Current).last = YYRHSLOC (Rhs, N).last; \
478 (Current).first = (Current).last = YYRHSLOC (Rhs, 0).last; \
484 void location_print (FILE *o, Span s);
485 #define LOCATION_PRINT location_print
489 /* Exercise pre-prologue dependency to %union. */
490 typedef int semantic_value;
494 /* Exercise %union. */
499 %printer { ]AT_CXX_IF([[yyo << $$]],
500 [[fprintf (yyo, "%d", $$)]])[; } <ival>;
502 ]AT_LANG_MATCH([c\|c++], [[
508 extern semantic_value global_result;
509 extern int global_count;
510 extern int global_nerrs;
520 static int power (int base, int exponent);
523 ]AT_YYLEX_DECLARE_EXTERN[
525 ]AT_TOKEN_TRANSLATE_IF([[
531 if (strcmp (cp, "end of input") == 0)
532 return "end of file";
533 else if (strcmp (cp, "number") == 0)
542 ]AT_LOCATION_TYPE_SPAN_IF([[
545 @$.first.l = @$.first.c = 1;
549 /* Bison Declarations */
550 %token CALC_EOF 0 ]AT_TOKEN_TRANSLATE_IF([_("end of input")], ["end of input"])[
551 %token <ival> NUM "number"
554 %nonassoc '=' /* comparison */
557 %precedence NEG /* negation--unary minus */
558 %right '^' /* exponentiation */
560 /* Grammar follows */
564 | input line { ]AT_PARAM_IF([++*count; ++global_count;])[ }
569 | exp '\n' { ]AT_PARAM_IF([*result = global_result = $1;], [AT_D_IF([], [USE ($1);])])[ }
576 if ($1 != $3)]AT_LANG_CASE(
580 snprintf (buf, sizeof buf, "calc: error: %d != %d", $1, $3);]AT_YYERROR_ARG_LOC_IF([[
581 yyerror (&@$, ]AT_PARAM_IF([result, count, nerrs, ])[buf);]], [[
583 YYLTYPE old_yylloc = yylloc;
585 yyerror (]AT_PARAM_IF([result, count, nerrs, ])[buf);
593 snprintf (buf, sizeof buf, "calc: error: %d != %d", $1, $3);
594 ]AT_GLR_IF([[yyparser.]])[error (]AT_LOCATION_IF([[@$, ]])[buf);
597 yyerror (]AT_LOCATION_IF([[@$, ]])[format ("calc: error: %d != %d", $1, $3));]])[
600 | exp '+' exp { $$ = $1 + $3; }
601 | exp '-' exp { $$ = $1 - $3; }
602 | exp '*' exp { $$ = $1 * $3; }
603 | exp '/' exp { $$ = $1 / $3; }
604 | '-' exp %prec NEG { $$ = -$2; }
605 | exp '^' exp { $$ = power ($1, $3); }
606 | '(' exp ')' { $$ = $2; }
607 | '(' error ')' { $$ = 1111; ]AT_D_IF([], [yyerrok;])[ }
608 | '!' { $$ = 0; ]AT_D_IF([return YYERROR], [YYERROR])[; }
609 | '-' error { $$ = 0; ]AT_D_IF([return YYERROR], [YYERROR])[; }
614 power (int base, int exponent)
617 assert (0 <= exponent);
618 for (/* Niente */; exponent; --exponent)
623 ]AT_LOCATION_TYPE_SPAN_IF([AT_CXX_IF([[
628 operator<< (std::ostream& o, const Span& s)
630 o << s.first.l << '.' << s.first.c;
631 if (s.first.l != s.last.l)
632 o << '-' << s.last.l << '.' << s.last.c - 1;
633 else if (s.first.c != s.last.c - 1)
634 o << '-' << s.last.c - 1;
640 location_print (FILE *o, Span s)
642 fprintf (o, "%d.%d", s.first.l, s.first.c);
643 if (s.first.l != s.last.l)
644 fprintf (o, "-%d.%d", s.last.l, s.last.c - 1);
645 else if (s.first.c != s.last.c - 1)
646 fprintf (o, "-%d", s.last.c - 1);
654 AT_DEFINES_IF([AT_DATA_SOURCE([[calc-lex.]AT_LANG_EXT],
655 [[#include "calc.]AT_LANG_HDR["
658 AT_DATA_SOURCE([[calc-main.]AT_LANG_EXT],
659 [[#include "calc.]AT_LANG_HDR["
666 m4_copy([_AT_DATA_CALC_Y(c)], [_AT_DATA_CALC_Y(c++)])
667 m4_copy([_AT_DATA_CALC_Y(c)], [_AT_DATA_CALC_Y(d)])
669 m4_define([_AT_DATA_CALC_Y(java)],
670 [AT_DATA_GRAMMAR([Calc.y],
671 [[/* Infix notation calculator--calc */
672 %define api.prefix {Calc}
673 %define api.parser.class {Calc}
678 %code imports {]AT_LOCATION_IF([[
679 import java.io.BufferedReader;]])[
680 import java.io.IOException;
681 import java.io.InputStream;
682 import java.io.InputStreamReader;
683 import java.io.Reader;
684 import java.io.StreamTokenizer;
690 ]AT_TOKEN_TRANSLATE_IF([[
691 static String i18n(String s)
693 if (s.equals ("end of input"))
694 return "end of file";
695 else if (s.equals ("number"))
703 /* Bison Declarations */
704 %token CALC_EOF 0 ]AT_TOKEN_TRANSLATE_IF([_("end of input")], ["end of input"])[
705 %token <Integer> NUM "number"
708 %nonassoc '=' /* comparison */
711 %precedence NEG /* negation--unary minus */
712 %right '^' /* exponentiation */
714 /* Grammar follows */
730 if ($1.intValue () != $3.intValue ())
731 yyerror (]AT_LOCATION_IF([[@$, ]])["calc: error: " + $1 + " != " + $3);
733 | exp '+' exp { $$ = $1 + $3; }
734 | exp '-' exp { $$ = $1 - $3; }
735 | exp '*' exp { $$ = $1 * $3; }
736 | exp '/' exp { $$ = $1 / $3; }
737 | '-' exp %prec NEG { $$ = -$2; }
738 | exp '^' exp { $$ = (int) Math.pow ($1, $3); }
739 | '(' exp ')' { $$ = $2; }
740 | '(' error ')' { $$ = 1111; }
741 | '!' { $$ = 0; return YYERROR; }
742 | '-' error { $$ = 0; return YYERROR; }
747 ]AT_JAVA_POSITION_DEFINE])[
749 ])# _AT_DATA_JAVA_CALC_Y
753 # AT_DATA_CALC_Y([BISON-OPTIONS])
754 # -------------------------------
755 # Produce 'calc.y' and, if %defines was specified, 'calc-lex.c' or
757 m4_define([AT_DATA_CALC_Y],
758 [_AT_DATA_CALC_Y($[1], $[2], $[3], [$1])
763 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [STDOUT], [NUM-STDERR-LINES])
764 # ------------------------------------------------------------------
765 # Run 'calc' on INPUT and expect no STDOUT nor STDERR.
767 # If BISON-OPTIONS contains '%debug' but not '%glr-parser', then
768 # NUM-STDERR-LINES is the number of expected lines on stderr.
769 # Currently this is ignored, though, since the output format is fluctuating.
771 # We don't count GLR's traces yet, since its traces are somewhat
772 # different from LALR's. Likewise for D.
774 # The push traces are the same, except for "Return for a new token", don't
776 m4_define([_AT_CHECK_CALC],
781 [AT_JAVA_PARSER_CHECK([Calc < input], 0, [AT_PARAM_IF([m4_n([$3])])], [stderr])],
782 [AT_PARSER_CHECK([calc input], 0, [AT_PARAM_IF([m4_n([$3])])], [stderr])])
783 AT_LANG_MATCH([c\|c++\|java],
785 [AT_CHECK([grep -c -v 'Return for a new token:' stderr],
787 [m4_n([AT_DEBUG_IF([$4], [0])])])])])
791 # _AT_CHECK_CALC_ERROR($1 = BISON-OPTIONS, $2 = EXIT-STATUS, $3 = INPUT,
793 # $5 = [NUM-STDERR-LINES],
794 # $6 = [CUSTOM-ERROR-MESSAGE])
795 # ----------------------------------------------------------------------
796 # Run 'calc' on INPUT, and expect a 'syntax error' message.
798 # If INPUT starts with a slash, it is used as absolute input file name,
799 # otherwise as contents.
801 # NUM-STDERR-LINES is the number of expected lines on stderr.
802 # If BISON-OPTIONS contains '%debug' but not '%glr', then NUM-STDERR-LINES
803 # is the number of expected lines on stderr.
805 # CUSTOM-ERROR-MESSAGE is the expected error message when parse.error
806 # is 'custom' and locations are enabled. Other expected formats are
808 m4_define([_AT_CHECK_CALC_ERROR],
809 [m4_bmatch([$3], [^/],
811 [AT_JAVA_PARSER_CHECK([Calc < $3], $2, [AT_PARAM_IF([m4_n([$4])])], [stderr])],
812 [AT_PARSER_CHECK([calc $3], $2, [AT_PARAM_IF([m4_n([$4])])], [stderr])])],
817 [AT_JAVA_PARSER_CHECK([Calc < input], $2, [AT_PARAM_IF([m4_n([$4])])], [stderr])],
818 [AT_PARSER_CHECK([calc input], $2, [AT_PARAM_IF([m4_n([$4])])], [stderr])])
821 # Normalize the observed and expected error messages, depending upon the
823 # 1. Remove the traces from observed.
838 /^yydestructor:/d' stderr >at-stderr
841 # 2. Create the reference error message.
846 # 3. If locations are not used, remove them.
847 AT_YYERROR_SEES_LOC_IF([],
848 [[sed 's/^[-0-9.]*: //' expout >at-expout
849 mv at-expout expout]])
851 # 4. If parse.error is not custom, turn the expected message to
852 # the traditional one.
853 AT_ERROR_CUSTOM_IF([], [
854 AT_PERL_REQUIRE([[-pi -e 'use strict;
855 s{syntax error on token \[(.*?)\] \(expected: (.*)\)}
858 my @exps = $][2 =~ /\[(.*?)\]/g;
859 ($][#exps && $][#exps < 4)
860 ? "syntax error, unexpected $unexp, expecting @{[join(\" or \", @exps)]}"
861 : "syntax error, unexpected $unexp";
866 # 5. If parse.error is simple, strip the', unexpected....' part.
868 [[sed 's/syntax error, .*$/syntax error/' expout >at-expout
869 mv at-expout expout]])
872 AT_CHECK([cat stderr], 0, [expout])
876 # AT_CHECK_SPACES([FILES])
877 # ------------------------
878 # Make sure we did not introduce bad spaces. Checked here because all
879 # the skeletons are (or should be) exercised here.
880 m4_define([AT_CHECK_SPACES],
881 [AT_PERL_CHECK([-ne '
883 print "$ARGV:$.: {$_}\n"
884 if (# No starting/ending empty lines.
885 (eof || $. == 1) && /^\s*$/
895 # AT_CHECK_JAVA_GREP(FILE, [LINE], [COUNT=1])
896 # -------------------------------------------
897 # Check that FILE contains exactly COUNT lines matching ^LINE$
898 # with grep. Unquoted so that COUNT can be a shell expression.
899 m4_define([AT_CHECK_JAVA_GREP],
900 [AT_CHECK_UNQUOTED([grep -c '^$2$' $1], [ignore], [m4_default([$3], [1])
904 # AT_CHECK_CALC([BISON-OPTIONS], [COMPILER-OPTIONS])
905 # --------------------------------------------------
906 # Start a testing chunk which compiles 'calc' grammar with
907 # BISON-OPTIONS, and performs several tests over the parser.
908 m4_define([AT_CHECK_CALC],
909 [m4_ifval([$3], [m4_fatal([$0: expected at most two arguments])])
911 # We use integers to avoid dependencies upon the precision of doubles.
912 AT_SETUP([Calculator $1 $2])
914 AT_BISON_OPTION_PUSHDEFS([$1])
917 AT_FULL_COMPILE(AT_JAVA_IF([[Calc]], [[calc]]), AT_DEFINES_IF([[lex], [main]], [[], []]), [$2], [-Wno-deprecated])
918 AT_PUSH_IF([AT_JAVA_IF(
919 [# Verify that this is a push parser.
920 AT_CHECK_JAVA_GREP([[Calc.java]],
921 [[.*public void push_parse_initialize ().*]])])])
923 AT_CHECK_SPACES([AT_JAVA_IF([Calc], [calc]).AT_LANG_EXT AT_DEFINES_IF([AT_JAVA_IF([Calc], [calc]).AT_LANG_HDR])])
925 # Test the precedences.
926 # The Java traces do not show the clean up sequence at the end,
927 # since it does not support %destructor.
943 [AT_JAVA_IF([1014], [1017])])
945 # Some syntax errors.
946 _AT_CHECK_CALC_ERROR([$1], [1], [1 2],
949 [AT_JAVA_IF([1.3-1.4], [1.3])[: syntax error on token [number] (expected: ['='] ['-'] ['+'] ['*'] ['/'] ['^'] ['\n'])]])
950 _AT_CHECK_CALC_ERROR([$1], [1], [1//2],
953 [AT_JAVA_IF([1.3-1.4], [1.3])[: syntax error on token ['/'] (expected: [number] ['-'] ['('] ['!'])]])
954 _AT_CHECK_CALC_ERROR([$1], [1], [error],
957 [AT_JAVA_IF([1.1-1.2], [1.1])[: syntax error on token [invalid token] (expected: [number] ['-'] ['\n'] ['('] ['!'])]])
958 _AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3],
962 [AT_JAVA_IF([1.7-1.8], [1.7])[: syntax error on token ['='] (expected: ['-'] ['+'] ['*'] ['/'] ['^'] ['\n'])]],
963 [AT_JAVA_IF([1.7-1.8], [1.7])[: syntax error on token ['='] (expected: ['-'] ['+'] ['*'] ['/'] ['^'])]])])
964 _AT_CHECK_CALC_ERROR([$1], [1],
969 [AT_JAVA_IF([2.1-2.2], [2.1])[: syntax error on token ['+'] (expected: ]AT_TOKEN_TRANSLATE_IF([[[end of file]]], [[[end of input]]])[ [number] ['-'] ['\n'] ['('] ['!'])]])
970 # Exercise error messages with EOF: work on an empty file.
971 _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null],
974 [[1.1: syntax error on token ]AT_TOKEN_TRANSLATE_IF([[[end of file]]], [[[end of input]]])[ (expected: [number] ['-'] ['\n'] ['('] ['!'])]])
976 # Exercise the error token: without it, we die at the first error,
979 # - have several errors which exercise different shift/discardings
980 # - (): nothing to pop, nothing to discard
981 # - (1 + 1 + 1 +): a lot to pop, nothing to discard
982 # - (* * *): nothing to pop, a lot to discard
983 # - (1 + 2 * *): some to pop and discard
985 # - test the action associated to 'error'
987 # - check the lookahead that triggers an error is not discarded
988 # when we enter error recovery. Below, the lookahead causing the
989 # first error is ")", which is needed to recover from the error and
990 # produce the "0" that triggers the "0 != 1" error.
992 _AT_CHECK_CALC_ERROR([$1], [0],
993 [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1],
996 [AT_JAVA_IF([1.2-1.3], [1.2])[: syntax error on token [')'] (expected: [number] ['-'] ['('] ['!'])
997 ]AT_JAVA_IF([1.18-1.19], [1.18])[: syntax error on token [')'] (expected: [number] ['-'] ['('] ['!'])
998 ]AT_JAVA_IF([1.23-1.24], [1.23])[: syntax error on token ['*'] (expected: [number] ['-'] ['('] ['!'])
999 ]AT_JAVA_IF([1.41-1.42], [1.41])[: syntax error on token ['*'] (expected: [number] ['-'] ['('] ['!'])
1000 ]AT_JAVA_IF([1.1-1.47], [1.1-46])[: calc: error: 4444 != 1]])
1002 # The same, but this time exercising explicitly triggered syntax errors.
1003 # POSIX says the lookahead causing the error should not be discarded.
1004 _AT_CHECK_CALC_ERROR([$1], [0], [(!) + (1 2) = 1],
1005 [[final: 2222 0 2]],
1007 [AT_JAVA_IF([1.10-1.11], [1.10])[: syntax error on token [number] (expected: ['='] ['-'] ['+'] ['*'] ['/'] ['^'] [')'])
1008 ]AT_JAVA_IF([1.1-1.16], [1.1-15])[: calc: error: 2222 != 1]])
1010 _AT_CHECK_CALC_ERROR([$1], [0], [(- *) + (1 2) = 1],
1011 [[final: 2222 0 3]],
1013 [AT_JAVA_IF([1.4-1.5], [1.4])[: syntax error on token ['*'] (expected: [number] ['-'] ['('] ['!'])
1014 ]AT_JAVA_IF([1.12-1.13], [1.12])[: syntax error on token [number] (expected: ['='] ['-'] ['+'] ['*'] ['/'] ['^'] [')'])
1015 ]AT_JAVA_IF([1.1-1.18], [1.1-17])[: calc: error: 2222 != 1]])
1017 # Check that yyerrok works properly: second error is not reported,
1018 # third and fourth are. Parse status is successful.
1019 _AT_CHECK_CALC_ERROR([$1], [0], [(* *) + (*) + (*)],
1020 [[final: 3333 0 3]],
1022 [AT_JAVA_IF([1.2-1.3], [1.2])[: syntax error on token ['*'] (expected: [number] ['-'] ['('] ['!'])
1023 ]AT_JAVA_IF([1.10-1.11], [1.10])[: syntax error on token ['*'] (expected: [number] ['-'] ['('] ['!'])
1024 ]AT_JAVA_IF([1.16-1.17], [1.16])[: syntax error on token ['*'] (expected: [number] ['-'] ['('] ['!'])]])
1029 # Check that returning YYerror from the scanner properly enters
1030 # error-recovery without issuing a second error message.
1032 _AT_CHECK_CALC_ERROR([$1], [0], [(#) + (#) = 2222],
1033 [[final: 2222 0 0]],
1035 [[1.2: syntax error: invalid character: '#'
1036 1.8: syntax error: invalid character: '#']])
1038 _AT_CHECK_CALC_ERROR([$1], [0], [(1 + #) = 1111],
1039 [[final: 1111 0 0]],
1041 [[1.6: syntax error: invalid character: '#']])
1043 _AT_CHECK_CALC_ERROR([$1], [0], [(# + 1) = 1111],
1044 [[final: 1111 0 0]],
1046 [[1.2: syntax error: invalid character: '#']])
1048 _AT_CHECK_CALC_ERROR([$1], [0], [(1 + # + 1) = 1111],
1049 [[final: 1111 0 0]],
1051 [[1.6: syntax error: invalid character: '#']])
1055 AT_BISON_OPTION_POPDEFS
1063 # ----------------- #
1064 # LALR Calculator. #
1065 # ----------------- #
1067 AT_BANNER([[LALR(1) Calculator.]])
1069 # AT_CHECK_CALC_LALR([BISON-OPTIONS])
1070 # -----------------------------------
1071 # Start a testing chunk which compiles 'calc' grammar with
1072 # BISON-OPTIONS, and performs several tests over the parser.
1073 m4_define([AT_CHECK_CALC_LALR],
1074 [AT_CHECK_CALC($@)])
1076 AT_CHECK_CALC_LALR([%define parse.trace])
1078 AT_CHECK_CALC_LALR([%defines])
1079 AT_CHECK_CALC_LALR([%debug %locations])
1080 AT_CHECK_CALC_LALR([%locations %define api.location.type {Span}])
1082 AT_CHECK_CALC_LALR([%name-prefix "calc"])
1083 AT_CHECK_CALC_LALR([%verbose])
1084 AT_CHECK_CALC_LALR([%yacc])
1085 AT_CHECK_CALC_LALR([%define parse.error detailed])
1086 AT_CHECK_CALC_LALR([%define parse.error verbose])
1088 AT_CHECK_CALC_LALR([%define api.pure full %locations])
1089 AT_CHECK_CALC_LALR([%define api.push-pull both %define api.pure full %locations])
1090 AT_CHECK_CALC_LALR([%define parse.error detailed %locations])
1092 AT_CHECK_CALC_LALR([%define parse.error detailed %locations %defines %define api.prefix {calc} %verbose %yacc])
1093 AT_CHECK_CALC_LALR([%define parse.error detailed %locations %defines %name-prefix "calc" %define api.token.prefix {TOK_} %verbose %yacc])
1095 AT_CHECK_CALC_LALR([%debug])
1096 AT_CHECK_CALC_LALR([%define parse.error detailed %debug %locations %defines %name-prefix "calc" %verbose %yacc])
1097 AT_CHECK_CALC_LALR([%define parse.error detailed %debug %locations %defines %define api.prefix {calc} %verbose %yacc])
1099 AT_CHECK_CALC_LALR([%define api.pure full %define parse.error detailed %debug %locations %defines %name-prefix "calc" %verbose %yacc])
1100 AT_CHECK_CALC_LALR([%define api.push-pull both %define api.pure full %define parse.error detailed %debug %locations %defines %define api.prefix {calc} %verbose %yacc])
1102 AT_CHECK_CALC_LALR([%define api.pure %define parse.error detailed %debug %locations %defines %define api.prefix {calc} %verbose %yacc %parse-param {semantic_value *result}{int *count}{int *nerrs}])
1104 AT_CHECK_CALC_LALR([%no-lines %define api.pure %define parse.error detailed %debug %locations %defines %define api.prefix {calc} %verbose %yacc %parse-param {semantic_value *result}{int *count}{int *nerrs}])
1105 AT_CHECK_CALC_LALR([%no-lines %define api.pure %define parse.error verbose %debug %locations %defines %define api.prefix {calc} %verbose %yacc %parse-param {semantic_value *result}{int *count}{int *nerrs}])
1108 AT_CHECK_CALC_LALR([%define parse.error custom])
1109 AT_CHECK_CALC_LALR([%define parse.error custom %locations %define api.prefix {calc}])
1110 AT_CHECK_CALC_LALR([%define parse.error custom %locations %define api.prefix {calc} %parse-param {semantic_value *result}{int *count}{int *nerrs}])
1111 AT_CHECK_CALC_LALR([%define parse.error custom %locations %define api.prefix {calc} %parse-param {semantic_value *result}{int *count}{int *nerrs} %define api.push-pull both %define api.pure full])
1112 AT_CHECK_CALC_LALR([%define parse.error custom %locations %define api.prefix {calc} %parse-param {semantic_value *result}{int *count}{int *nerrs} %define api.push-pull both %define api.pure full %define parse.lac full])
1114 # ---------------- #
1116 # ---------------- #
1118 AT_BANNER([[GLR Calculator.]])
1120 m4_define([AT_CHECK_CALC_GLR],
1121 [AT_CHECK_CALC([%glr-parser] $@)])
1125 AT_CHECK_CALC_GLR([%defines])
1126 AT_CHECK_CALC_GLR([%locations])
1127 AT_CHECK_CALC_GLR([%locations %define api.location.type {Span}])
1128 AT_CHECK_CALC_GLR([%name-prefix "calc"])
1129 AT_CHECK_CALC_GLR([%define api.prefix {calc}])
1130 AT_CHECK_CALC_GLR([%verbose])
1131 AT_CHECK_CALC_GLR([%define parse.error verbose])
1133 AT_CHECK_CALC_GLR([%define api.pure %locations])
1134 AT_CHECK_CALC_GLR([%define parse.error verbose %locations])
1136 AT_CHECK_CALC_GLR([%define parse.error custom %locations %defines %name-prefix "calc" %verbose])
1137 AT_CHECK_CALC_GLR([%define parse.error detailed %locations %defines %name-prefix "calc" %verbose])
1138 AT_CHECK_CALC_GLR([%define parse.error verbose %locations %defines %name-prefix "calc" %verbose])
1140 AT_CHECK_CALC_GLR([%debug])
1141 AT_CHECK_CALC_GLR([%define parse.error verbose %debug %locations %defines %name-prefix "calc" %verbose])
1142 AT_CHECK_CALC_GLR([%define parse.error verbose %debug %locations %defines %define api.prefix {calc} %define api.token.prefix {TOK_} %verbose])
1144 AT_CHECK_CALC_GLR([%define api.pure %define parse.error verbose %debug %locations %defines %name-prefix "calc" %verbose])
1146 AT_CHECK_CALC_GLR([%define api.pure %define parse.error verbose %debug %locations %defines %name-prefix "calc" %verbose %parse-param {semantic_value *result}{int *count}{int *nerrs}])
1147 AT_CHECK_CALC_GLR([%define api.pure %define parse.error verbose %debug %locations %defines %define api.prefix {calc} %verbose %parse-param {semantic_value *result}{int *count}{int *nerrs}])
1149 AT_CHECK_CALC_GLR([%no-lines %define api.pure %define parse.error verbose %debug %locations %defines %define api.prefix {calc} %verbose %parse-param {semantic_value *result}{int *count}{int *nerrs}])
1152 # ---------------------- #
1153 # LALR1 C++ Calculator. #
1154 # ---------------------- #
1156 AT_BANNER([[LALR(1) C++ Calculator.]])
1158 # First let's try using %skeleton
1159 AT_CHECK_CALC([%skeleton "lalr1.cc" %defines])
1161 m4_define([AT_CHECK_CALC_LALR1_CC],
1162 [AT_CHECK_CALC([%language "C++" $1], [$2])])
1164 AT_CHECK_CALC_LALR1_CC([])
1165 AT_CHECK_CALC_LALR1_CC([%locations])
1166 AT_CHECK_CALC_LALR1_CC([%locations], [$NO_EXCEPTIONS_CXXFLAGS])
1167 AT_CHECK_CALC_LALR1_CC([%locations %define api.location.type {Span}])
1168 AT_CHECK_CALC_LALR1_CC([%defines %locations %define parse.error verbose %name-prefix "calc" %verbose])
1170 AT_CHECK_CALC_LALR1_CC([%locations %define parse.error verbose %define api.prefix {calc} %verbose])
1171 AT_CHECK_CALC_LALR1_CC([%locations %define parse.error verbose %debug %name-prefix "calc" %verbose])
1173 AT_CHECK_CALC_LALR1_CC([%locations %define parse.error verbose %debug %define api.prefix {calc} %verbose])
1174 AT_CHECK_CALC_LALR1_CC([%locations %define parse.error verbose %debug %define api.prefix {calc} %define api.token.prefix {TOK_} %verbose])
1176 AT_CHECK_CALC_LALR1_CC([%defines %locations %define parse.error verbose %debug %name-prefix "calc" %verbose %parse-param {semantic_value *result}{int *count}{int *nerrs}])
1178 AT_CHECK_CALC_LALR1_CC([%define parse.error verbose %debug %define api.prefix {calc} %verbose %parse-param {semantic_value *result}{int *count}{int *nerrs}])
1179 AT_CHECK_CALC_LALR1_CC([%defines %locations %define parse.error verbose %debug %define api.prefix {calc} %verbose %parse-param {semantic_value *result}{int *count}{int *nerrs}])
1181 AT_CHECK_CALC_LALR1_CC([%defines %locations %define api.location.file none])
1182 AT_CHECK_CALC_LALR1_CC([%defines %locations %define api.location.file "my-location.hh"])
1184 AT_CHECK_CALC_LALR1_CC([%no-lines %defines %locations %define api.location.file "my-location.hh"])
1186 AT_CHECK_CALC_LALR1_CC([%locations %define parse.lac full %define parse.error verbose])
1187 AT_CHECK_CALC_LALR1_CC([%locations %define parse.lac full %define parse.error detailed])
1189 AT_CHECK_CALC_LALR1_CC([%define parse.error custom])
1190 AT_CHECK_CALC_LALR1_CC([%define parse.error custom %locations %define api.prefix {calc} %parse-param {semantic_value *result}{int *count}{int *nerrs}])
1191 AT_CHECK_CALC_LALR1_CC([%define parse.error custom %locations %define api.prefix {calc} %parse-param {semantic_value *result}{int *count}{int *nerrs} %define parse.lac full])
1193 # -------------------- #
1194 # GLR C++ Calculator. #
1195 # -------------------- #
1197 AT_BANNER([[GLR C++ Calculator.]])
1199 # Again, we try also using %skeleton.
1200 AT_CHECK_CALC([%skeleton "glr.cc"])
1202 m4_define([AT_CHECK_CALC_GLR_CC],
1203 [AT_CHECK_CALC([%language "C++" %glr-parser] $@)])
1205 AT_CHECK_CALC_GLR_CC([])
1206 AT_CHECK_CALC_GLR_CC([%locations])
1207 AT_CHECK_CALC_GLR_CC([%locations %define api.location.type {Span}])
1208 AT_CHECK_CALC_GLR_CC([%defines %define parse.error verbose %name-prefix "calc" %verbose])
1209 AT_CHECK_CALC_GLR_CC([%define parse.error verbose %define api.prefix {calc} %verbose])
1211 AT_CHECK_CALC_GLR_CC([%debug])
1213 AT_CHECK_CALC_GLR_CC([%define parse.error verbose %debug %name-prefix "calc" %verbose])
1214 AT_CHECK_CALC_GLR_CC([%define parse.error verbose %debug %name-prefix "calc" %define api.token.prefix {TOK_} %verbose])
1216 AT_CHECK_CALC_GLR_CC([%locations %defines %define parse.error verbose %debug %name-prefix "calc" %verbose %parse-param {semantic_value *result}{int *count}{int *nerrs}])
1217 AT_CHECK_CALC_GLR_CC([%locations %defines %define parse.error verbose %debug %define api.prefix {calc} %verbose %parse-param {semantic_value *result}{int *count}{int *nerrs}])
1219 AT_CHECK_CALC_GLR_CC([%no-lines %locations %defines %define parse.error verbose %debug %define api.prefix {calc} %verbose %parse-param {semantic_value *result}{int *count}{int *nerrs}])
1222 # -------------------- #
1223 # LALR1 D Calculator. #
1224 # -------------------- #
1226 AT_BANNER([[LALR(1) D Calculator.]])
1228 # First let's try using %skeleton
1229 AT_CHECK_CALC([%skeleton "lalr1.d"])
1231 m4_define([AT_CHECK_CALC_LALR1_D],
1232 [AT_CHECK_CALC([%language "D" $1], [$2])])
1234 AT_CHECK_CALC_LALR1_D([])
1235 AT_CHECK_CALC_LALR1_D([%locations])
1236 #AT_CHECK_CALC_LALR1_D([%locations %define api.location.type {Span}])
1237 AT_CHECK_CALC_LALR1_D([%define parse.error verbose %define api.prefix {calc} %verbose])
1239 AT_CHECK_CALC_LALR1_D([%debug])
1241 AT_CHECK_CALC_LALR1_D([%define parse.error verbose %debug %verbose])
1242 #AT_CHECK_CALC_LALR1_D([%define parse.error verbose %debug %define api.token.prefix {TOK_} %verbose])
1244 #AT_CHECK_CALC_LALR1_D([%locations %define parse.error verbose %debug %verbose %parse-param {semantic_value *result}{int *count}{int *nerrs}])
1245 #AT_CHECK_CALC_LALR1_D([%locations %define parse.error verbose %debug %define api.prefix {calc} %verbose %parse-param {semantic_value *result}{int *count}{int *nerrs}])
1248 # ----------------------- #
1249 # LALR1 Java Calculator. #
1250 # ----------------------- #
1252 AT_BANNER([[LALR(1) Java Calculator.]])
1254 m4_define([AT_CHECK_CALC_LALR1_JAVA],
1255 [AT_CHECK_CALC([%language "Java" $1], [$2])])
1257 AT_CHECK_CALC_LALR1_JAVA
1258 AT_CHECK_CALC_LALR1_JAVA([%define parse.error custom])
1259 AT_CHECK_CALC_LALR1_JAVA([%define parse.error detailed])
1260 AT_CHECK_CALC_LALR1_JAVA([%define parse.error verbose])
1261 AT_CHECK_CALC_LALR1_JAVA([%locations %define parse.error custom])
1262 AT_CHECK_CALC_LALR1_JAVA([%locations %define parse.error detailed])
1263 AT_CHECK_CALC_LALR1_JAVA([%locations %define parse.error verbose])
1264 AT_CHECK_CALC_LALR1_JAVA([%define parse.trace %define parse.error verbose])
1265 AT_CHECK_CALC_LALR1_JAVA([%define parse.trace %define parse.error verbose %locations %lex-param {InputStream is}])
1267 AT_CHECK_CALC_LALR1_JAVA([%define api.push-pull both])
1268 AT_CHECK_CALC_LALR1_JAVA([%define api.push-pull both %define parse.error detailed %locations])
1269 AT_CHECK_CALC_LALR1_JAVA([%define parse.trace %define parse.error custom %locations %lex-param {InputStream is} %define api.push-pull both])
1270 AT_CHECK_CALC_LALR1_JAVA([%define parse.trace %define parse.error verbose %locations %lex-param {InputStream is} %define api.push-pull both])
1273 m4_popdef([AT_TOKEN_TRANSLATE_IF])
1274 m4_popdef([AT_CALC_MAIN])
1275 m4_popdef([AT_CALC_YYLEX])