1 | /* $Id: cpy.y,v 1.18 2010/02/25 15:49:00 ragge Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Copyright (c) 2004 Anders Magnusson (ragge@ludd.luth.se).
|
---|
5 | * All rights reserved.
|
---|
6 | *
|
---|
7 | * Redistribution and use in source and binary forms, with or without
|
---|
8 | * modification, are permitted provided that the following conditions
|
---|
9 | * are met:
|
---|
10 | * 1. Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * 3. The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /*
|
---|
31 | * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
|
---|
32 | *
|
---|
33 | * Redistribution and use in source and binary forms, with or without
|
---|
34 | * modification, are permitted provided that the following conditions
|
---|
35 | * are met:
|
---|
36 | *
|
---|
37 | * Redistributions of source code and documentation must retain the above
|
---|
38 | * copyright notice, this list of conditions and the following disclaimer.
|
---|
39 | * Redistributions in binary form must reproduce the above copyright
|
---|
40 | * notice, this list of conditions and the following disclaimer in the
|
---|
41 | * documentation and/or other materials provided with the distribution.
|
---|
42 | * All advertising materials mentioning features or use of this software
|
---|
43 | * must display the following acknowledgement:
|
---|
44 | * This product includes software developed or owned by Caldera
|
---|
45 | * International, Inc.
|
---|
46 | * Neither the name of Caldera International, Inc. nor the names of other
|
---|
47 | * contributors may be used to endorse or promote products derived from
|
---|
48 | * this software without specific prior written permission.
|
---|
49 | *
|
---|
50 | * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
|
---|
51 | * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
|
---|
52 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
53 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
54 | * DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
|
---|
55 | * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
56 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
57 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
58 | * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
|
---|
59 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
---|
60 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
---|
61 | * POSSIBILITY OF SUCH DAMAGE.
|
---|
62 | */
|
---|
63 |
|
---|
64 | %{
|
---|
65 |
|
---|
66 | #include "cpp.h"
|
---|
67 |
|
---|
68 | void yyerror(const char *);
|
---|
69 | int yylex(void);
|
---|
70 | int setd(int l, int r);
|
---|
71 |
|
---|
72 | #define EVALUNARY(tok, l, r) l.nd_val = tok r.nd_val; l.op = r.op
|
---|
73 | #define EVALBIN(tok, d, l, r) \
|
---|
74 | d.op = setd(l.op, r.op); d.nd_val = l.nd_val tok r.nd_val
|
---|
75 | #define EVALUBIN(tok, d, l, r, t) \
|
---|
76 | d.op = setd(l.op, r.op); \
|
---|
77 | if (d.op == NUMBER) d.nd_val = l.nd_val tok r.nd_val; \
|
---|
78 | else d.nd_uval = l.nd_uval tok r.nd_uval; \
|
---|
79 | if (t && d.op) d.op = NUMBER
|
---|
80 | #define XEVALUBIN(tok, d, l, r) \
|
---|
81 | if (r.nd_val) { EVALUBIN(tok, d, l, r, 0); } else d.op = 0
|
---|
82 | %}
|
---|
83 |
|
---|
84 | %term stop
|
---|
85 | %term EQ NE LE GE LS RS
|
---|
86 | %term ANDAND OROR IDENT NUMBER UNUMBER DEFINED
|
---|
87 | /*
|
---|
88 | * The following terminals are not used in the yacc code.
|
---|
89 | */
|
---|
90 | %term STRING WSPACE CMNT
|
---|
91 |
|
---|
92 | %left ','
|
---|
93 | %right '?' ':'
|
---|
94 | %left OROR
|
---|
95 | %left ANDAND
|
---|
96 | %left '|' '^'
|
---|
97 | %left '&'
|
---|
98 | %binary EQ NE
|
---|
99 | %binary '<' '>' LE GE
|
---|
100 | %left LS RS
|
---|
101 | %left '+' '-'
|
---|
102 | %left '*' '/' '%'
|
---|
103 | %right '!' '~' UMINUS
|
---|
104 | %left '('
|
---|
105 |
|
---|
106 | %union {
|
---|
107 | struct nd node;
|
---|
108 | }
|
---|
109 |
|
---|
110 | %type <node> term e NUMBER UNUMBER
|
---|
111 |
|
---|
112 | %%
|
---|
113 | S: e '\n' {
|
---|
114 | if ($1.op == 0)
|
---|
115 | error("division by zero");
|
---|
116 | return $1.nd_val;
|
---|
117 | }
|
---|
118 |
|
---|
119 | e: e '*' e
|
---|
120 | { EVALUBIN(*, $$, $1, $3, 0); }
|
---|
121 | | e '/' e
|
---|
122 | { XEVALUBIN(/, $$, $1, $3); }
|
---|
123 | | e '%' e
|
---|
124 | { XEVALUBIN(%, $$, $1, $3); }
|
---|
125 | | e '+' e
|
---|
126 | { EVALBIN(+, $$, $1, $3); }
|
---|
127 | | e '-' e
|
---|
128 | { EVALBIN(-, $$, $1, $3); }
|
---|
129 | | e LS e
|
---|
130 | { EVALBIN(<<, $$, $1, $3); }
|
---|
131 | | e RS e
|
---|
132 | { EVALUBIN(>>, $$, $1, $3, 0); }
|
---|
133 | | e '<' e
|
---|
134 | { EVALUBIN(<, $$, $1, $3, 1); }
|
---|
135 | | e '>' e
|
---|
136 | { EVALUBIN(>, $$, $1, $3, 1); }
|
---|
137 | | e LE e
|
---|
138 | { EVALUBIN(<=, $$, $1, $3, 1); }
|
---|
139 | | e GE e
|
---|
140 | { EVALUBIN(>=, $$, $1, $3, 1); }
|
---|
141 | | e EQ e
|
---|
142 | { EVALUBIN(==, $$, $1, $3, 1); }
|
---|
143 | | e NE e
|
---|
144 | { EVALUBIN(!=, $$, $1, $3, 1); }
|
---|
145 | | e '&' e
|
---|
146 | { EVALBIN(&, $$, $1, $3); }
|
---|
147 | | e '^' e
|
---|
148 | { EVALBIN(^, $$, $1, $3); }
|
---|
149 | | e '|' e
|
---|
150 | { EVALBIN(|, $$, $1, $3); }
|
---|
151 | | e ANDAND e {
|
---|
152 | $$ = $1;
|
---|
153 | if ($1.nd_val) {
|
---|
154 | $$.op = setd($1.op, $3.op);
|
---|
155 | $$.nd_val = ($3.nd_val != 0);
|
---|
156 | }
|
---|
157 | if ($$.op == UNUMBER) $$.op = NUMBER;
|
---|
158 | }
|
---|
159 | | e OROR e {
|
---|
160 | if ($1.nd_val != 0) {
|
---|
161 | $$.nd_val = ($1.nd_val != 0);
|
---|
162 | $$.op = $1.op;
|
---|
163 | } else {
|
---|
164 | $$.nd_val = ($3.nd_val != 0);
|
---|
165 | $$.op = setd($1.op, $3.op);
|
---|
166 | }
|
---|
167 | if ($$.op == UNUMBER) $$.op = NUMBER;
|
---|
168 | }
|
---|
169 | | e '?' e ':' e {
|
---|
170 | if ($1.op == 0)
|
---|
171 | $$ = $1;
|
---|
172 | else if ($1.nd_val)
|
---|
173 | $$ = $3;
|
---|
174 | else
|
---|
175 | $$ = $5;
|
---|
176 | }
|
---|
177 | | e ',' e {
|
---|
178 | $$.op = setd($1.op, $3.op);
|
---|
179 | $$.nd_val = $3.nd_val;
|
---|
180 | if ($$.op) $$.op = $3.op;
|
---|
181 | }
|
---|
182 | | term
|
---|
183 | {$$ = $1;}
|
---|
184 | term:
|
---|
185 | '-' term %prec UMINUS
|
---|
186 | { EVALUNARY(-, $$, $2); }
|
---|
187 | | '+' term %prec UMINUS
|
---|
188 | {$$ = $2;}
|
---|
189 | | '!' term
|
---|
190 | { $$.nd_val = ! $2.nd_val; $$.op = $2.op ? NUMBER : 0; }
|
---|
191 | | '~' term
|
---|
192 | { EVALUNARY(~, $$, $2); }
|
---|
193 | | '(' e ')'
|
---|
194 | {$$ = $2;}
|
---|
195 | | DEFINED '(' NUMBER ')'
|
---|
196 | {$$= $3;}
|
---|
197 | | DEFINED NUMBER
|
---|
198 | {$$ = $2;}
|
---|
199 | | NUMBER
|
---|
200 | {$$ = $1;}
|
---|
201 | %%
|
---|
202 |
|
---|
203 | void
|
---|
204 | yyerror(const char *err)
|
---|
205 | {
|
---|
206 | error(err);
|
---|
207 | }
|
---|
208 |
|
---|
209 | /*
|
---|
210 | * Set return type of an expression.
|
---|
211 | */
|
---|
212 | int
|
---|
213 | setd(int l, int r)
|
---|
214 | {
|
---|
215 | if (!l || !r)
|
---|
216 | return 0; /* div by zero involved */
|
---|
217 | if (l == UNUMBER || r == UNUMBER)
|
---|
218 | return UNUMBER;
|
---|
219 | return NUMBER;
|
---|
220 | }
|
---|
221 |
|
---|