]> mj.ucw.cz Git - umpf.git/blob - cond.y
numbers added
[umpf.git] / cond.y
1 %{
2
3 #include <stdio.h>
4 #include <string.h>
5
6 int yylex (void);
7 void yyerror (char const *);
8
9 %}
10
11 %union {
12         int b;  
13         int n;
14         char* str;
15 }
16
17 %token <str> CONST
18 %token <n> NUM
19 %left EQ NEQ GE LE GT LT
20 %left '|' '&' '^'
21 %left '!'
22 %type <b> boo
23
24 %%
25 input:  /* empty */
26         | input line
27 ;
28
29 line:   '\n'
30         | boo '\n'              { printf("%s\n",$1?"true":"false"); }
31         | error '\n'            { yyerrok; }
32 ;
33
34 boo:     CONST EQ CONST         { $$ = ! strcmp($1, $3); }
35         | CONST NEQ CONST       { $$ = !! strcmp($1, $3); }
36         | NUM EQ NUM            { $$ = $1 == $3 }
37         | NUM NEQ NUM           { $$ = $1 != $3 }
38         | NUM GE NUM            { $$ = $1 >= $3 }
39         | NUM LE NUM            { $$ = $1 <= $3 }
40         | NUM GT NUM            { $$ = $1 > $3 }
41         | NUM LT NUM            { $$ = $1 < $3 }
42         | boo '|' boo           { $$ = $1 || $3 }
43         | boo '&' boo           { $$ = $1 && $3 }
44         | boo '^' boo           { $$ = ($1 || $3) && !($1 && $3) }
45         | '!' boo               { $$ = ! $2 }
46 ;
47
48 ;
49 %%
50
51 #include <ctype.h>
52 #include <stdlib.h>
53
54 #define BUFSIZE 4096
55
56 int
57 yylex(void)
58 {
59
60         int i, c, last;
61         
62         while ((c = getchar ()) == ' ' || c == '\t');
63         
64         if (c == '"'){
65                 last = '"';
66                 i = 0;
67                 if (!(yylval.str = malloc(BUFSIZE))){
68                         puts("Low memory");
69                         exit(0);
70                 }
71                 while ((c = getchar()) != '"' || last == '\\'){
72                         yylval.str[i] = c;
73                         last = c;
74                         i++;
75                         if (i >= BUFSIZE-1)
76                                 break;
77                 }       
78                 yylval.str[i] = '\0';
79         
80                 return CONST;   
81         }
82
83         if (isdigit(c)){
84                 ungetc(c,stdin);
85                 scanf("%d",&yylval.n);
86                 return NUM;
87         }
88
89         if (c == '!'){
90                 if ((c = getchar ()) == '=')
91                         return NEQ;
92         }
93
94         if (c == '<'){
95                 if ((c = getchar ()) == '=')
96                         return LE;
97                 else
98                         return LT;
99         }
100
101         if (c == '>'){
102                 if ((c = getchar ()) == '=')
103                         return GE;
104                 else
105                         return GT;
106         }
107
108         if (c == '='){
109                 if ((c = getchar ()) == '=')
110                         return EQ;
111         }
112         
113         if (c == EOF)
114                 return 0;
115         
116         return c;
117 }
118
119 void
120 yyerror (char const *s)
121 {
122         fprintf (stderr, "%s\n", s);
123 }
124
125 int
126 main(void)
127 {
128         return yyparse ();
129 }