]> git.saurik.com Git - bison.git/blob - tests/calc.m4
* src/output.c: Formatting changes.
[bison.git] / tests / calc.m4
1 # -*- Autoconf -*-
2
3 cat <<EOF
4
5 Simple Calculator.
6
7 EOF
8
9 ## ---------------------------------------------------- ##
10 ## Compile the grammar described in the documentation. ##
11 ## ---------------------------------------------------- ##
12
13 # We use integers to avoid dependencies upon the precision of doubles.
14 AT_SETUP(Compiling a grammar)
15
16 AT_DATA([calc.y],
17 [[/* Infix notation calculator--calc */
18
19 %{
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23 #define YYSTYPE int
24
25 static int power (int base, int exponent);
26 static int read_signed_integer (FILE *stream);
27 static void yyerror (const char *s);
28 extern void perror (const char *s);
29 %}
30
31 /* BISON Declarations */
32 %token NUM
33 %left '-' '+'
34 %left '*' '/'
35 %left NEG /* negation--unary minus */
36 %right '^' /* exponentiation */
37
38 /* Grammar follows */
39 %%
40 input: /* empty string */
41 | input line
42 ;
43
44 line: '\n'
45 | exp '\n' { printf ("%d", $1); }
46 ;
47
48 exp: NUM { $$ = $1; }
49 | exp '+' exp { $$ = $1 + $3; }
50 | exp '-' exp { $$ = $1 - $3; }
51 | exp '*' exp { $$ = $1 * $3; }
52 | exp '/' exp { $$ = $1 / $3; }
53 | '-' exp %prec NEG { $$ = -$2; }
54 | exp '^' exp { $$ = power ($1, $3); }
55 | '(' exp ')' { $$ = $2; }
56 ;
57 %%
58 /* The input. */
59 FILE *yyin;
60
61 static void
62 yyerror (const char *s)
63 {
64 fprintf (stderr, "%s\n", s);
65 }
66
67 static int
68 read_signed_integer (FILE *stream)
69 {
70 int c = getc (stream);
71 int sign = 1;
72 int n = 0;
73
74 if (c == '-')
75 {
76 c = getc (stream);
77 sign = -1;
78 }
79
80 while (isdigit (c))
81 {
82 n = 10 * n + (c - '0');
83 c = getc (stream);
84 }
85
86 ungetc (c, stream);
87
88 return sign * n;
89 }
90
91 /*---------------------------------------------------------------.
92 | Lexical analyzer returns an integer on the stack and the token |
93 | NUM, or the ASCII character read if not a number. Skips all |
94 | blanks and tabs, returns 0 for EOF. |
95 `---------------------------------------------------------------*/
96
97 int
98 yylex ()
99 {
100 int c;
101
102 /* Skip white space. */
103 while ((c = getc (yyin)) == ' ' || c == '\t')
104 ;
105 /* process numbers */
106 if (c == '.' || isdigit (c))
107 {
108 ungetc (c, yyin);
109 yylval = read_signed_integer (yyin);
110 return NUM;
111 }
112 /* Return end-of-file. */
113 if (c == EOF)
114 return 0;
115 /* Return single chars. */
116 return c;
117 }
118
119 static int
120 power (int base, int exponent)
121 {
122 int res = 1;
123 if (exponent < 0)
124 exit (1);
125 for (/* Niente */; exponent; --exponent)
126 res *= base;
127 return res;
128 }
129
130 int
131 main (int argn, const char **argv)
132 {
133 if (argn == 2)
134 yyin = fopen (argv[1], "r");
135 else
136 yyin = stdin;
137
138 if (!stdin)
139 {
140 perror (argv[1]);
141 exit (1);
142 }
143 yyparse ();
144 return 0;
145 }
146 ]])
147
148 # Specify the output files to avoid problems on different file systems.
149 AT_CHECK([bison calc.y -o calc.c], 0, [], [])
150 AT_CHECK([$CC $CFLAGS calc.c -o calc], 0, [], [])
151
152 # AT_CHECK_CALC(INPUT, OUTPUT)
153 # ----------------------------
154 # Run `calc' on INPUT, and expect OUTPUT.
155 AT_DEFINE([AT_CHECK_CALC],
156 [AT_CHECK([echo "$1" | calc], 0, [$2], [])])
157
158 # Test the priorities.
159 AT_CHECK_CALC([1 + 2 * 3], [7])
160 AT_CHECK_CALC([1 + 2 * -3], [-5])
161
162 AT_CHECK_CALC([-1^2], [-1])
163 AT_CHECK_CALC([(-1)^2], [1])
164
165 AT_CHECK_CALC([---1], [-1])
166
167 AT_CHECK_CALC([1 - 2 - 3], [-4])
168 AT_CHECK_CALC([1 - (2 - 3)], [2])
169
170 AT_CHECK_CALC([2^2^3], [256])
171 AT_CHECK_CALC([(2^2)^3], [64])
172
173 AT_CLEANUP(calc calc.c)