]> mj.ucw.cz Git - umpf.git/blob - cond.y
7355a429a88bb4b9f4ef9c112fee39f165ab1d8b
[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 = NULL; }
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 */ {$$ = NULL; }
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 = NULL;
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
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
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
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
120                         }
121         | '(' cond ')' { $$ = $2; }
122         | ass_right rop ass_right       {
123                                                 $$ = tree_malloc(ST_COND);
124                                                 $$->pt.cond.left = $1;  
125                                                 $$->pt.cond.right = $3; 
126                                                 $$->pt.cond.op = $2;    
127                                         }
128 ;
129
130 rop:    '>'
131         | '<'
132         | EQ
133         | NEQ
134         | LE
135         | GE
136         | RE
137         | NRE
138 ;
139
140 ass:
141         VAR '=' ass_right       {
142                                         $$ = tree_malloc(ST_ASS);
143
144                                         $$->pt.ass.left = tree_malloc(ST_LEAF);
145                                         $$->pt.ass.left->pt.leaf.type = L_VAR;
146                                         $$->pt.ass.left->pt.leaf.value = $1;
147
148                                         $$->pt.ass.right = $3;
149                                 }
150 ;
151
152 leaves:         | VAR   {
153                                 $$ = tree_malloc(ST_LEAF);
154                                 $$->pt.leaf.type = L_VAR;
155                                 $$->pt.leaf.value = $1;
156                         }
157                 | CONST { 
158                                 $$ = tree_malloc(ST_LEAF);
159                                 $$->pt.leaf.type = L_CONST;
160                                 $$->pt.leaf.value = $1;
161                         }
162 ;
163
164 arrow:  left ARROW right ass_right  {
165                                         $$ = tree_malloc(ST_ARROW);
166                                         $$->pt.arrow.s = $4;
167                                         $$->pt.arrow.kw_left = $1;
168                                         $$->pt.arrow.kw_right = $3;
169                                 }
170 ;
171
172 left:   /* empty */ {$$=NULL;}
173         | KW_COPY { $$ = "copy"; }
174
175 ;
176
177 right:  /* empty */ {$$ = NULL; }
178         | KW_PIPE { $$ = "pipe"; }
179         | KW_MAIL { $$ = "mail"; }
180 ;
181
182 ass_right:      leaves
183                 | ass_right '.' ass_right       {
184                                         $$ = tree_malloc(ST_OP);
185                                         $$->pt.op.op = '.';
186                                         $$->pt.op.left = $1;    
187                                         $$->pt.op.right = $3;   
188                                 }
189 ;
190
191 %%
192
193 struct tree* 
194 tree_malloc(int type)
195 {
196         struct tree* temp;
197         temp = xmalloc(sizeof (struct tree));
198         temp->st=type;
199
200         return temp;
201 }
202
203 void
204 yyerror (char const *s)
205 {
206         fprintf (stderr, "Line %d: %s\n", line, s);
207 }