1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
%{
#define _XOPEN_SOURCE 700
#include <locale.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
typedef struct {
int type;
union {
intmax_t i;
char *s;
} u;
} expr_yystype;
static char **expr_args;
static int expr_end = 0;
#define YYSTYPE expr_yystype
static int yylex(void);
static int yyerror(const char *s);
static void expr_only_integers(YYSTYPE *y1, YYSTYPE *y2);
static int expr_compare(YYSTYPE *y1, YYSTYPE *y2);
static void expr_print(YYSTYPE *y1);
%}
%token INTEGER
%token STRING
%token '(' ')'
%left ':'
%left '*' '/' '%'
%left '+' '-'
%left '=' '<' '>' LE GE NE
%left '&'
%left '|'
%start expr
%%
expr : STRING {
expr_print(&$1);
}
| INTEGER {
expr_print(&$1);
}
| '(' expr ')' {
$$ = $2;
return $$.type;
}
| expr ':' expr {
printf("match\n");
/* TODO: number of initial characters in $1.u.s matching re $2.u.s */
}
| expr '*' expr {
expr_only_integers(&$1, &$3);
$$.u.i = $1.u.i * $3.u.i;
expr_print(&$$);
return $$.type;
}
| expr '/' expr {
expr_only_integers(&$1, &$3);
$$.u.i = $1.u.i / $3.u.i;
expr_print(&$$);
return $$.type;
}
| expr '%' expr {
expr_only_integers(&$1, &$3);
$$.u.i = $1.u.i % $3.u.i;
expr_print(&$$);
return $$.type;
}
| expr '+' expr {
expr_only_integers(&$1, &$3);
$$.u.i = $1.u.i + $3.u.i;
expr_print(&$$);
return $$.type;
}
| expr '-' expr {
expr_only_integers(&$1, &$3);
$$.u.i = $1.u.i - $3.u.i;
expr_print(&$$);
return $$.type;
}
| expr '=' expr {
$$.type = INTEGER;
$$.u.i = expr_compare(&$1, &$3) == 0;
expr_print(&$$);
return $$.type;
}
| expr NE expr {
$$.type = INTEGER;
$$.u.i = expr_compare(&$1, &$3) != 0;
expr_print(&$$);
return $$.type;
}
| expr '<' expr {
$$.type = INTEGER;
$$.u.i = expr_compare(&$1, &$3) < 0;
expr_print(&$$);
return $$.type;
}
| expr LE expr {
$$.type = INTEGER;
$$.u.i = expr_compare(&$1, &$3) <= 0;
expr_print(&$$);
return $$.type;
}
| expr '>' expr {
$$.type = INTEGER;
$$.u.i = expr_compare(&$1, &$3) > 0;
expr_print(&$$);
return $$.type;
}
| expr GE expr {
$$.type = INTEGER;
$$.u.i = expr_compare(&$1, &$3) >= 0;
expr_print(&$$);
return $$.type;
}
| expr '&' expr {
printf("and\n");
}
| expr '|' expr {
printf("or\n");
}
;
%%
static void expr_print(YYSTYPE *y1)
{
if (!expr_end) {
return;
}
if (y1->type == INTEGER) {
printf("%jd\n", y1->u.i);
} else {
printf("%s\n", y1->u.s);
}
}
static void expr_only_integers(YYSTYPE *y1, YYSTYPE *y2)
{
if (y1->type == STRING) {
fprintf(stderr, "expr: %s is not an integer\n", y1->u.s);
exit(1);
}
if (y2->type == STRING) {
fprintf(stderr, "expr: %s is not an integer\n", y2->u.s);
exit(1);
}
}
static int expr_compare(YYSTYPE *y1, YYSTYPE *y2)
{
if (y1->type == INTEGER && y2->type == INTEGER) {
return y1->u.i - y2->u.i;
}
char buf[64];
char *s1 = y1->u.s;
char *s2 = y2->u.s;
if (y1->type == INTEGER) {
snprintf(buf, sizeof(buf), "%jd", y1->u.i);
s1 = buf;
} else if (y2->type == INTEGER) {
snprintf(buf, sizeof(buf), "%jd", y2->u.i);
s2 = buf;
}
return strcmp(s1, s2);
}
static int yylex(void)
{
if (expr_args[optind] == NULL) {
expr_end = 1;
return 0;
}
char *s = expr_args[optind];
if (strlen(s) == 1) {
char c = s[0];
if (strchr("|&=+-*/%():", c)) {
optind++;
return c;
}
}
if (strlen(s) == 2) {
if (!strcmp(s, ">=")) {
optind++;
return GE;
}
if (!strcmp(s, "<=")) {
optind++;
return LE;
}
if (!strcmp(s, "!=")) {
optind++;
return NE;
}
}
char *end = NULL;
yylval.type = INTEGER;
yylval.u.i = strtoimax(s, &end, 10);
if (end && *end != '\0') {
yylval.type = STRING;
yylval.u.s = s;
}
optind++;
return yylval.type;
}
static int yyerror(const char *s)
{
fprintf(stderr, "expr: %s\n", s);
return 0;
}
int main(int argc, char *argv[])
{
setlocale(LC_ALL, "");
int c;
while ((c = getopt(argc, argv, "")) != -1) {
switch (c) {
default:
return -1;
}
}
if (optind >= argc) {
fprintf(stderr, "expr: missing operands\n");
return 1;
}
expr_args = argv;
return yyparse();
}
|