Print this page
7085 add support for "if" and "else" statements in dtrace
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libdtrace/common/dt_grammar.y
+++ new/usr/src/lib/libdtrace/common/dt_grammar.y
1 1 %{
2 2 /*
3 3 * CDDL HEADER START
4 4 *
5 5 * The contents of this file are subject to the terms of the
6 6 * Common Development and Distribution License, Version 1.0 only
7 7 * (the "License"). You may not use this file except in compliance
8 8 * with the License.
9 9 *
10 10 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11 11 * or http://www.opensolaris.org/os/licensing.
12 12 * See the License for the specific language governing permissions
13 13 * and limitations under the License.
14 14 *
15 15 * When distributing Covered Code, include this CDDL HEADER in each
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
16 16 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17 17 * If applicable, add the following below this CDDL HEADER, with the
18 18 * fields enclosed by brackets "[]" replaced with your own identifying
19 19 * information: Portions Copyright [yyyy] [name of copyright owner]
20 20 *
21 21 * CDDL HEADER END
22 22 *
23 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 +
26 27 /*
27 - * Copyright (c) 2013 by Delphix. All rights reserved.
28 + * Copyright (c) 2014 by Delphix. All rights reserved.
28 29 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
29 30 */
30 31
31 32 #include <dt_impl.h>
32 33
33 34 #define OP1(op, c) dt_node_op1(op, c)
34 35 #define OP2(op, l, r) dt_node_op2(op, l, r)
35 36 #define OP3(x, y, z) dt_node_op3(x, y, z)
36 37 #define LINK(l, r) dt_node_link(l, r)
37 38 #define DUP(s) strdup(s)
38 39
39 40 %}
40 41
41 42 %union {
42 43 dt_node_t *l_node;
43 44 dt_decl_t *l_decl;
44 45 char *l_str;
45 46 uintmax_t l_int;
46 47 int l_tok;
47 48 }
48 49
49 50 %token DT_TOK_COMMA DT_TOK_ELLIPSIS
50 51 %token DT_TOK_ASGN DT_TOK_ADD_EQ DT_TOK_SUB_EQ DT_TOK_MUL_EQ
51 52 %token DT_TOK_DIV_EQ DT_TOK_MOD_EQ DT_TOK_AND_EQ DT_TOK_XOR_EQ DT_TOK_OR_EQ
52 53 %token DT_TOK_LSH_EQ DT_TOK_RSH_EQ DT_TOK_QUESTION DT_TOK_COLON
53 54 %token DT_TOK_LOR DT_TOK_LXOR DT_TOK_LAND
54 55 %token DT_TOK_BOR DT_TOK_XOR DT_TOK_BAND DT_TOK_EQU DT_TOK_NEQ
55 56 %token DT_TOK_LT DT_TOK_LE DT_TOK_GT DT_TOK_GE DT_TOK_LSH DT_TOK_RSH
56 57 %token DT_TOK_ADD DT_TOK_SUB DT_TOK_MUL DT_TOK_DIV DT_TOK_MOD
57 58 %token DT_TOK_LNEG DT_TOK_BNEG DT_TOK_ADDADD DT_TOK_SUBSUB
58 59 %token DT_TOK_PREINC DT_TOK_POSTINC DT_TOK_PREDEC DT_TOK_POSTDEC
59 60 %token DT_TOK_IPOS DT_TOK_INEG DT_TOK_DEREF DT_TOK_ADDROF
60 61 %token DT_TOK_OFFSETOF DT_TOK_SIZEOF DT_TOK_STRINGOF DT_TOK_XLATE
61 62 %token DT_TOK_LPAR DT_TOK_RPAR DT_TOK_LBRAC DT_TOK_RBRAC DT_TOK_PTR DT_TOK_DOT
62 63
63 64 %token <l_str> DT_TOK_STRING
64 65 %token <l_str> DT_TOK_IDENT
65 66 %token <l_str> DT_TOK_PSPEC
66 67 %token <l_str> DT_TOK_AGG
67 68 %token <l_str> DT_TOK_TNAME
68 69 %token <l_int> DT_TOK_INT
69 70
70 71 %token DT_KEY_AUTO
71 72 %token DT_KEY_BREAK
72 73 %token DT_KEY_CASE
73 74 %token DT_KEY_CHAR
74 75 %token DT_KEY_CONST
75 76 %token DT_KEY_CONTINUE
76 77 %token DT_KEY_COUNTER
77 78 %token DT_KEY_DEFAULT
78 79 %token DT_KEY_DO
79 80 %token DT_KEY_DOUBLE
80 81 %token DT_KEY_ELSE
81 82 %token DT_KEY_ENUM
82 83 %token DT_KEY_EXTERN
83 84 %token DT_KEY_FLOAT
84 85 %token DT_KEY_FOR
85 86 %token DT_KEY_GOTO
86 87 %token DT_KEY_IF
87 88 %token DT_KEY_IMPORT
88 89 %token DT_KEY_INLINE
89 90 %token DT_KEY_INT
90 91 %token DT_KEY_LONG
91 92 %token DT_KEY_PROBE
92 93 %token DT_KEY_PROVIDER
93 94 %token DT_KEY_REGISTER
94 95 %token DT_KEY_RESTRICT
95 96 %token DT_KEY_RETURN
96 97 %token DT_KEY_SELF
97 98 %token DT_KEY_SHORT
98 99 %token DT_KEY_SIGNED
99 100 %token DT_KEY_STATIC
100 101 %token DT_KEY_STRING
101 102 %token DT_KEY_STRUCT
102 103 %token DT_KEY_SWITCH
103 104 %token DT_KEY_THIS
104 105 %token DT_KEY_TYPEDEF
105 106 %token DT_KEY_UNION
106 107 %token DT_KEY_UNSIGNED
107 108 %token DT_KEY_USERLAND
108 109 %token DT_KEY_VOID
109 110 %token DT_KEY_VOLATILE
110 111 %token DT_KEY_WHILE
111 112 %token DT_KEY_XLATOR
112 113
113 114 %token DT_TOK_EPRED
114 115 %token DT_CTX_DEXPR
115 116 %token DT_CTX_DPROG
116 117 %token DT_CTX_DTYPE
117 118 %token DT_TOK_EOF 0
118 119
119 120 %left DT_TOK_COMMA
120 121 %right DT_TOK_ASGN DT_TOK_ADD_EQ DT_TOK_SUB_EQ DT_TOK_MUL_EQ DT_TOK_DIV_EQ
121 122 DT_TOK_MOD_EQ DT_TOK_AND_EQ DT_TOK_XOR_EQ DT_TOK_OR_EQ DT_TOK_LSH_EQ
122 123 DT_TOK_RSH_EQ
123 124 %left DT_TOK_QUESTION DT_TOK_COLON
124 125 %left DT_TOK_LOR
125 126 %left DT_TOK_LXOR
126 127 %left DT_TOK_LAND
127 128 %left DT_TOK_BOR
128 129 %left DT_TOK_XOR
129 130 %left DT_TOK_BAND
130 131 %left DT_TOK_EQU DT_TOK_NEQ
131 132 %left DT_TOK_LT DT_TOK_LE DT_TOK_GT DT_TOK_GE
132 133 %left DT_TOK_LSH DT_TOK_RSH
133 134 %left DT_TOK_ADD DT_TOK_SUB
134 135 %left DT_TOK_MUL DT_TOK_DIV DT_TOK_MOD
135 136 %right DT_TOK_LNEG DT_TOK_BNEG DT_TOK_ADDADD DT_TOK_SUBSUB
136 137 DT_TOK_IPOS DT_TOK_INEG
137 138 %right DT_TOK_DEREF DT_TOK_ADDROF DT_TOK_SIZEOF DT_TOK_STRINGOF DT_TOK_XLATE
138 139 %left DT_TOK_LPAR DT_TOK_RPAR DT_TOK_LBRAC DT_TOK_RBRAC DT_TOK_PTR DT_TOK_DOT
139 140
140 141 %type <l_node> d_expression
141 142 %type <l_node> d_program
142 143 %type <l_node> d_type
143 144
144 145 %type <l_node> translation_unit
145 146 %type <l_node> external_declaration
146 147 %type <l_node> inline_definition
147 148 %type <l_node> translator_definition
↓ open down ↓ |
110 lines elided |
↑ open up ↑ |
148 149 %type <l_node> translator_member_list
149 150 %type <l_node> translator_member
150 151 %type <l_node> provider_definition
151 152 %type <l_node> provider_probe_list
152 153 %type <l_node> provider_probe
153 154 %type <l_node> probe_definition
154 155 %type <l_node> probe_specifiers
155 156 %type <l_node> probe_specifier_list
156 157 %type <l_node> probe_specifier
157 158 %type <l_node> statement_list
159 +%type <l_node> statement_list_impl
160 +%type <l_node> statement_or_block
158 161 %type <l_node> statement
159 162 %type <l_node> declaration
160 163 %type <l_node> init_declarator_list
161 164 %type <l_node> init_declarator
162 165
163 166 %type <l_decl> type_specifier
164 167 %type <l_decl> type_qualifier
165 168 %type <l_decl> struct_or_union_specifier
166 169 %type <l_decl> specifier_qualifier_list
167 170 %type <l_decl> enum_specifier
168 171 %type <l_decl> declarator
169 172 %type <l_decl> direct_declarator
170 173 %type <l_decl> pointer
171 174 %type <l_decl> type_qualifier_list
172 175 %type <l_decl> type_name
173 176 %type <l_decl> abstract_declarator
174 177 %type <l_decl> direct_abstract_declarator
175 178
176 179 %type <l_node> parameter_type_list
177 180 %type <l_node> parameter_list
178 181 %type <l_node> parameter_declaration
179 182
180 183 %type <l_node> array
181 184 %type <l_node> array_parameters
182 185 %type <l_node> function
183 186 %type <l_node> function_parameters
184 187
185 188 %type <l_node> expression
186 189 %type <l_node> assignment_expression
187 190 %type <l_node> conditional_expression
188 191 %type <l_node> constant_expression
189 192 %type <l_node> logical_or_expression
190 193 %type <l_node> logical_xor_expression
191 194 %type <l_node> logical_and_expression
192 195 %type <l_node> inclusive_or_expression
193 196 %type <l_node> exclusive_or_expression
194 197 %type <l_node> and_expression
195 198 %type <l_node> equality_expression
196 199 %type <l_node> relational_expression
197 200 %type <l_node> shift_expression
198 201 %type <l_node> additive_expression
199 202 %type <l_node> multiplicative_expression
200 203 %type <l_node> cast_expression
201 204 %type <l_node> unary_expression
202 205 %type <l_node> postfix_expression
203 206 %type <l_node> primary_expression
204 207 %type <l_node> argument_expression_list
205 208
206 209 %type <l_tok> assignment_operator
207 210 %type <l_tok> unary_operator
208 211 %type <l_tok> struct_or_union
209 212
210 213 %%
211 214
212 215 dtrace_program: d_expression DT_TOK_EOF { return (dt_node_root($1)); }
213 216 | d_program DT_TOK_EOF { return (dt_node_root($1)); }
214 217 | d_type DT_TOK_EOF { return (dt_node_root($1)); }
215 218 ;
216 219
217 220 d_expression: DT_CTX_DEXPR { $$ = NULL; }
218 221 | DT_CTX_DEXPR expression { $$ = $2; }
219 222 ;
220 223
221 224 d_program: DT_CTX_DPROG { $$ = dt_node_program(NULL); }
222 225 | DT_CTX_DPROG translation_unit { $$ = dt_node_program($2); }
223 226 ;
224 227
225 228 d_type: DT_CTX_DTYPE { $$ = NULL; }
226 229 | DT_CTX_DTYPE type_name { $$ = (dt_node_t *)$2; }
227 230 ;
228 231
229 232 translation_unit:
230 233 external_declaration
231 234 | translation_unit external_declaration { $$ = LINK($1, $2); }
232 235 ;
233 236
234 237 external_declaration:
235 238 inline_definition
236 239 | translator_definition
237 240 | provider_definition
238 241 | probe_definition
239 242 | declaration
240 243 ;
241 244
242 245 inline_definition:
243 246 DT_KEY_INLINE declaration_specifiers declarator
244 247 { dt_scope_push(NULL, CTF_ERR); } DT_TOK_ASGN
245 248 assignment_expression ';' {
246 249 /*
247 250 * We push a new declaration scope before shifting the
248 251 * assignment_expression in order to preserve ds_class
249 252 * and ds_ident for use in dt_node_inline(). Once the
250 253 * entire inline_definition rule is matched, pop the
251 254 * scope and construct the inline using the saved decl.
252 255 */
253 256 dt_scope_pop();
254 257 $$ = dt_node_inline($6);
255 258 }
256 259 ;
257 260
258 261 translator_definition:
259 262 DT_KEY_XLATOR type_name DT_TOK_LT type_name
260 263 DT_TOK_IDENT DT_TOK_GT '{' translator_member_list '}' ';' {
261 264 $$ = dt_node_xlator($2, $4, $5, $8);
262 265 }
263 266 | DT_KEY_XLATOR type_name DT_TOK_LT type_name
264 267 DT_TOK_IDENT DT_TOK_GT '{' '}' ';' {
265 268 $$ = dt_node_xlator($2, $4, $5, NULL);
266 269 }
267 270 ;
268 271
269 272 translator_member_list:
270 273 translator_member
271 274 | translator_member_list translator_member { $$ = LINK($1,$2); }
272 275 ;
273 276
274 277 translator_member:
275 278 DT_TOK_IDENT DT_TOK_ASGN assignment_expression ';' {
276 279 $$ = dt_node_member(NULL, $1, $3);
277 280 }
278 281 ;
279 282
280 283 provider_definition:
281 284 DT_KEY_PROVIDER DT_TOK_IDENT '{' provider_probe_list '}' ';' {
282 285 $$ = dt_node_provider($2, $4);
283 286 }
284 287 | DT_KEY_PROVIDER DT_TOK_IDENT '{' '}' ';' {
285 288 $$ = dt_node_provider($2, NULL);
286 289 }
287 290 ;
288 291
289 292 provider_probe_list:
290 293 provider_probe
291 294 | provider_probe_list provider_probe { $$ = LINK($1, $2); }
292 295 ;
293 296
294 297 provider_probe:
295 298 DT_KEY_PROBE DT_TOK_IDENT function DT_TOK_COLON function ';' {
296 299 $$ = dt_node_probe($2, 2, $3, $5);
297 300 }
298 301 | DT_KEY_PROBE DT_TOK_IDENT function ';' {
299 302 $$ = dt_node_probe($2, 1, $3, NULL);
300 303 }
301 304 ;
302 305
303 306
304 307 probe_definition:
305 308 probe_specifiers {
306 309 /*
307 310 * If the input stream is a file, do not permit a probe
308 311 * specification without / <pred> / or { <act> } after
309 312 * it. This can only occur if the next token is EOF or
↓ open down ↓ |
142 lines elided |
↑ open up ↑ |
310 313 * an ambiguous predicate was slurped up as a comment.
311 314 * We cannot perform this check if input() is a string
312 315 * because dtrace(1M) [-fmnP] also use the compiler and
313 316 * things like dtrace -n BEGIN have to be accepted.
314 317 */
315 318 if (yypcb->pcb_fileptr != NULL) {
316 319 dnerror($1, D_SYNTAX, "expected predicate and/"
317 320 "or actions following probe description\n");
318 321 }
319 322 $$ = dt_node_clause($1, NULL, NULL);
323 + yybegin(YYS_CLAUSE);
320 324 }
321 325 | probe_specifiers '{' statement_list '}' {
322 326 $$ = dt_node_clause($1, NULL, $3);
327 + yybegin(YYS_CLAUSE);
323 328 }
324 329 | probe_specifiers DT_TOK_DIV expression DT_TOK_EPRED {
325 330 dnerror($3, D_SYNTAX, "expected actions { } following "
326 331 "probe description and predicate\n");
327 332 }
328 333 | probe_specifiers DT_TOK_DIV expression DT_TOK_EPRED
329 334 '{' statement_list '}' {
330 335 $$ = dt_node_clause($1, $3, $6);
336 + yybegin(YYS_CLAUSE);
331 337 }
332 338 ;
333 339
334 340 probe_specifiers:
335 341 probe_specifier_list { yybegin(YYS_EXPR); $$ = $1; }
336 342 ;
337 343
338 344 probe_specifier_list:
339 345 probe_specifier
340 346 | probe_specifier_list DT_TOK_COMMA probe_specifier {
341 347 $$ = LINK($1, $3);
342 348 }
343 349 ;
344 350
345 351 probe_specifier:
346 352 DT_TOK_PSPEC { $$ = dt_node_pdesc_by_name($1); }
347 353 | DT_TOK_INT { $$ = dt_node_pdesc_by_id($1); }
348 354 ;
349 355
350 -statement_list: statement { $$ = $1; }
351 - | statement_list ';' statement { $$ = LINK($1, $3); }
356 +statement_list_impl: /* empty */ { $$ = NULL; }
357 + | statement_list_impl statement { $$ = LINK($1, $2); }
352 358 ;
353 359
354 -statement: /* empty */ { $$ = NULL; }
355 - | expression { $$ = dt_node_statement($1); }
360 +statement_list:
361 + statement_list_impl { $$ = $1; }
362 + | statement_list_impl expression {
363 + $$ = LINK($1, dt_node_statement($2));
364 + }
356 365 ;
357 366
367 +statement_or_block:
368 + statement
369 + | '{' statement_list '}' { $$ = $2; }
370 +
371 +statement: ';' { $$ = NULL; }
372 + | expression ';' { $$ = dt_node_statement($1); }
373 + | DT_KEY_IF DT_TOK_LPAR expression DT_TOK_RPAR statement_or_block {
374 + $$ = dt_node_if($3, $5, NULL);
375 + }
376 + | DT_KEY_IF DT_TOK_LPAR expression DT_TOK_RPAR
377 + statement_or_block DT_KEY_ELSE statement_or_block {
378 + $$ = dt_node_if($3, $5, $7);
379 + }
380 + ;
381 +
358 382 argument_expression_list:
359 383 assignment_expression
360 384 | argument_expression_list DT_TOK_COMMA assignment_expression {
361 385 $$ = LINK($1, $3);
362 386 }
363 387 ;
364 388
365 389 primary_expression:
366 390 DT_TOK_IDENT { $$ = dt_node_ident($1); }
367 391 | DT_TOK_AGG { $$ = dt_node_ident($1); }
368 392 | DT_TOK_INT { $$ = dt_node_int($1); }
369 393 | DT_TOK_STRING { $$ = dt_node_string($1); }
370 394 | DT_KEY_SELF { $$ = dt_node_ident(DUP("self")); }
371 395 | DT_KEY_THIS { $$ = dt_node_ident(DUP("this")); }
372 396 | DT_TOK_LPAR expression DT_TOK_RPAR { $$ = $2; }
373 397 ;
374 398
375 399 postfix_expression:
376 400 primary_expression
377 401 | postfix_expression
378 402 DT_TOK_LBRAC argument_expression_list DT_TOK_RBRAC {
379 403 $$ = OP2(DT_TOK_LBRAC, $1, $3);
380 404 }
381 405 | postfix_expression DT_TOK_LPAR DT_TOK_RPAR {
382 406 $$ = dt_node_func($1, NULL);
383 407 }
384 408 | postfix_expression
385 409 DT_TOK_LPAR argument_expression_list DT_TOK_RPAR {
386 410 $$ = dt_node_func($1, $3);
387 411 }
388 412 | postfix_expression DT_TOK_DOT DT_TOK_IDENT {
389 413 $$ = OP2(DT_TOK_DOT, $1, dt_node_ident($3));
390 414 }
391 415 | postfix_expression DT_TOK_DOT DT_TOK_TNAME {
392 416 $$ = OP2(DT_TOK_DOT, $1, dt_node_ident($3));
393 417 }
394 418 | postfix_expression DT_TOK_PTR DT_TOK_IDENT {
395 419 $$ = OP2(DT_TOK_PTR, $1, dt_node_ident($3));
396 420 }
397 421 | postfix_expression DT_TOK_PTR DT_TOK_TNAME {
398 422 $$ = OP2(DT_TOK_PTR, $1, dt_node_ident($3));
399 423 }
400 424 | postfix_expression DT_TOK_ADDADD {
401 425 $$ = OP1(DT_TOK_POSTINC, $1);
402 426 }
403 427 | postfix_expression DT_TOK_SUBSUB {
404 428 $$ = OP1(DT_TOK_POSTDEC, $1);
405 429 }
406 430 | DT_TOK_OFFSETOF DT_TOK_LPAR type_name DT_TOK_COMMA
407 431 DT_TOK_IDENT DT_TOK_RPAR {
408 432 $$ = dt_node_offsetof($3, $5);
409 433 }
410 434 | DT_TOK_OFFSETOF DT_TOK_LPAR type_name DT_TOK_COMMA
411 435 DT_TOK_TNAME DT_TOK_RPAR {
412 436 $$ = dt_node_offsetof($3, $5);
413 437 }
414 438 | DT_TOK_XLATE DT_TOK_LT type_name DT_TOK_GT
415 439 DT_TOK_LPAR expression DT_TOK_RPAR {
416 440 $$ = OP2(DT_TOK_XLATE, dt_node_type($3), $6);
417 441 }
418 442 ;
419 443
420 444 unary_expression:
421 445 postfix_expression
422 446 | DT_TOK_ADDADD unary_expression { $$ = OP1(DT_TOK_PREINC, $2); }
423 447 | DT_TOK_SUBSUB unary_expression { $$ = OP1(DT_TOK_PREDEC, $2); }
424 448 | unary_operator cast_expression { $$ = OP1($1, $2); }
425 449 | DT_TOK_SIZEOF unary_expression { $$ = OP1(DT_TOK_SIZEOF, $2); }
426 450 | DT_TOK_SIZEOF DT_TOK_LPAR type_name DT_TOK_RPAR {
427 451 $$ = OP1(DT_TOK_SIZEOF, dt_node_type($3));
428 452 }
429 453 | DT_TOK_STRINGOF unary_expression {
430 454 $$ = OP1(DT_TOK_STRINGOF, $2);
431 455 }
432 456 ;
433 457
434 458 unary_operator: DT_TOK_BAND { $$ = DT_TOK_ADDROF; }
435 459 | DT_TOK_MUL { $$ = DT_TOK_DEREF; }
436 460 | DT_TOK_ADD { $$ = DT_TOK_IPOS; }
437 461 | DT_TOK_SUB { $$ = DT_TOK_INEG; }
438 462 | DT_TOK_BNEG { $$ = DT_TOK_BNEG; }
439 463 | DT_TOK_LNEG { $$ = DT_TOK_LNEG; }
440 464 ;
441 465
442 466 cast_expression:
443 467 unary_expression
444 468 | DT_TOK_LPAR type_name DT_TOK_RPAR cast_expression {
445 469 $$ = OP2(DT_TOK_LPAR, dt_node_type($2), $4);
446 470 }
447 471 ;
448 472
449 473 multiplicative_expression:
450 474 cast_expression
451 475 | multiplicative_expression DT_TOK_MUL cast_expression {
452 476 $$ = OP2(DT_TOK_MUL, $1, $3);
453 477 }
454 478 | multiplicative_expression DT_TOK_DIV cast_expression {
455 479 $$ = OP2(DT_TOK_DIV, $1, $3);
456 480 }
457 481 | multiplicative_expression DT_TOK_MOD cast_expression {
458 482 $$ = OP2(DT_TOK_MOD, $1, $3);
459 483 }
460 484 ;
461 485
462 486 additive_expression:
463 487 multiplicative_expression
464 488 | additive_expression DT_TOK_ADD multiplicative_expression {
465 489 $$ = OP2(DT_TOK_ADD, $1, $3);
466 490 }
467 491 | additive_expression DT_TOK_SUB multiplicative_expression {
468 492 $$ = OP2(DT_TOK_SUB, $1, $3);
469 493 }
470 494 ;
471 495
472 496 shift_expression:
473 497 additive_expression
474 498 | shift_expression DT_TOK_LSH additive_expression {
475 499 $$ = OP2(DT_TOK_LSH, $1, $3);
476 500 }
477 501 | shift_expression DT_TOK_RSH additive_expression {
478 502 $$ = OP2(DT_TOK_RSH, $1, $3);
479 503 }
480 504 ;
481 505
482 506 relational_expression:
483 507 shift_expression
484 508 | relational_expression DT_TOK_LT shift_expression {
485 509 $$ = OP2(DT_TOK_LT, $1, $3);
486 510 }
487 511 | relational_expression DT_TOK_GT shift_expression {
488 512 $$ = OP2(DT_TOK_GT, $1, $3);
489 513 }
490 514 | relational_expression DT_TOK_LE shift_expression {
491 515 $$ = OP2(DT_TOK_LE, $1, $3);
492 516 }
493 517 | relational_expression DT_TOK_GE shift_expression {
494 518 $$ = OP2(DT_TOK_GE, $1, $3);
495 519 }
496 520 ;
497 521
498 522 equality_expression:
499 523 relational_expression
500 524 | equality_expression DT_TOK_EQU relational_expression {
501 525 $$ = OP2(DT_TOK_EQU, $1, $3);
502 526 }
503 527 | equality_expression DT_TOK_NEQ relational_expression {
504 528 $$ = OP2(DT_TOK_NEQ, $1, $3);
505 529 }
506 530 ;
507 531
508 532 and_expression:
509 533 equality_expression
510 534 | and_expression DT_TOK_BAND equality_expression {
511 535 $$ = OP2(DT_TOK_BAND, $1, $3);
512 536 }
513 537 ;
514 538
515 539 exclusive_or_expression:
516 540 and_expression
517 541 | exclusive_or_expression DT_TOK_XOR and_expression {
518 542 $$ = OP2(DT_TOK_XOR, $1, $3);
519 543 }
520 544 ;
521 545
522 546 inclusive_or_expression:
523 547 exclusive_or_expression
524 548 | inclusive_or_expression DT_TOK_BOR exclusive_or_expression {
525 549 $$ = OP2(DT_TOK_BOR, $1, $3);
526 550 }
527 551 ;
528 552
529 553 logical_and_expression:
530 554 inclusive_or_expression
531 555 | logical_and_expression DT_TOK_LAND inclusive_or_expression {
532 556 $$ = OP2(DT_TOK_LAND, $1, $3);
533 557 }
534 558 ;
535 559
536 560 logical_xor_expression:
537 561 logical_and_expression
538 562 | logical_xor_expression DT_TOK_LXOR logical_and_expression {
539 563 $$ = OP2(DT_TOK_LXOR, $1, $3);
540 564 }
541 565 ;
542 566
543 567 logical_or_expression:
544 568 logical_xor_expression
545 569 | logical_or_expression DT_TOK_LOR logical_xor_expression {
546 570 $$ = OP2(DT_TOK_LOR, $1, $3);
547 571 }
548 572 ;
549 573
550 574 constant_expression: conditional_expression
551 575 ;
552 576
553 577 conditional_expression:
554 578 logical_or_expression
555 579 | logical_or_expression DT_TOK_QUESTION expression DT_TOK_COLON
556 580 conditional_expression { $$ = OP3($1, $3, $5); }
557 581 ;
558 582
559 583 assignment_expression:
560 584 conditional_expression
561 585 | unary_expression assignment_operator assignment_expression {
562 586 $$ = OP2($2, $1, $3);
563 587 }
564 588 ;
565 589
566 590 assignment_operator:
567 591 DT_TOK_ASGN { $$ = DT_TOK_ASGN; }
568 592 | DT_TOK_MUL_EQ { $$ = DT_TOK_MUL_EQ; }
569 593 | DT_TOK_DIV_EQ { $$ = DT_TOK_DIV_EQ; }
570 594 | DT_TOK_MOD_EQ { $$ = DT_TOK_MOD_EQ; }
571 595 | DT_TOK_ADD_EQ { $$ = DT_TOK_ADD_EQ; }
572 596 | DT_TOK_SUB_EQ { $$ = DT_TOK_SUB_EQ; }
573 597 | DT_TOK_LSH_EQ { $$ = DT_TOK_LSH_EQ; }
574 598 | DT_TOK_RSH_EQ { $$ = DT_TOK_RSH_EQ; }
575 599 | DT_TOK_AND_EQ { $$ = DT_TOK_AND_EQ; }
576 600 | DT_TOK_XOR_EQ { $$ = DT_TOK_XOR_EQ; }
577 601 | DT_TOK_OR_EQ { $$ = DT_TOK_OR_EQ; }
578 602 ;
579 603
580 604 expression: assignment_expression
581 605 | expression DT_TOK_COMMA assignment_expression {
582 606 $$ = OP2(DT_TOK_COMMA, $1, $3);
583 607 }
584 608 ;
585 609
586 610 declaration: declaration_specifiers ';' {
587 611 $$ = dt_node_decl();
588 612 dt_decl_free(dt_decl_pop());
589 613 yybegin(YYS_CLAUSE);
590 614 }
591 615 | declaration_specifiers init_declarator_list ';' {
592 616 $$ = $2;
593 617 dt_decl_free(dt_decl_pop());
594 618 yybegin(YYS_CLAUSE);
595 619 }
596 620 ;
597 621
598 622 declaration_specifiers:
599 623 d_storage_class_specifier
600 624 | d_storage_class_specifier declaration_specifiers
601 625 | type_specifier
602 626 | type_specifier declaration_specifiers
603 627 | type_qualifier
604 628 | type_qualifier declaration_specifiers
605 629 ;
606 630
607 631 parameter_declaration_specifiers:
608 632 storage_class_specifier
609 633 | storage_class_specifier declaration_specifiers
610 634 | type_specifier
611 635 | type_specifier declaration_specifiers
612 636 | type_qualifier
613 637 | type_qualifier declaration_specifiers
614 638 ;
615 639
616 640 storage_class_specifier:
617 641 DT_KEY_AUTO { dt_decl_class(DT_DC_AUTO); }
618 642 | DT_KEY_REGISTER { dt_decl_class(DT_DC_REGISTER); }
619 643 | DT_KEY_STATIC { dt_decl_class(DT_DC_STATIC); }
620 644 | DT_KEY_EXTERN { dt_decl_class(DT_DC_EXTERN); }
621 645 | DT_KEY_TYPEDEF { dt_decl_class(DT_DC_TYPEDEF); }
622 646 ;
623 647
624 648 d_storage_class_specifier:
625 649 storage_class_specifier
626 650 | DT_KEY_SELF { dt_decl_class(DT_DC_SELF); }
627 651 | DT_KEY_THIS { dt_decl_class(DT_DC_THIS); }
628 652 ;
629 653
630 654 type_specifier: DT_KEY_VOID { $$ = dt_decl_spec(CTF_K_INTEGER, DUP("void")); }
631 655 | DT_KEY_CHAR { $$ = dt_decl_spec(CTF_K_INTEGER, DUP("char")); }
632 656 | DT_KEY_SHORT { $$ = dt_decl_attr(DT_DA_SHORT); }
633 657 | DT_KEY_INT { $$ = dt_decl_spec(CTF_K_INTEGER, DUP("int")); }
634 658 | DT_KEY_LONG { $$ = dt_decl_attr(DT_DA_LONG); }
635 659 | DT_KEY_FLOAT { $$ = dt_decl_spec(CTF_K_FLOAT, DUP("float")); }
636 660 | DT_KEY_DOUBLE { $$ = dt_decl_spec(CTF_K_FLOAT, DUP("double")); }
637 661 | DT_KEY_SIGNED { $$ = dt_decl_attr(DT_DA_SIGNED); }
638 662 | DT_KEY_UNSIGNED { $$ = dt_decl_attr(DT_DA_UNSIGNED); }
639 663 | DT_KEY_USERLAND { $$ = dt_decl_attr(DT_DA_USER); }
640 664 | DT_KEY_STRING {
641 665 $$ = dt_decl_spec(CTF_K_TYPEDEF, DUP("string"));
642 666 }
643 667 | DT_TOK_TNAME { $$ = dt_decl_spec(CTF_K_TYPEDEF, $1); }
644 668 | struct_or_union_specifier
645 669 | enum_specifier
646 670 ;
647 671
648 672 type_qualifier: DT_KEY_CONST { $$ = dt_decl_attr(DT_DA_CONST); }
649 673 | DT_KEY_RESTRICT { $$ = dt_decl_attr(DT_DA_RESTRICT); }
650 674 | DT_KEY_VOLATILE { $$ = dt_decl_attr(DT_DA_VOLATILE); }
651 675 ;
652 676
653 677 struct_or_union_specifier:
654 678 struct_or_union_definition struct_declaration_list '}' {
655 679 $$ = dt_scope_pop();
656 680 }
657 681 | struct_or_union DT_TOK_IDENT { $$ = dt_decl_spec($1, $2); }
658 682 | struct_or_union DT_TOK_TNAME { $$ = dt_decl_spec($1, $2); }
659 683 ;
660 684
661 685 struct_or_union_definition:
662 686 struct_or_union '{' { dt_decl_sou($1, NULL); }
663 687 | struct_or_union DT_TOK_IDENT '{' { dt_decl_sou($1, $2); }
664 688 | struct_or_union DT_TOK_TNAME '{' { dt_decl_sou($1, $2); }
665 689 ;
666 690
667 691 struct_or_union:
668 692 DT_KEY_STRUCT { $$ = CTF_K_STRUCT; }
669 693 | DT_KEY_UNION { $$ = CTF_K_UNION; }
670 694 ;
671 695
672 696 struct_declaration_list:
673 697 struct_declaration
674 698 | struct_declaration_list struct_declaration
675 699 ;
676 700
677 701 init_declarator_list:
678 702 init_declarator
679 703 | init_declarator_list DT_TOK_COMMA init_declarator {
680 704 $$ = LINK($1, $3);
681 705 }
682 706 ;
683 707
684 708 init_declarator:
685 709 declarator {
686 710 $$ = dt_node_decl();
687 711 dt_decl_reset();
688 712 }
689 713 ;
690 714
691 715 struct_declaration:
692 716 specifier_qualifier_list struct_declarator_list ';' {
693 717 dt_decl_free(dt_decl_pop());
694 718 }
695 719 ;
696 720
697 721 specifier_qualifier_list:
698 722 type_specifier
699 723 | type_specifier specifier_qualifier_list { $$ = $2; }
700 724 | type_qualifier
701 725 | type_qualifier specifier_qualifier_list { $$ = $2; }
702 726 ;
703 727
704 728 struct_declarator_list:
705 729 struct_declarator
706 730 | struct_declarator_list DT_TOK_COMMA struct_declarator
707 731 ;
708 732
709 733 struct_declarator:
710 734 declarator { dt_decl_member(NULL); }
711 735 | DT_TOK_COLON constant_expression { dt_decl_member($2); }
712 736 | declarator DT_TOK_COLON constant_expression {
713 737 dt_decl_member($3);
714 738 }
715 739 ;
716 740
717 741 enum_specifier:
718 742 enum_definition enumerator_list '}' { $$ = dt_scope_pop(); }
719 743 | DT_KEY_ENUM DT_TOK_IDENT { $$ = dt_decl_spec(CTF_K_ENUM, $2); }
720 744 | DT_KEY_ENUM DT_TOK_TNAME { $$ = dt_decl_spec(CTF_K_ENUM, $2); }
721 745 ;
722 746
723 747 enum_definition:
724 748 DT_KEY_ENUM '{' { dt_decl_enum(NULL); }
725 749 | DT_KEY_ENUM DT_TOK_IDENT '{' { dt_decl_enum($2); }
726 750 | DT_KEY_ENUM DT_TOK_TNAME '{' { dt_decl_enum($2); }
727 751 ;
728 752
729 753 enumerator_list:
730 754 enumerator
731 755 | enumerator_list DT_TOK_COMMA enumerator
732 756 ;
733 757
734 758 enumerator: DT_TOK_IDENT { dt_decl_enumerator($1, NULL); }
735 759 | DT_TOK_IDENT DT_TOK_ASGN expression {
736 760 dt_decl_enumerator($1, $3);
737 761 }
738 762 ;
739 763
740 764 declarator: direct_declarator
741 765 | pointer direct_declarator
742 766 ;
743 767
744 768 direct_declarator:
745 769 DT_TOK_IDENT { $$ = dt_decl_ident($1); }
746 770 | lparen declarator DT_TOK_RPAR { $$ = $2; }
747 771 | direct_declarator array { dt_decl_array($2); }
748 772 | direct_declarator function { dt_decl_func($1, $2); }
749 773 ;
750 774
751 775 lparen: DT_TOK_LPAR { dt_decl_top()->dd_attr |= DT_DA_PAREN; }
752 776 ;
753 777
754 778 pointer: DT_TOK_MUL { $$ = dt_decl_ptr(); }
755 779 | DT_TOK_MUL type_qualifier_list { $$ = dt_decl_ptr(); }
756 780 | DT_TOK_MUL pointer { $$ = dt_decl_ptr(); }
757 781 | DT_TOK_MUL type_qualifier_list pointer { $$ = dt_decl_ptr(); }
758 782 ;
759 783
760 784 type_qualifier_list:
761 785 type_qualifier
762 786 | type_qualifier_list type_qualifier { $$ = $2; }
763 787 ;
764 788
765 789 parameter_type_list:
766 790 parameter_list
767 791 | DT_TOK_ELLIPSIS { $$ = dt_node_vatype(); }
768 792 | parameter_list DT_TOK_COMMA DT_TOK_ELLIPSIS {
769 793 $$ = LINK($1, dt_node_vatype());
770 794 }
771 795 ;
772 796
773 797 parameter_list: parameter_declaration
774 798 | parameter_list DT_TOK_COMMA parameter_declaration {
775 799 $$ = LINK($1, $3);
776 800 }
777 801 ;
778 802
779 803 parameter_declaration:
780 804 parameter_declaration_specifiers {
781 805 $$ = dt_node_type(NULL);
782 806 }
783 807 | parameter_declaration_specifiers declarator {
784 808 $$ = dt_node_type(NULL);
785 809 }
786 810 | parameter_declaration_specifiers abstract_declarator {
787 811 $$ = dt_node_type(NULL);
788 812 }
789 813 ;
790 814
791 815 type_name: specifier_qualifier_list {
792 816 $$ = dt_decl_pop();
793 817 }
794 818 | specifier_qualifier_list abstract_declarator {
795 819 $$ = dt_decl_pop();
796 820 }
797 821 ;
798 822
799 823 abstract_declarator:
800 824 pointer
801 825 | direct_abstract_declarator
802 826 | pointer direct_abstract_declarator
803 827 ;
804 828
805 829 direct_abstract_declarator:
806 830 lparen abstract_declarator DT_TOK_RPAR { $$ = $2; }
807 831 | direct_abstract_declarator array { dt_decl_array($2); }
808 832 | array { dt_decl_array($1); $$ = NULL; }
809 833 | direct_abstract_declarator function { dt_decl_func($1, $2); }
810 834 | function { dt_decl_func(NULL, $1); }
811 835 ;
812 836
813 837 array: DT_TOK_LBRAC { dt_scope_push(NULL, CTF_ERR); }
814 838 array_parameters DT_TOK_RBRAC {
815 839 dt_scope_pop();
816 840 $$ = $3;
817 841 }
818 842 ;
819 843
820 844 array_parameters:
821 845 /* empty */ { $$ = NULL; }
822 846 | constant_expression { $$ = $1; }
823 847 | parameter_type_list { $$ = $1; }
824 848 ;
825 849
826 850 function: DT_TOK_LPAR { dt_scope_push(NULL, CTF_ERR); }
827 851 function_parameters DT_TOK_RPAR {
828 852 dt_scope_pop();
829 853 $$ = $3;
830 854 }
831 855 ;
832 856
833 857 function_parameters:
834 858 /* empty */ { $$ = NULL; }
835 859 | parameter_type_list { $$ = $1; }
836 860 ;
837 861
838 862 %%
↓ open down ↓ |
471 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX