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