]> mj.ucw.cz Git - umpf.git/blob - cond.y
started with filtering mail
[umpf.git] / cond.y
1 %{
2
3 #include <stdio.h>
4 #include <string.h>
5
6 #include "umpf.h"
7
8 static struct tree* tree_malloc(int type);
9
10 %}
11 %error-verbose
12
13 %union {
14         int n;
15         char* str;
16         struct tree* tr;        
17 }
18
19 %right <n> '!'
20 %token <str> CONST
21 %token <n> NUM
22 %token <str> VAR
23 %token <n> KW_DISCARD
24 %token <n> KW_PIPE KW_MAIL KW_COPY KW_FILTER
25 %token '(' ')' '{' '}' ';'
26 %nonassoc KW_IF
27 %nonassoc KW_ELSE
28 %left ARROW
29 %left <n> EQ NEQ GE LE '<' '>' RE NRE
30 %left <n> '='
31 %left <n> '.'
32 %left <n> '+' '-' 
33 %left <n> '*' '/'
34 %left <n> '|'
35 %left <n> '^'
36 %left <n> '&'
37 %type <tr> input 
38 %type <tr> command 
39 %type <tr> next 
40 %type <tr> ass 
41 %type <tr> ass_right 
42 %type <tr> ass_right_p
43 %type <tr> cif
44 %type <tr> arrow 
45 %type <tr> cond
46 %type <n> rop 
47 %type <n> left
48 %type <n> right 
49 %type <tr> leaves 
50
51 %%
52 input:  /* empty */     { $$ = input_tree = tree_malloc(ST_EMPTY); }
53         | command input {       $$ = tree_malloc(ST_BLOCK); 
54                                 $$->pt.block.head = $1;
55                                 $$->pt.block.tail = $2;
56
57                                 input_tree = $$;
58                         } 
59 ;
60
61 command:         ';' { $$ = tree_malloc(ST_EMPTY); }
62                 | '{' command next '}'  {
63                                                 $$ = tree_malloc(ST_BLOCK); 
64                                                 $$->pt.block.head = $2;
65                                                 $$->pt.block.tail = $3; 
66                                         }
67                 | '{' '}' { $$ = tree_malloc(ST_EMPTY); }
68                 | cif
69                 | ass ';' { $$ = $1; }
70                 | arrow ';' { $$ = $1; }
71                 
72         
73 ;
74
75 next:   /* empty */ {$$ = tree_malloc(ST_EMPTY); }
76         | command
77         
78
79 ;
80
81 cif:    KW_IF cond command KW_ELSE command      { 
82                                         $$ = tree_malloc(ST_IF);
83                                         $$->pt.tif.c = $2;
84                                         $$->pt.tif.i = $3;
85                                         $$->pt.tif.e = $5;
86                                 }
87         | KW_IF cond command    { 
88                                         $$ = tree_malloc(ST_IF);
89                                         $$->pt.tif.c = $2;
90                                         $$->pt.tif.i = $3;
91                                         $$->pt.tif.e = tree_malloc(ST_EMPTY); 
92                                 }
93 ;
94
95 cond:   '!' cond {
96                                 $$ = tree_malloc(ST_COND);
97                                 $$->pt.cond.left = $2;  
98                                 $$->pt.cond.right = NULL; 
99                                 $$->pt.cond.op = $1;    
100                                 $$->pt.cond.type = OP_BOOL;     
101                 }
102         | cond '|' cond {
103                                 $$ = tree_malloc(ST_COND);
104                                 $$->pt.cond.left = $1;  
105                                 $$->pt.cond.right = $3; 
106                                 $$->pt.cond.op = $2;    
107                                 $$->pt.cond.type = OP_BOOL;     
108                         }
109         | cond '&' cond {
110                                 $$ = tree_malloc(ST_COND);
111                                 $$->pt.cond.left = $1;  
112                                 $$->pt.cond.right = $3; 
113                                 $$->pt.cond.op = $2;    
114                                 $$->pt.cond.type = OP_BOOL;     
115                         }
116         | cond '^' cond {
117                                 $$ = tree_malloc(ST_COND);
118                                 $$->pt.cond.left = $1;  
119                                 $$->pt.cond.right = $3; 
120                                 $$->pt.cond.op = $2;    
121                                 $$->pt.cond.type = OP_BOOL;     
122
123                         }
124         | '(' cond ')' { $$ = $2; }
125         | ass_right rop ass_right       {
126                                                 $$ = tree_malloc(ST_COND);
127                                                 $$->pt.cond.left = $1;  
128                                                 $$->pt.cond.right = $3; 
129                                                 $$->pt.cond.op = $2;    
130                                                 $$->pt.cond.type = OP_REL;      
131                                         }
132         | ass_right {
133                                 $$ = tree_malloc(ST_COND);
134                                 $$->pt.cond.left = $1;  
135                                 $$->pt.cond.type = JUST_BOOL;   
136                 }
137 ;
138
139 rop:    '>'
140         | '<'
141         | EQ
142         | NEQ
143         | LE
144         | GE
145         | RE
146         | NRE
147 ;
148
149 ass:
150         VAR '=' ass_right_p     {
151                                         $$ = tree_malloc(ST_ASS);
152
153                                         $$->pt.ass.left = tree_malloc(ST_LEAF);
154                                         $$->pt.ass.left->pt.leaf.type = L_VAR;
155                                         $$->pt.ass.left->pt.leaf.value = $1;
156                                         $$->pt.ass.left->pt.leaf.n = find_var($1, var_hash);
157                                         $$->pt.ass.right = $3;
158                                 }
159 ;
160
161 leaves:         VAR     {
162                                 $$ = tree_malloc(ST_LEAF);
163                                 $$->pt.leaf.type = L_VAR;
164                                 $$->pt.leaf.value = $1;
165                                 $$->pt.leaf.n = find_var($1, var_hash);
166                         }
167                 | CONST { 
168                                 $$ = tree_malloc(ST_LEAF);
169                                 $$->pt.leaf.type = L_CONST;
170                                 $$->pt.leaf.value = $1;
171                                 $$->pt.leaf.n = store_const($1);
172                         }
173 ;
174
175 arrow:  left ARROW right ass_right  {
176                                         $$ = tree_malloc(ST_ARROW);
177                                         $$->pt.arrow.s = $4;
178                                         $$->pt.arrow.left = $1;
179                                         $$->pt.arrow.right = $3;
180                                 }
181         | left ARROW KW_DISCARD         { //FIXME: actually left does not make sense here 
182                                         $$ = tree_malloc(ST_ARROW);
183                                         $$->pt.arrow.s = NULL;
184                                         $$->pt.arrow.left = K_EMPTY;
185                                         $$->pt.arrow.right = K_DISCARD;
186                                 }
187 ;
188
189 left:   /* empty */ { $$ = K_EMPTY;}
190         | KW_COPY { $$ = K_COPY; }
191
192 ;
193
194 right:  /* empty */ { $$ = K_EMPTY; }
195         | KW_PIPE { $$ = K_PIPE; }
196         | KW_MAIL { $$ = K_MAIL; }
197         | KW_FILTER { $$ = K_FILTER; }
198 ;
199
200 ass_right_p:    '(' ass_right ')'       {$$ = $2; }
201                 | ass_right     {$$ = $1; }
202 ;
203
204 ass_right:      leaves
205                 | ass_right '.' ass_right       {
206                                         $$ = tree_malloc(ST_OP);
207                                         $$->pt.op.op = $2;
208                                         $$->pt.op.left = $1;    
209                                         $$->pt.op.right = $3;   
210                                 }
211                 | ass_right '+' ass_right       {
212                                         $$ = tree_malloc(ST_OP);
213                                         $$->pt.op.op = $2;
214                                         $$->pt.op.left = $1;    
215                                         $$->pt.op.right = $3;   
216                                 }
217                 | ass_right '-' ass_right       {
218                                         $$ = tree_malloc(ST_OP);
219                                         $$->pt.op.op = $2;
220                                         $$->pt.op.left = $1;    
221                                         $$->pt.op.right = $3;   
222                                 }
223                 | ass_right '*' ass_right       {
224                                         $$ = tree_malloc(ST_OP);
225                                         $$->pt.op.op = $2;
226                                         $$->pt.op.left = $1;    
227                                         $$->pt.op.right = $3;   
228                                 }
229                 | ass_right '/' ass_right       {
230                                         $$ = tree_malloc(ST_OP);
231                                         $$->pt.op.op = $2;
232                                         $$->pt.op.left = $1;    
233                                         $$->pt.op.right = $3;   
234                                 }
235 ;
236
237 %%
238
239 struct tree* 
240 tree_malloc(int type)
241 {
242         struct tree* temp;
243         temp = xmalloc(sizeof (struct tree));
244         temp->st=type;
245
246         return temp;
247 }
248
249 void
250 yyerror (char const *s)
251 {
252         fprintf (stderr, "Line %d: %s\n", line, s);
253 }