Pristine Ack-5.5
[Ack-5.5.git] / util / cpp / expression.g
1 /* $Id: expression.g,v 1.5 1994/06/24 10:18:15 ceriel Exp $ */
2 /*
3  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4  * See the copyright notice in the ACK home directory, in the file "Copyright".
5  */
6 /*      EXPRESSION SYNTAX PARSER        */
7
8 %lexical        LLlex;
9 %start          If_expr, if_expression;
10
11 {
12 #include        "LLlex.h"
13 #include        <em_arith.h>
14
15 extern arith ifval;
16 }
17
18 if_expression
19 :
20         constant_expression(&ifval)
21 ;
22
23 /* 7.1 */
24 primary(arith *pval;)
25 :
26         constant(pval)
27 |
28         '(' expression(pval) ')'
29 ;
30
31 unary(arith *pval;)
32         {int oper;}
33 :
34         unop(&oper)
35         unary(pval)
36         { ch7mon(oper, pval); }
37 |
38         primary(pval)
39 ;
40
41 binary_expression(int maxrank; arith *pval;)
42         {int oper; arith val1;}
43 :
44         unary(pval)
45         [%while (rank_of(DOT) <= maxrank)
46                 binop(&oper)
47                 binary_expression(rank_of(oper)-1, &val1)
48                 {
49                         ch7bin(pval, oper, val1);
50                 }
51         ]*
52 ;
53
54 /* 7.13 */
55 conditional_expression(arith *pval;)
56         {arith val1 = 0, val2 = 0;}
57 :
58         /* allow all binary operators */
59         binary_expression(rank_of('?') - 1, pval)
60         [       '?'
61                 expression(&val1)
62                 ':'
63                 assignment_expression(&val2)
64                 { *pval = (*pval ? val1 : val2); }
65         ]?
66 ;
67
68 /* 7.14 */
69 assignment_expression(arith *pval;)
70 :
71         conditional_expression(pval)
72 ;
73
74 /* 7.15 */
75 expression(arith *pval;)
76         {arith val1;}
77 :
78         assignment_expression(pval)
79         [       ','
80                 assignment_expression(&val1)
81                 {
82                         ch7bin(pval, ',', val1);
83                 }
84         ]*
85 ;
86
87 unop(int *oper;) :
88         [ '-' | '!' | '~' ]
89         {*oper = DOT;}
90 ;
91
92 multop:
93         '*' | '/' | '%'
94 ;
95
96 addop:
97         '+' | '-'
98 ;
99
100 shiftop:
101         LEFT | RIGHT
102 ;
103
104 relop:
105         '<' | '>' | LESSEQ | GREATEREQ
106 ;
107
108 eqop:
109         EQUAL | NOTEQUAL
110 ;
111
112 arithop:
113         multop | addop | shiftop
114 |
115         '&' | '^' | '|'
116 ;
117
118 binop(int *oper;) :
119         [ arithop | relop | eqop | AND | OR ]
120         {*oper = DOT;}
121 ;
122
123 constant(arith *pval;) :
124         INTEGER
125         {*pval = dot.tk_val;}
126 ;
127
128 constant_expression (arith *pval;) :
129         assignment_expression(pval)
130 ;