Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / cemcom.ansi / 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 1.14 1994/06/27 07:59:47 ceriel Exp $ */
6 /*      EXPRESSION SYNTAX PARSER        */
7
8 {
9 #include        <alloc.h>
10 #include        "lint.h"
11 #include        "debug.h"
12 #include        <flt_arith.h>
13 #include        "arith.h"
14 #include        "LLlex.h"
15 #include        "type.h"
16 #include        "idf.h"
17 #include        "label.h"
18 #include        "expr.h"
19 #include        "code.h"
20 #include        "sizes.h"
21
22 extern struct expr *intexpr();
23 int InSizeof = 0;       /* inside a sizeof- expression */
24 int ResultKnown = 0;    /* result of the expression is already known */
25
26 /* Since the grammar in the standard is not LL(n), it is modified so that
27  * it accepts basically the same grammar. This means that there is no 1-1
28  * mapping from the grammar in the standard to the grammar given here.
29  * Such is life.
30  */
31 }
32
33 /* 3.3.1 */
34 primary(register struct expr **expp;) :
35         IDENTIFIER
36         {dot2expr(expp);}
37 |
38         constant(expp)
39 |
40         string(expp)
41 |
42         '(' expression(expp) ')'
43         { (*expp)->ex_flags |= EX_PARENS; }
44 ;
45
46
47 /* Character string literals that are adjacent tokens
48  * are concatenated into a single character string
49  * literal.
50  */
51 string(register struct expr **expp;)
52         {       register int i, len;
53                 register char *str;
54                 register int fund;
55         }
56 :
57         STRING
58         {       str = dot.tk_bts;
59                 len = dot.tk_len;
60                 fund = dot.tk_fund;
61         }
62         [
63                 STRING
64                 {       /* A pasted string keeps the type of the first
65                          * string literal.
66                          * The pasting of normal strings and wide
67                          * character strings are stated as having an
68                          * undefined behaviour.
69                          */
70                         if (dot.tk_fund != fund)
71                                 warning("illegal pasting of string literals");
72                         str = Realloc(str, (unsigned) (--len + dot.tk_len));
73                         for (i = 0; i < dot.tk_len; i++)
74                                 str[len++] = dot.tk_bts[i];
75                 }
76         ]*
77         { string2expr(expp, str, len); }
78 ;
79
80 /* 3.3.2 */
81 postfix_expression(register struct expr **expp;)
82         { int oper; 
83           struct expr *e1 = 0;
84           struct idf *idf;
85         }
86 :
87         primary(expp)
88         [
89                 '[' expression(&e1) ']'
90                         { ch3bin(expp, '[', e1); e1 = 0; }
91         |
92                 '(' parameter_list(&e1)? ')'
93                         { ch3bin(expp, '(', e1); call_proto(expp); e1 = 0; }
94         |
95                 [ '.' | ARROW ]                 { oper = DOT; }
96                 identifier(&idf)                { ch3sel(expp, oper, idf); }
97         |
98                 [
99                         PLUSPLUS        { oper = POSTINCR; }
100                 |
101                         MINMIN          { oper = POSTDECR; }
102                 ]
103                     { ch3incr(expp, oper); }
104         ]*
105 ;
106
107 parameter_list(struct expr **expp;)
108         {struct expr *e1 = 0;}
109 :
110         assignment_expression(expp)
111         {any2opnd(expp, PARCOMMA);}
112         [ %persistent
113                 ','
114                 assignment_expression(&e1)
115                 {any2opnd(&e1, PARCOMMA);}
116                 {ch3bin(expp, PARCOMMA, e1);}
117         ]*
118 ;
119
120 %first  first_of_type_specifier, type_specifier;
121
122 /* 3.3.3 & 3.3.4 */
123 unary(register struct expr **expp;)
124         {struct type *tp; int oper;}
125 :
126 %if (first_of_type_specifier(AHEAD) && AHEAD != IDENTIFIER)
127         cast(&tp) unary(expp)
128         {       ch3cast(expp, CAST, tp);
129                 (*expp)->ex_flags |= EX_CAST;
130                 if (int_size != pointer_size)
131                         (*expp)->ex_flags &= ~EX_PTRDIFF;
132         }
133 |
134         postfix_expression(expp)
135 |
136         unop(&oper) unary(expp)
137         {ch3mon(oper, expp);}
138 |
139         size_of(expp)
140 ;
141
142 /* When an identifier is used in a sizeof()-expression, we must stil not
143  * mark it as used.
144  * extern int i;  ....  sizeof(i)  .... need not have a definition for i
145  */
146 size_of(register struct expr **expp;)
147         {struct type *tp;}
148 :
149         SIZEOF { InSizeof++; }  /* handle (sizeof(sizeof(int))) too */
150         [%if (first_of_type_specifier(AHEAD) && AHEAD != IDENTIFIER)
151                 cast(&tp)
152                 {
153                         *expp = intexpr(size_of_type(tp, "type"), UNSIGNED);
154                         (*expp)->ex_flags |= EX_SIZEOF;
155                 }
156         |
157                 unary(expp)
158                 {ch3mon(SIZEOF, expp);}
159         ]
160         { InSizeof--; }
161 ;
162
163 /* 3.3.5-3.3.17 */
164 /*      The set of operators in C is stratified in 15 levels, with level
165         N being treated in RM 7.N (although this is not the standard
166         anymore). The standard describes this in phrase-structure-grammar,
167         which we are unable to parse. The description that follows comes
168         from the old C-compiler.
169
170         In principle each operator is assigned a rank, ranging
171         from 1 to 15.  Such an expression can be parsed by a construct
172         like:
173                 binary_expression(int maxrank;)
174                         {int oper;}
175                 :
176                         binary_expression(maxrank - 1)
177                         [%if (rank_of(DOT) <= maxrank)
178                                 binop(&oper)
179                                 binary_expression(rank_of(oper)-1)
180                         ]?
181                 ;
182         except that some call of 'unary' is necessary, depending on the
183         grammar.
184         
185         This simple view is marred by three complications:
186         1.      Level 15 (comma operator) is not allowed in many
187                 contexts and is different.
188         2.      Level 13 (conditional operator) is a ternary operator,
189                 which does not fit this scheme at all.
190         3.      Level 14 (assignment operators) group right-to-left, as
191                 opposed to 2-12, which group left-to-right (or are
192                 immaterial).
193         4.      The operators in level 14 start with operators in levels
194                 2-13 (RM 7.14: The two parts of a compound assignment
195                 operator are separate tokens.)  This causes LL1 problems.
196         This forces us to have four rules:
197                 binary_expression       for level 2-12
198                 conditional_expression  for level 13
199                 assignment_expression   for level 14 and
200                 expression              for the most general expression
201 */
202
203 binary_expression(int maxrank; struct expr **expp;)
204         {int oper, OldResultKnown; struct expr *e1;}
205 :
206         unary(expp)
207         [%while (rank_of(DOT) <= maxrank )
208                 /*      '?', '=', and ',' are no binops
209                  */
210                 binop(&oper)
211                 { OldResultKnown = ResultKnown;
212                   if (oper == OR || oper == AND) {
213                           if (is_cp_cst(*expp) || is_fp_cst(*expp)) {
214                                   if (is_zero_cst(*expp)) {
215                                           if (oper == AND) ResultKnown++;
216                                   } else if (oper == OR) ResultKnown++;
217                           }
218                   }
219                 }
220                 binary_expression(rank_of(oper)-1, &e1)
221                 {
222                         ch3bin(expp, oper, e1);
223                         ResultKnown = OldResultKnown;
224                 }
225         ]*
226 ;
227
228 /* 3.3.15 */
229 conditional_expression(struct expr **expp;)
230         {struct expr *e1 = 0, *e2 = 0; int OldResultKnown, ConstExpr=0;}
231 :
232         /* allow all binary operators */
233         binary_expression(rank_of('?') - 1, expp)
234         [       '?'
235                 { OldResultKnown = ResultKnown;
236                   if (is_cp_cst(*expp) || is_fp_cst(*expp)) {
237                           ConstExpr++;
238                           if (is_zero_cst(*expp)) ResultKnown++;
239                   }
240                 }
241                 expression(&e1)
242                 ':'
243                 { if (ConstExpr) {
244                         if (OldResultKnown == ResultKnown) ResultKnown++;
245                         else ResultKnown = OldResultKnown;
246                   }
247                 }
248                 conditional_expression(&e2)
249                 {       
250                         ResultKnown = OldResultKnown;
251                         ch3bin(&e1, ':', e2);
252                         opnd2test(expp, '?');
253                         ch3bin(expp, '?', e1);
254                 }
255         ]?
256 ;
257
258 /* 3.3.16 */
259 assignment_expression(struct expr **expp;)
260         { int oper;
261           struct expr *e1 = 0;
262         }
263 :
264         conditional_expression(expp)
265         [
266                 asgnop(&oper)
267                 assignment_expression(&e1)
268                 {ch3asgn(expp, oper, e1);}
269         |
270                 empty           /* LLgen artefact ??? */
271         ]
272 ;
273
274 /* 3.3.17 */
275 expression(struct expr **expp;)
276         {struct expr *e1;}
277 :
278         assignment_expression(expp)
279         [       ','
280                 assignment_expression(&e1)
281                 {
282                         ch3bin(expp, ',', e1);
283                 }
284         ]*
285 ;
286
287 unop(int *oper;) :
288         ['*' | '&' | '-' | '+' | '!' | '~' | PLUSPLUS | MINMIN]
289         {   if (DOT == '&') DOT = ADDRESSOF;
290             *oper = DOT;
291         }
292 ;
293
294 multop:
295         '*' | '/' | '%'
296 ;
297
298 addop:
299         '+' | '-'
300 ;
301
302 shiftop:
303         LEFT | RIGHT
304 ;
305
306 relop:
307         '<' | '>' | LESSEQ | GREATEREQ
308 ;
309
310 eqop:
311         EQUAL | NOTEQUAL
312 ;
313
314 arithop:
315         multop | addop | shiftop
316 |
317         '&' | '^' | '|'
318 ;
319
320 binop(int *oper;) :
321         [ arithop | relop | eqop | AND | OR ]
322         {*oper = DOT;}
323 ;
324
325 asgnop(register int *oper;):
326         [ '=' | PLUSAB | MINAB | TIMESAB | DIVAB | MODAB 
327         | LEFTAB | RIGHTAB | ANDAB | XORAB | ORAB ]
328         { *oper = DOT; }
329
330 ;
331
332 constant(struct expr **expp;) :
333 [
334         INTEGER
335 |
336         FLOATING
337 ]       {dot2expr(expp);}
338 ;
339
340 /* 3.4 */
341 constant_expression (struct expr **expp;) :
342         conditional_expression(expp)
343         { chk_cst_expr(expp); }
344 ;
345
346 identifier(struct idf **idfp;) :
347 [ IDENTIFIER
348 | TYPE_IDENTIFIER
349 ]
350         { *idfp = dot.tk_idf; }
351 ;