1 /* 2 * [test] btyacc 3 * [test] cc -c 4 */ 5 %token AUTO REGISTER STATIC EXTERN TYPEDEF VOID CHAR SHORT INT LONG FLOAT 6 DOUBLE SIGNED UNSIGNED CONST VOLATILE STRUCT UNION ENUM CASE DEFAULT 7 IF SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN ELSE 8 MULEQ DIVEQ MODEQ ADDEQ SUBEQ LSHEQ RSHEQ ANDEQ XOREQ OREQ 9 AND OR EQU NEQ LEQ GEQ LSH RSH INC DEC ARROW IDENTIFIER STRING 10 INTCONST CHARCONST FLOATCONST ELIPSIS SIZEOF 11 12 %% 13 14 translation.unit 15 : external.declaration 16 | translation.unit external.declaration 17 ; 18 19 external.declaration 20 : function.definition 21 | declaration 22 ; 23 24 function.definition 25 : declaration.specifiers declarator declaration.list.opt compound.statement 26 | declarator declaration.list.opt compound.statement 27 ; 28 29 declaration.specifiers.opt 30 : 31 | declaration.specifiers 32 ; 33 34 declaration.list.opt 35 : 36 | declaration.list 37 ; 38 39 declaration 40 : declaration.specifiers init.declarator.list.opt ';' 41 ; 42 43 declaration.list 44 : declaration 45 | declaration.list declaration 46 ; 47 48 declaration.specifiers 49 : storage.class.specifier declaration.specifiers.opt 50 | type.specifier declaration.specifiers.opt 51 | type.qualifier declaration.specifiers.opt 52 ; 53 54 storage.class.specifier 55 : AUTO 56 | REGISTER 57 | STATIC 58 | EXTERN 59 | TYPEDEF 60 ; 61 62 type.specifier 63 : VOID 64 | CHAR 65 | SHORT 66 | INT 67 | LONG 68 | FLOAT 69 | DOUBLE 70 | SIGNED 71 | UNSIGNED 72 | struct.or.union.specifier 73 | enum.specifier 74 | typedef.name 75 ; 76 77 type.qualifier 78 : CONST 79 | VOLATILE 80 ; 81 82 struct.or.union.specifier 83 : struct.or.union identifier.opt '{' struct.declaration.list '}' 84 | struct.or.union IDENTIFIER 85 ; 86 87 struct.or.union 88 : STRUCT 89 | UNION 90 ; 91 92 struct.declaration.list 93 : struct.declaration 94 | struct.declaration.list struct.declaration 95 ; 96 97 init.declarator.list.opt 98 : 99 | init.declarator.list 100 ; 101 102 init.declarator.list 103 : init.declarator 104 | init.declarator.list ',' init.declarator 105 ; 106 107 init.declarator 108 : declarator 109 | declarator '=' initializer 110 ; 111 112 struct.declaration 113 : specifier.qualifier.list struct.declarator.list ';' 114 ; 115 116 specifier.qualifier.list.opt 117 : 118 | specifier.qualifier.list 119 ; 120 121 specifier.qualifier.list 122 : type.specifier specifier.qualifier.list.opt 123 | type.qualifier specifier.qualifier.list.opt 124 ; 125 126 struct.declarator.list 127 : struct.declarator 128 | struct.declarator.list ',' struct.declarator 129 ; 130 131 struct.declarator 132 : declarator 133 | declarator.opt ':' constant.expression 134 ; 135 136 enum.specifier 137 : ENUM identifier.opt '{' enumerator.list '}' 138 | ENUM IDENTIFIER 139 ; 140 141 enumerator.list 142 : enumerator 143 | enumerator.list ',' enumerator 144 ; 145 146 enumerator 147 : IDENTIFIER 148 | IDENTIFIER '=' constant.expression 149 ; 150 151 declarator.opt 152 : 153 | declarator 154 ; 155 156 declarator 157 : pointer.opt direct.declarator 158 ; 159 160 direct.declarator 161 : IDENTIFIER 162 | '(' declarator ')' 163 | direct.declarator '[' constant.expression ']' 164 | direct.declarator '(' parameter.type.list ')' 165 | direct.declarator '(' identifier.list.opt ')' 166 ; 167 168 pointer.opt 169 : 170 | pointer 171 ; 172 173 pointer : '*' type.qualifier.list.opt 174 | '*' type.qualifier.list.opt pointer 175 ; 176 177 type.qualifier.list.opt 178 : 179 | type.qualifier.list 180 ; 181 182 type.qualifier.list 183 : type.qualifier 184 | type.qualifier.list type.qualifier 185 ; 186 187 parameter.type.list.opt 188 : 189 | parameter.type.list 190 ; 191 192 parameter.type.list 193 : parameter.list 194 | parameter.list ',' ELIPSIS 195 ; 196 197 parameter.list 198 : parameter.declaration 199 | parameter.list ',' parameter.declaration 200 ; 201 202 parameter.declaration 203 : declaration.specifiers declarator 204 | declaration.specifiers abstract.declarator.opt 205 ; 206 207 identifier.list.opt 208 : 209 | identifier.list 210 ; 211 212 identifier.list 213 : IDENTIFIER 214 | identifier.list ',' IDENTIFIER 215 ; 216 217 initializer 218 : assignment.expression 219 | '{' initializer.list '}' 220 | '{' initializer.list ',' '}' 221 ; 222 223 224 initializer.list 225 : initializer 226 | initializer.list ',' initializer 227 ; 228 229 type.name 230 : specifier.qualifier.list abstract.declarator.opt 231 ; 232 233 abstract.declarator.opt 234 : 235 | abstract.declarator 236 ; 237 238 abstract.declarator 239 : pointer 240 | pointer.opt direct.abstract.declarator 241 ; 242 243 direct.abstract.declarator.opt 244 : 245 | direct.abstract.declarator 246 ; 247 248 direct.abstract.declarator 249 : '(' abstract.declarator ')' 250 | direct.abstract.declarator.opt '[' constant.expression.opt ']' 251 | direct.abstract.declarator '(' parameter.type.list.opt ')' 252 | '(' parameter.type.list.opt ')' 253 ; 254 255 typedef.name 256 : IDENTIFIER 257 ; 258 259 identifier.opt 260 : 261 | IDENTIFIER 262 ; 263 264 statement 265 : labeled.statement 266 | expression.statement 267 | compound.statement 268 | selection.statement 269 | iteration.statement 270 | jump.statement 271 ; 272 273 labeled.statement 274 : IDENTIFIER ':' statement 275 | CASE constant.expression ':' statement 276 | DEFAULT ':' statement 277 ; 278 279 expression.statement 280 : expression.opt ';' 281 ; 282 283 compound.statement 284 : '{' declaration.list.opt statement.list.opt '}' 285 ; 286 287 statement.list.opt 288 : 289 | statement.list 290 ; 291 292 statement.list 293 : statement 294 | statement.list statement 295 ; 296 297 selection.statement 298 : IF '(' expression ')' statement 299 | IF '(' expression ')' statement ELSE statement 300 | SWITCH '(' expression ')' statement 301 ; 302 303 iteration.statement 304 : WHILE '(' expression ')' statement 305 | DO statement WHILE '(' expression ')' ';' 306 | FOR '(' expression.opt ';' expression.opt ';' expression.opt ')' statement 307 ; 308 309 jump.statement 310 : GOTO IDENTIFIER ';' 311 | CONTINUE ';' 312 | BREAK ';' 313 | RETURN expression.opt ';' 314 ; 315 316 expression.opt 317 : 318 | expression 319 ; 320 321 constant.expression.opt 322 : 323 | constant.expression 324 ; 325 326 constant.expression 327 : assignment.expression 328 ; 329 330 expression 331 : assignment.expression 332 | expression ',' assignment.expression 333 ; 334 335 assignment.expression 336 : conditional.expression 337 | unary.expression assignment.operator assignment.expression 338 ; 339 340 assignment.operator 341 : '=' | MULEQ | DIVEQ | MODEQ | ADDEQ | SUBEQ 342 | LSHEQ | RSHEQ | ANDEQ | XOREQ | OREQ 343 ; 344 345 conditional.expression 346 : logical.OR.expression 347 | logical.OR.expression '?' expression ':' conditional.expression 348 ; 349 350 logical.OR.expression 351 : logical.AND.expression 352 | logical.OR.expression OR logical.AND.expression 353 ; 354 355 logical.AND.expression 356 : inclusive.OR.expression 357 | logical.AND.expression AND inclusive.OR.expression 358 ; 359 360 inclusive.OR.expression 361 : exclusive.OR.expression 362 | inclusive.OR.expression '|' exclusive.OR.expression 363 ; 364 365 exclusive.OR.expression 366 : AND.expression 367 | exclusive.OR.expression '^' AND.expression 368 ; 369 370 AND.expression 371 : equality.expression 372 | AND.expression '&' equality.expression 373 ; 374 375 equality.expression 376 : relational.expression 377 | equality.expression EQU relational.expression 378 | equality.expression NEQ relational.expression 379 ; 380 381 relational.expression 382 : shift.expression 383 | relational.expression '<' shift.expression 384 | relational.expression '>' shift.expression 385 | relational.expression LEQ shift.expression 386 | relational.expression GEQ shift.expression 387 ; 388 389 shift.expression 390 : additive.expression 391 | shift.expression LSH additive.expression 392 | shift.expression RSH additive.expression 393 ; 394 395 additive.expression 396 : multiplicative.expression 397 | additive.expression '+' multiplicative.expression 398 | additive.expression '-' multiplicative.expression 399 ; 400 401 multiplicative.expression 402 : cast.expression 403 | multiplicative.expression '*' cast.expression 404 | multiplicative.expression '/' cast.expression 405 | multiplicative.expression '%' cast.expression 406 ; 407 408 cast.expression 409 : unary.expression 410 | '(' type.name ')' cast.expression 411 ; 412 413 unary.expression 414 : postfix.expression 415 | INC unary.expression 416 | DEC unary.expression 417 | unary.operator cast.expression 418 | SIZEOF unary.expression 419 | SIZEOF '(' type.name ')' 420 ; 421 422 unary.operator 423 : '&' | '*' | '+' | '-' | '~' | '!' 424 ; 425 426 postfix.expression 427 : primary.expression 428 | postfix.expression '[' expression ']' 429 | postfix.expression '(' argument.expression.list.opt ')' 430 | postfix.expression '.' IDENTIFIER 431 | postfix.expression ARROW IDENTIFIER 432 | postfix.expression INC 433 | postfix.expression DEC 434 ; 435 436 primary.expression 437 : IDENTIFIER 438 | constant 439 | STRING 440 | '(' expression ')' 441 ; 442 443 argument.expression.list.opt 444 : 445 | argument.expression.list 446 ; 447 448 argument.expression.list 449 : assignment.expression 450 | argument.expression.list ',' assignment.expression 451 ; 452 453 constant 454 : INTCONST 455 | CHARCONST 456 | FLOATCONST 457 ; 458 459