Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / cpp.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 /*      EXPRESSION SYNTAX PARSER        */
6
7 %lexical        LLlex;
8 %start          If_expr, if_expression;
9
10 {
11 #include        "arith.h"
12 #include        "LLlex.h"
13
14 extern arith ifval;
15 }
16
17 if_expression
18 { int is_unsigned = 0; }
19 :
20         constant_expression(&ifval, &is_unsigned)
21 ;
22
23 /* 7.1 */
24 primary(arith *pval; int *is_uns;)
25 :
26         constant(pval, is_uns)
27 |
28         '(' expression(pval, is_uns) ')'
29 ;
30
31 unary(arith *pval; int *is_uns;)
32         {int oper;}
33 :
34         unop(&oper)
35         unary(pval, is_uns)
36         { ch3mon(oper, pval, is_uns); }
37 |
38         primary(pval, is_uns)
39 ;
40
41 binary_expression(int maxrank; arith *pval; int *is_uns;)
42         {int oper; arith val1; int u;}
43 :
44         unary(pval, is_uns)
45         [%while (rank_of(DOT) <= maxrank)
46                 binop(&oper)
47                 binary_expression(rank_of(oper)-1, &val1, &u)
48                 {
49                         ch3bin(pval, is_uns, oper, val1, u);
50                 }
51         ]*
52 ;
53
54 /* 7.13 */
55 conditional_expression(arith *pval; int *is_uns)
56         {arith val1 = 0, val2 = 0; int u;}
57 :
58         /* allow all binary operators */
59         binary_expression(rank_of('?') - 1, pval, is_uns)
60         [       '?'
61                 expression(&val1, is_uns)
62                 ':'
63                 assignment_expression(&val2, &u)
64                 { if (*pval) *pval = val1;
65                   else { *pval = val2; *is_uns = u; }
66                 }
67         ]?
68 ;
69
70 /* 7.14 */
71 assignment_expression(arith *pval; int *is_uns)
72 :
73         conditional_expression(pval, is_uns)
74 ;
75
76 /* 7.15 */
77 expression(arith *pval; int *is_uns)
78         {arith val1;
79          int is_uns1;
80         }
81 :
82         assignment_expression(pval,is_uns)
83         [       ','
84                 assignment_expression(&val1, &is_uns1)
85                 {
86                         ch3bin(pval, is_uns, ',', val1, is_uns1);
87                 }
88         ]*
89 ;
90
91 unop(int *oper;) :
92         [ '-' | '!' | '~' ]
93         {*oper = DOT;}
94 ;
95
96 multop:
97         '*' | '/' | '%'
98 ;
99
100 addop:
101         '+' | '-'
102 ;
103
104 shiftop:
105         LEFT | RIGHT
106 ;
107
108 relop:
109         '<' | '>' | LESSEQ | GREATEREQ
110 ;
111
112 eqop:
113         EQUAL | NOTEQUAL
114 ;
115
116 arithop:
117         multop | addop | shiftop
118 |
119         '&' | '^' | '|'
120 ;
121
122 binop(int *oper;) :
123         [ arithop | relop | eqop | AND | OR ]
124         {*oper = DOT;}
125 ;
126
127 constant(arith *pval; int *is_uns) :
128         INTEGER
129         {*pval = dot.tk_val;
130          *is_uns = dot.tk_unsigned;
131         }
132 ;
133
134 constant_expression (arith *pval; int *is_uns) :
135         assignment_expression(pval, is_uns)
136 ;