Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / cemcom / expression.g
1 /*
2  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3  * See the copyright notice in the ACK home directory, in the file "Copyright".
4  */
5 /* $Id: expression.g,v 3.12 1997/02/21 17:10:41 ceriel Exp $ */
6 /*      EXPRESSION SYNTAX PARSER        */
7
8 {
9 #include        "arith.h"
10 #include        "LLlex.h"
11 #include        "type.h"
12 #include        "idf.h"
13 #include        "label.h"
14 #include        "expr.h"
15 #include        "code.h"
16 #include        "noRoption.h"
17
18 extern char options[];
19 extern struct expr *intexpr();
20 }
21
22 /* 7.1 */
23 primary(register struct expr **expp;) :
24         IDENTIFIER
25         {dot2expr(expp);}
26 |
27         %illegal TYPE_IDENTIFIER
28 |
29         constant(expp)
30 |
31         STRING
32         {dot2expr(expp);}
33 |
34         '(' expression(expp) ')'
35         {(*expp)->ex_flags |= EX_PARENS;}
36 ;
37
38 secundary(register struct expr **expp;) :
39         primary(expp)
40         [
41                 index_pack(expp)
42         |
43                 parameter_pack(expp)
44         |
45                 selection(expp)
46         ]*
47 ;
48
49 index_pack(struct expr **expp;)
50         {struct expr *e1;}
51 :
52         '[' expression(&e1) ']'
53         {ch7bin(expp, '[', e1);}
54 ;
55
56 parameter_pack(struct expr **expp;)
57         {struct expr *e1 = 0;}
58 :
59         '(' parameter_list(&e1)? ')'
60         {ch7bin(expp, '(', e1);}
61 ;
62
63 selection(struct expr **expp;)
64         {int oper; struct idf *idf;}
65 :
66         [ '.' | ARROW ]
67         {oper = DOT;}
68         identifier(&idf)
69         {ch7sel(expp, oper, idf);}
70 ;
71
72 parameter_list(struct expr **expp;)
73         {struct expr *e1 = 0;}
74 :
75         assignment_expression(expp)
76         {any2opnd(expp, PARCOMMA);}
77         [ %persistent
78                 ','
79                 assignment_expression(&e1)
80                 {any2opnd(&e1, PARCOMMA);}
81                 {ch7bin(expp, PARCOMMA, e1);}
82         ]*
83 ;
84
85 /* 7.2 */
86 postfixed(struct expr **expp;)
87         {int oper;}
88 :
89         secundary(expp)
90         [
91                 postop(&oper)
92                 {ch7incr(expp, oper);}
93         |
94                 empty
95         ]
96 ;
97
98 %first  first_of_type_specifier, type_specifier;
99
100 unary(register struct expr **expp;)
101         {struct type *tp; int oper;}
102 :
103 %if (first_of_type_specifier(AHEAD) && AHEAD != IDENTIFIER)
104         cast(&tp) unary(expp)
105         {       ch7cast(expp, CAST, tp);
106                 (*expp)->ex_flags |= EX_CAST;
107         }
108 |
109         postfixed(expp)
110 |
111         unop(&oper) unary(expp)
112         {ch7mon(oper, expp);}
113 |
114         size_of(expp)
115 ;
116
117 size_of(register struct expr **expp;)
118         {struct type *tp;}
119 :
120         SIZEOF
121         [%if (first_of_type_specifier(AHEAD) && AHEAD != IDENTIFIER)
122                 cast(&tp)
123                 {
124                         *expp = intexpr(size_of_type(tp, "type"), INT);
125                         (*expp)->ex_flags |= EX_SIZEOF;
126                 }
127         |
128                 unary(expp)
129                 {ch7mon(SIZEOF, expp);}
130         ]
131 ;
132
133 /* 7.3-7.12 */
134 /*      The set of operators in C is stratified in 15 levels, with level
135         N being treated in RM 7.N.  In principle each operator is
136         assigned a rank, ranging from 1 to 15.  Such an expression can
137         be parsed by a construct like:
138                 binary_expression(int maxrank;)
139                         {int oper;}
140                 :
141                         binary_expression(maxrank - 1)
142                         [%if (rank_of(DOT) <= maxrank)
143                                 binop(&oper)
144                                 binary_expression(rank_of(oper)-1)
145                         ]?
146                 ;
147         except that some call of 'unary' is necessary, depending on the
148         grammar.
149         
150         This simple view is marred by three complications:
151         1.      Level 15 (comma operator) is not allowed in many
152                 contexts and is different.
153         2.      Level 13 (conditional operator) is a ternary operator,
154                 which does not fit this scheme at all.
155         3.      Level 14 (assignment operators) group right-to-left, as
156                 opposed to 2-12, which group left-to-right (or are
157                 immaterial).
158         4.      The operators in level 14 start with operators in levels
159                 2-13 (RM 7.14: The two parts of a compound assignment
160                 operator are separate tokens.)  This causes LL1 problems.
161         This forces us to have four rules:
162                 binary_expression       for level 2-12
163                 conditional_expression  for level 13
164                 assignment_expression   for level 14 and
165                 expression              for the most general expression
166 */
167
168 binary_expression(int maxrank; struct expr **expp;)
169         {int oper; struct expr *e1;}
170 :
171         unary(expp)
172         [%while (rank_of(DOT) <= maxrank && AHEAD != '=')
173                 /*      '?', '=', and ',' are no binops, and the test
174                         for AHEAD != '=' keeps the other assignment
175                         operators out
176                 */
177                 binop(&oper)
178                 binary_expression(rank_of(oper)-1, &e1)
179                 {
180                         ch7bin(expp, oper, e1);
181                 }
182         ]*
183 ;
184
185 /* 7.13 */
186 conditional_expression(struct expr **expp;)
187 /*      There is some unfortunate disagreement about what is allowed
188         between the '?' and the ':' of a conditional_expression.
189         Although the Ritchie compiler does not even allow
190         conditional_expressions there, some other compilers (e.g., VAX)
191         accept a full assignment_expression there, and programs
192         (like, e.g., emacs) rely on it. So we have little choice.
193 */
194         {struct expr *e1 = 0, *e2 = 0;}
195 :
196         /* allow all binary operators */
197         binary_expression(rank_of('?') - 1, expp)
198         [       '?'
199                 expression(&e1)
200                 {
201 #ifndef NOROPTION
202                         check_conditional(e1, '?', "between ? and :");
203 #endif
204                 }
205                 ':'
206                 assignment_expression(&e2)
207                 {       
208 #ifndef NOROPTION
209                         check_conditional(e2, '=', "after :");
210 #endif
211                         ch7bin(&e1, ':', e2);
212                         opnd2test(expp, '?');
213                         ch7bin(expp, '?', e1);
214                 }
215         ]?
216 ;
217
218 /* 7.14 */
219 assignment_expression(struct expr **expp;)
220         {
221                 int oper;
222                 struct expr *e1 = 0;
223         }
224 :
225         conditional_expression(expp)
226         [%prefer        /* (rank_of(DOT) <= maxrank) for any asgnop */
227                 asgnop(&oper)
228                 assignment_expression(&e1)
229                 {ch7asgn(expp, oper, e1);}
230         |
231                 empty           /* LLgen artefact ??? */
232         ]
233 ;
234
235 /* 7.15 */
236 expression(struct expr **expp;)
237         {struct expr *e1;}
238 :
239         assignment_expression(expp)
240         [       ','
241                 assignment_expression(&e1)
242                 {
243                         ch7bin(expp, ',', e1);
244                 }
245         ]*
246 ;
247
248 unop(int *oper;) :
249         ['*' | '&' | '-' | '!' | '~' | PLUSPLUS | MINMIN]
250         {*oper = DOT;}
251 ;
252
253 postop(int *oper;):
254         PLUSPLUS {*oper = POSTINCR;}
255 |
256         MINMIN {*oper = POSTDECR;}
257 ;
258
259 multop:
260         '*' | '/' | '%'
261 ;
262
263 addop:
264         '+' | '-'
265 ;
266
267 shiftop:
268         LEFT | RIGHT
269 ;
270
271 relop:
272         '<' | '>' | LESSEQ | GREATEREQ
273 ;
274
275 eqop:
276         EQUAL | NOTEQUAL
277 ;
278
279 arithop:
280         multop | addop | shiftop
281 |
282         '&' | '^' | '|'
283 ;
284
285 binop(int *oper;) :
286         [ arithop | relop | eqop | AND | OR ]
287         {*oper = DOT;}
288 ;
289
290 asgnop(register int *oper;):
291         '=' {*oper = DOT;}
292 |
293         '+' '=' {*oper = PLUSAB;}
294 |
295         '-' '=' {*oper = MINAB;}
296 |
297         '*' '=' {*oper = TIMESAB;}
298 |
299         '/' '=' {*oper = DIVAB;}
300 |
301         '%' '=' {*oper = MODAB;}
302 |
303         LEFT '=' {*oper = LEFTAB;}
304 |
305         RIGHT '=' {*oper = RIGHTAB;}
306 |
307         '&' '=' {*oper = ANDAB;}
308 |
309         '^' '=' {*oper = XORAB;}
310 |
311         '|' '=' {*oper = ORAB;}
312 ;
313
314 constant(struct expr **expp;) :
315 [
316         INTEGER
317 |
318         FLOATING
319 ]       {dot2expr(expp);}
320 ;
321
322 /* 15 */
323 constant_expression (struct expr **expp;) :
324         assignment_expression(expp)
325         {chk_cst_expr(expp);}
326 ;
327
328 identifier(struct idf **idfp;) :
329 [
330         IDENTIFIER
331 |
332         TYPE_IDENTIFIER
333 ]
334         {
335                 *idfp = dot.tk_idf;
336         }
337 ;