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