blob: 57b3de2e8d3092c1418962db6e64beb46f0386a8 (
plain)
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
|
%{
#define _XOPEN_SOURCE 700
#include <locale.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include "expr.h"
#include "y.tab.h"
extern int yyparse(void);
%}
DIGIT [0-9]
%%
"|" return '|';
"&" return '&';
"=" return '=';
">" return '>';
">=" return GE;
"<" return '<';
"<=" return LE;
"!=" return NE;
"+" return '+';
"-" return '-';
"*" return '*';
"/" return '/';
"%" return '%';
"(" return '(';
")" return ')';
":" return ':';
{DIGIT}+ { yylval.u.i = atoi(yytext); return yylval.type = INTEGER; }
-{DIGIT}+ { yylval.u.i = atoi(yytext); return yylval.type = INTEGER; }
.+ { yylval.u.s = strdup(yytext); return yylval.type = STRING; }
\n ;
%%
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;
}
size_t n = 1;
for (int i = optind; i < argc; i++) {
n += strlen(argv[i]) + 1;
}
FILE *mem = fmemopen(NULL, n, "w+");
for (int i = optind; i < argc; i++) {
fprintf(mem, "%s\n", argv[i]);
}
rewind(mem);
yyin = mem;
return yyparse();
}
|