]> git.saurik.com Git - bison.git/blame_incremental - tests/calc.m4
`user_toknums' is output as a `short[]' in `output.c', while it is
[bison.git] / tests / calc.m4
... / ...
CommitLineData
1# -*- Autoconf -*-
2
3cat <<EOF
4
5Simple Calculator.
6
7EOF
8
9
10## ---------------------------------------------------- ##
11## Compile the grammar described in the documentation. ##
12## ---------------------------------------------------- ##
13
14
15# ------------------------- #
16# Helping Autotest macros. #
17# ------------------------- #
18
19
20# _AT_DATA_CALC_Y($1, $2, $3)
21# ---------------------------
22# Produce `calc.y'. Don't call this macro directly, because it contains
23# some occurrences of `$1' etc. which will be interpreted by m4. So
24# you should call it with $1, $2, and $3 as arguments, which is what
25# AT_DATA_CALC_Y does.
26AT_DEFINE([_AT_DATA_CALC_Y],
27[AT_DATA([calc.y],
28[[/* Infix notation calculator--calc */
29
30%{
31#include <stdio.h>
32#include <stdlib.h>
33#include <ctype.h>
34#define YYSTYPE int
35
36static int power (int base, int exponent);
37static int read_signed_integer (FILE *stream);
38static void yyerror (const char *s);
39extern void perror (const char *s);
40%}
41
42/* BISON Declarations */
43%token NUM
44%left '-' '+'
45%left '*' '/'
46%left NEG /* negation--unary minus */
47%right '^' /* exponentiation */
48
49/* Grammar follows */
50%%
51input: /* empty string */
52 | input line
53;
54
55line: '\n'
56 | exp '\n' { printf ("%d", $1); }
57;
58
59exp: NUM { $$ = $1; }
60 | exp '+' exp { $$ = $1 + $3; }
61 | exp '-' exp { $$ = $1 - $3; }
62 | exp '*' exp { $$ = $1 * $3; }
63 | exp '/' exp { $$ = $1 / $3; }
64 | '-' exp %prec NEG { $$ = -$2; }
65 | exp '^' exp { $$ = power ($1, $3); }
66 | '(' exp ')' { $$ = $2; }
67;
68%%
69/* The input. */
70FILE *yyin;
71
72static void
73yyerror (const char *s)
74{
75 fprintf (stderr, "%s\n", s);
76}
77
78static int
79read_signed_integer (FILE *stream)
80{
81 int c = getc (stream);
82 int sign = 1;
83 int n = 0;
84
85 if (c == '-')
86 {
87 c = getc (stream);
88 sign = -1;
89 }
90
91 while (isdigit (c))
92 {
93 n = 10 * n + (c - '0');
94 c = getc (stream);
95 }
96
97 ungetc (c, stream);
98
99 return sign * n;
100}
101
102/*---------------------------------------------------------------.
103| Lexical analyzer returns an integer on the stack and the token |
104| NUM, or the ASCII character read if not a number. Skips all |
105| blanks and tabs, returns 0 for EOF. |
106`---------------------------------------------------------------*/
107
108int
109yylex ()
110{
111 int c;
112
113 /* Skip white space. */
114 while ((c = getc (yyin)) == ' ' || c == '\t')
115 ;
116 /* process numbers */
117 if (c == '.' || isdigit (c))
118 {
119 ungetc (c, yyin);
120 yylval = read_signed_integer (yyin);
121 return NUM;
122 }
123 /* Return end-of-file. */
124 if (c == EOF)
125 return 0;
126 /* Return single chars. */
127 return c;
128}
129
130static int
131power (int base, int exponent)
132{
133 int res = 1;
134 if (exponent < 0)
135 exit (1);
136 for (/* Niente */; exponent; --exponent)
137 res *= base;
138 return res;
139}
140
141int
142main (int argn, const char **argv)
143{
144 if (argn == 2)
145 yyin = fopen (argv[1], "r");
146 else
147 yyin = stdin;
148
149 if (!stdin)
150 {
151 perror (argv[1]);
152 exit (1);
153 }
154
155#if YYDEBUG
156 yydebug = 1;
157#endif
158 yyparse ();
159 return 0;
160}
161]])
162])# _AT_DATA_CALC_Y
163
164
165# AT_DATA_CALC_Y
166# --------------
167# Produce `calc.y'.
168AT_DEFINE([AT_DATA_CALC_Y],
169[_AT_DATA_CALC_Y($[1], $[2], $[3])])
170
171
172# _AT_CHECK_CALC(INPUT, OUTPUT, [STDERR])
173# ---------------------------------------
174# Run `calc' on INPUT, and expect OUTPUT and STDERR.
175AT_DEFINE([_AT_CHECK_CALC],
176[AT_CHECK([echo "$1" | calc], 0, [$2], [$3])])
177
178
179# AT_CHECK_CALC(TITLE, [BISON-OPTIONS], [PARSER-EXPECTED-STDERR])
180# ---------------------------------------------------------------
181# Start a testing chunk named TITLE which compiles `calc' grammar with
182# BISON-OPTIONS, and performs several tests over the parser.
183AT_DEFINE([AT_CHECK_CALC],
184[# We use integers to avoid dependencies upon the precision of doubles.
185AT_SETUP([$1])
186
187AT_DATA_CALC_Y
188
189# Specify the output files to avoid problems on different file systems.
190AT_CHECK([bison calc.y -o calc.c $2], 0, [], [])
191AT_CHECK([$CC $CFLAGS calc.c -o calc], 0, [], [])
192
193# Test the priorities.
194_AT_CHECK_CALC([1 + 2 * 3], [7], [$3])
195_AT_CHECK_CALC([1 + 2 * -3], [-5], [$3])
196
197_AT_CHECK_CALC([-1^2], [-1], [$3])
198_AT_CHECK_CALC([(-1)^2], [1], [$3])
199
200_AT_CHECK_CALC([---1], [-1], [$3])
201
202_AT_CHECK_CALC([1 - 2 - 3], [-4], [$3])
203_AT_CHECK_CALC([1 - (2 - 3)], [2], [$3])
204
205_AT_CHECK_CALC([2^2^3], [256], [$3])
206_AT_CHECK_CALC([(2^2)^3], [64], [$3])
207
208AT_CLEANUP(calc calc.c)
209])# AT_CHECK_CALC
210
211
212# -------------- #
213# Actual tests. #
214# -------------- #
215
216
217AT_CHECK_CALC([Simple calculator])
218
219AT_CHECK_CALC([Simple Yacc compatible calculator],
220 [--yacc])
221
222AT_CHECK_CALC([Simple calculator whose tokens are numbered from 3],
223 [--raw])
224
225AT_CHECK_CALC([Simple debugging calculator],
226 [--debug], ignore)
227
228AT_CHECK_CALC([Simple Yacc compatible debugging calculator],
229 [--debug --yacc], ignore)