]> mj.ucw.cz Git - umpf.git/blob - cond.y
42a663d1f9947ac78f022f867e894d51d4e71437
[umpf.git] / cond.y
1 %{
2
3 #include <stdio.h>
4 #include <string.h>
5
6 #include "brum.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 %token <str> CONST
20 %token <n> NUM
21 %token <str> VAR
22 %token <str> KW_PIPE KW_MAIL KW_COPY
23 %token '(' ')' '{' '}' ';'
24 %nonassoc KW_IF
25 %nonassoc KW_ELSE
26 %left ARROW
27 %left <n> EQ NEQ GE LE '<' '>' RE NRE
28 %left '='
29 %left '.'
30 %left '+' '-' 
31 %left '*' '/'
32 %left <n> '|'
33 %left <n> '^'
34 %left <n> '&'
35 %left <n> '!'
36 %type <tr> input 
37 %type <tr> command 
38 %type <tr> next 
39 %type <tr> ass 
40 %type <tr> ass_right 
41 %type <tr> cif
42 %type <tr> arrow 
43 %type <tr> cond
44 %type <n> rop 
45 %type <str> left
46 %type <str> right 
47 %type <tr> leaves 
48
49 %%
50 input:  /* empty */     { $$ = input_tree = tree_malloc(ST_EMPTY); }
51         | command input {       $$ = tree_malloc(ST_BLOCK); 
52                                 $$->pt.block.head = $1;
53                                 $$->pt.block.tail = $2;
54
55                                 input_tree = $$;
56                         } 
57 ;
58
59 command:         ';' { $$ = tree_malloc(ST_EMPTY); }
60                 | '{' command next '}'  {
61                                                 $$ = tree_malloc(ST_BLOCK); 
62                                                 $$->pt.block.head = $2;
63                                                 $$->pt.block.tail = $3; 
64                                         }
65                 | '{' '}' { $$ = tree_malloc(ST_EMPTY); }
66                 | cif
67                 | ass ';' { $$ = $1; }
68                 | arrow ';' { $$ = $1; }
69                 
70         
71 ;
72
73 next:   /* empty */ {$$ = tree_malloc(ST_EMPTY); }
74         | command
75         
76
77 ;
78
79 cif:    KW_IF cond command KW_ELSE command      { 
80                                         $$ = tree_malloc(ST_IF);
81                                         $$->pt.tif.c = $2;
82                                         $$->pt.tif.i = $3;
83                                         $$->pt.tif.e = $5;
84                                 }
85         | KW_IF cond command    { 
86                                         $$ = tree_malloc(ST_IF);
87                                         $$->pt.tif.c = $2;
88                                         $$->pt.tif.i = $3;
89                                         $$->pt.tif.e = tree_malloc(ST_EMPTY); 
90                                 }
91 ;
92
93 cond:   '!' cond {
94                                 $$ = tree_malloc(ST_COND);
95                                 $$->pt.cond.left = $2;  
96                                 $$->pt.cond.right = NULL; 
97                                 $$->pt.cond.op = $1;    
98                                 $$->pt.cond.type = OP_BOOL;     
99                 }
100         | cond '|' cond {
101                                 $$ = tree_malloc(ST_COND);
102                                 $$->pt.cond.left = $1;  
103                                 $$->pt.cond.right = $3; 
104                                 $$->pt.cond.op = $2;    
105                                 $$->pt.cond.type = OP_BOOL;     
106                         }
107         | cond '&' cond {
108                                 $$ = tree_malloc(ST_COND);
109                                 $$->pt.cond.left = $1;  
110                                 $$->pt.cond.right = $3; 
111                                 $$->pt.cond.op = $2;    
112                                 $$->pt.cond.type = OP_BOOL;     
113                         }
114         | cond '^' cond {
115                                 $$ = tree_malloc(ST_COND);
116                                 $$->pt.cond.left = $1;  
117                                 $$->pt.cond.right = $3; 
118                                 $$->pt.cond.op = $2;    
119                                 $$->pt.cond.type = OP_BOOL;     
120
121                         }
122         | '(' cond ')' { $$ = $2; }
123         | ass_right rop ass_right       {
124                                                 $$ = tree_malloc(ST_COND);
125                                                 $$->pt.cond.left = $1;  
126                                                 $$->pt.cond.right = $3; 
127                                                 $$->pt.cond.op = $2;    
128                                                 $$->pt.cond.type = OP_REL;      
129                                         }
130 ;
131
132 rop:    '>'
133         | '<'
134         | EQ
135         | NEQ
136         | LE
137         | GE
138         | RE
139         | NRE
140 ;
141
142 ass:
143         VAR '=' ass_right       {
144                                         $$ = tree_malloc(ST_ASS);
145
146                                         $$->pt.ass.left = tree_malloc(ST_LEAF);
147                                         $$->pt.ass.left->pt.leaf.type = L_VAR;
148                                         $$->pt.ass.left->pt.leaf.value = $1;
149
150                                         $$->pt.ass.right = $3;
151                                 }
152 ;
153
154 leaves:         VAR     {
155                                 $$ = tree_malloc(ST_LEAF);
156                                 $$->pt.leaf.type = L_VAR;
157                                 $$->pt.leaf.value = $1;
158                         }
159                 | CONST { 
160                                 $$ = tree_malloc(ST_LEAF);
161                                 $$->pt.leaf.type = L_CONST;
162                                 $$->pt.leaf.value = $1;
163                         }
164 ;
165
166 arrow:  left ARROW right ass_right  {
167                                         $$ = tree_malloc(ST_ARROW);
168                                         $$->pt.arrow.s = $4;
169                                         $$->pt.arrow.kw_left = $1;
170                                         $$->pt.arrow.kw_right = $3;
171                                 }
172 ;
173
174 left:   /* empty */ { $$ = NULL;}
175         | KW_COPY { $$ = "copy"; }
176
177 ;
178
179 right:  /* empty */ { $$ = NULL; }
180         | KW_PIPE { $$ = "pipe"; }
181         | KW_MAIL { $$ = "mail"; }
182 ;
183
184 ass_right:      leaves
185                 | ass_right '.' ass_right       {
186                                         $$ = tree_malloc(ST_OP);
187                                         $$->pt.op.op = '.';
188                                         $$->pt.op.left = $1;    
189                                         $$->pt.op.right = $3;   
190                                 }
191 ;
192
193 %%
194
195 struct tree* 
196 tree_malloc(int type)
197 {
198         struct tree* temp;
199         temp = xmalloc(sizeof (struct tree));
200         temp->st=type;
201
202         return temp;
203 }
204
205 void
206 yyerror (char const *s)
207 {
208         fprintf (stderr, "Line %d: %s\n", line, s);
209 }