| 1 |
%defs ( ); |
| 2 |
%unitactions; |
| 3 |
|
| 4 |
%tokens |
| 5 |
: COLON (":") |
| 6 |
| SEMICOLON (";") |
| 7 |
| LPAREN ("(") |
| 8 |
| RPAREN (")") |
| 9 |
| LCURLY ("{") |
| 10 |
| RCURLY ("}") |
| 11 |
| LBRACE ("[") |
| 12 |
| RBRACE ("]") |
| 13 |
| DOT (".") |
| 14 |
| COMMA (",") |
| 15 |
| QUESTION ("?") |
| 16 |
| PERCENT ("%") |
| 17 |
| AMP ("&") |
| 18 |
| BAR ("|") |
| 19 |
| TILDE ("~") |
| 20 |
| DIVIDE ("/") |
| 21 |
| PLUS ("+") |
| 22 |
| MINUS ("-") |
| 23 |
| HAT ("^") |
| 24 |
| BANG ("!") |
| 25 |
| TIMES ("*") |
| 26 |
| INC ("++") |
| 27 |
| DEC ("--") |
| 28 |
| ARROW ("->") |
| 29 |
| ID of string |
| 30 |
| EQUALS ("=") |
| 31 |
| PLUSEQUALS ("+=") |
| 32 |
| MINUSEQUALS ("-=") |
| 33 |
| XOREQUALS ("^=") |
| 34 |
| MODEQUALS ("%=") |
| 35 |
| TIMESEQUALS ("*=") |
| 36 |
| DIVEQUALS ("/=") |
| 37 |
| OREQUALS ("|=") |
| 38 |
| ANDEQUALS ("&=") |
| 39 |
| LSHIFTEQUALS ("<<=") |
| 40 |
| RSHIFTEQUALS (">>=") |
| 41 |
| LTE ("<=") |
| 42 |
| GTE (">=") |
| 43 |
| LT ("<") |
| 44 |
| GT (">") |
| 45 |
| EQ ("==") |
| 46 |
| NEQ ("!=") |
| 47 |
| OR ("||") |
| 48 |
| AND ("&&") |
| 49 |
| LSHIFT ("<<") |
| 50 |
| RSHIFT (">>") |
| 51 |
| DECNUM of LargeInt.int |
| 52 |
| REALNUM of real |
| 53 |
| STRING of string |
| 54 |
| CCONST of LargeInt.int |
| 55 |
| EXTERN ("extern") |
| 56 |
| AUTO ("auto") |
| 57 |
| STATIC ("static") |
| 58 |
| REGISTER ("register") |
| 59 |
| CONST ("const") |
| 60 |
| VOLATILE ("volatile") |
| 61 |
| IF ("if") |
| 62 |
| ELSE ("else") |
| 63 |
| FOR ("for") |
| 64 |
| DO ("do") |
| 65 |
| SWITCH ("switch") |
| 66 |
| CASE ("case") |
| 67 |
| DEFAULT ("default") |
| 68 |
| WHILE ("while") |
| 69 |
| RETURN ("return") |
| 70 |
| BREAK ("break") |
| 71 |
| CONTINUE ("continue") |
| 72 |
| GOTO ("goto") |
| 73 |
|
| 74 |
| CHAR ("char") |
| 75 |
| DOUBLE ("double") |
| 76 |
| ENUM ("enum") |
| 77 |
| FLOAT ("float") |
| 78 |
| INT ("int") |
| 79 |
| LONG ("long") |
| 80 |
| SHORT ("short") |
| 81 |
(* | FRACTIONAL | SATURATE (* D *) *) |
| 82 |
| STRUCT ("struct") |
| 83 |
| UNION ("union") |
| 84 |
| UNSIGNED ("unsigned") |
| 85 |
| SIGNED ("signed") |
| 86 |
| VOID ("void") |
| 87 |
| SIZEOF ("sizeof") |
| 88 |
| TYPEDEF ("typedef") |
| 89 |
| ELIPSIS ("...") |
| 90 |
; |
| 91 |
|
| 92 |
translation_unit |
| 93 |
: external_declaration* |
| 94 |
; |
| 95 |
|
| 96 |
external_declaration |
| 97 |
: %try function_definition |
| 98 |
| %try declaration |
| 99 |
; |
| 100 |
|
| 101 |
function_definition |
| 102 |
: declaration_specifiers? declarator declaration* compound_statement |
| 103 |
; |
| 104 |
|
| 105 |
declaration |
| 106 |
: declaration_specifiers init_declarator_list? ";" |
| 107 |
; |
| 108 |
|
| 109 |
declaration_specifiers |
| 110 |
: (storage_class_specifier | type_specifier | type_qualifier)+ |
| 111 |
; |
| 112 |
|
| 113 |
storage_class_specifier |
| 114 |
: "auto" |
| 115 |
| "register" |
| 116 |
| "static" |
| 117 |
| "extern" |
| 118 |
| "typedef" |
| 119 |
; |
| 120 |
|
| 121 |
type_specifier |
| 122 |
: "void" |
| 123 |
| "char" |
| 124 |
| "short" |
| 125 |
| "int" |
| 126 |
| "long" |
| 127 |
| "float" |
| 128 |
| "double" |
| 129 |
| "signed" |
| 130 |
| "unsigned" |
| 131 |
| struct_or_union_specifier |
| 132 |
| enum_specifier |
| 133 |
(* | typedef_name *) |
| 134 |
; |
| 135 |
|
| 136 |
type_qualifier |
| 137 |
: "const" |
| 138 |
| "volatile" |
| 139 |
; |
| 140 |
|
| 141 |
struct_or_union_specifier |
| 142 |
: struct_or_union ID? "{" struct_declaration+ "}" |
| 143 |
| struct_or_union ID |
| 144 |
; |
| 145 |
|
| 146 |
struct_or_union |
| 147 |
: "struct" |
| 148 |
| "union" |
| 149 |
; |
| 150 |
|
| 151 |
init_declarator_list |
| 152 |
: init_declarator ("," init_declarator)* |
| 153 |
; |
| 154 |
|
| 155 |
init_declarator |
| 156 |
: declarator ("=" initializer)? |
| 157 |
; |
| 158 |
|
| 159 |
struct_declaration |
| 160 |
: specifier_qualifier_list struct_declarator_list ";" |
| 161 |
; |
| 162 |
|
| 163 |
specifier_qualifier_list |
| 164 |
: (type_specifier | type_qualifier)+ |
| 165 |
; |
| 166 |
|
| 167 |
struct_declarator_list |
| 168 |
: struct_declarator ("," struct_declarator)* |
| 169 |
; |
| 170 |
|
| 171 |
struct_declarator |
| 172 |
: declarator (":" constant_expression)? |
| 173 |
| ":" constant_expression |
| 174 |
; |
| 175 |
|
| 176 |
enum_specifier |
| 177 |
: "enum" ID? "{" enumerator_list "}" |
| 178 |
| "enum" ID |
| 179 |
; |
| 180 |
|
| 181 |
enumerator_list |
| 182 |
: enumerator ("," enumerator)* |
| 183 |
; |
| 184 |
|
| 185 |
enumerator |
| 186 |
: ID ("=" constant_expression)? |
| 187 |
; |
| 188 |
|
| 189 |
declarator |
| 190 |
: pointer? direct_declarator |
| 191 |
; |
| 192 |
|
| 193 |
direct_declarator |
| 194 |
: inner_direct_declarator |
| 195 |
( "[" constant_expression? "]" |
| 196 |
| "(" parameter_type_list ")" |
| 197 |
| "(" identifier_list? ")" |
| 198 |
)* |
| 199 |
; |
| 200 |
|
| 201 |
inner_direct_declarator |
| 202 |
: ID |
| 203 |
| "(" declarator ")" |
| 204 |
; |
| 205 |
|
| 206 |
pointer |
| 207 |
: ("*" type_qualifier_list?)+ |
| 208 |
; |
| 209 |
|
| 210 |
type_qualifier_list |
| 211 |
: type_qualifier+ |
| 212 |
; |
| 213 |
|
| 214 |
parameter_type_list |
| 215 |
: parameter_list ("," "...")? |
| 216 |
; |
| 217 |
|
| 218 |
parameter_list |
| 219 |
: parameter_declaration ("," parameter_declaration)* |
| 220 |
; |
| 221 |
|
| 222 |
parameter_declaration |
| 223 |
: declaration_specifiers (%try declarator | %try abstract_declarator)? |
| 224 |
; |
| 225 |
|
| 226 |
identifier_list |
| 227 |
: ID ("," ID)* |
| 228 |
; |
| 229 |
|
| 230 |
initializer |
| 231 |
: assignment_expression |
| 232 |
| "{" initializer_list ","? "}" |
| 233 |
; |
| 234 |
|
| 235 |
initializer_list |
| 236 |
: initializer ("," initializer)* |
| 237 |
; |
| 238 |
|
| 239 |
type_name |
| 240 |
: specifier_qualifier_list abstract_declarator? |
| 241 |
; |
| 242 |
|
| 243 |
abstract_declarator |
| 244 |
: pointer direct_abstract_declarator? |
| 245 |
| direct_abstract_declarator |
| 246 |
; |
| 247 |
|
| 248 |
direct_abstract_declarator |
| 249 |
: "(" abstract_declarator ")" |
| 250 |
( "[" constant_expression? "]" |
| 251 |
| "(" parameter_type_list? ")" |
| 252 |
)* |
| 253 |
| ( "[" constant_expression? "]" |
| 254 |
| "(" parameter_type_list? ")" |
| 255 |
)+ |
| 256 |
; |
| 257 |
|
| 258 |
(* |
| 259 |
typedef_name |
| 260 |
: ID |
| 261 |
; |
| 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 |
: ID ":" statement |
| 275 |
| "case" constant_expression ":" statement |
| 276 |
| "default" ":" statement |
| 277 |
; |
| 278 |
|
| 279 |
expression_statement |
| 280 |
: expression? ";" |
| 281 |
; |
| 282 |
|
| 283 |
compound_statement |
| 284 |
: "{" (declaration | statement)* "}" |
| 285 |
; |
| 286 |
|
| 287 |
selection_statement |
| 288 |
: %try "if" "(" expression ")" statement "else" statement |
| 289 |
| %try "if" "(" expression ")" statement |
| 290 |
| "switch" "(" expression ")" statement |
| 291 |
; |
| 292 |
|
| 293 |
iteration_statement |
| 294 |
: "while" "(" expression ")" statement |
| 295 |
| "do" statement "while" "(" expression ")" ";" |
| 296 |
| "for" "(" expression? ";" expression? ";" expression? ")" statement |
| 297 |
; |
| 298 |
|
| 299 |
jump_statement |
| 300 |
: "goto" ID ";" |
| 301 |
| "continue" ";" |
| 302 |
| "break" ";" |
| 303 |
| "return" expression? ";" |
| 304 |
; |
| 305 |
|
| 306 |
expression |
| 307 |
: assignment_expression ("," assignment_expression)* |
| 308 |
; |
| 309 |
|
| 310 |
(* take a look at this ... |
| 311 |
assignment_expression |
| 312 |
: conditional_expression |
| 313 |
| unary_expression assignment_operator assignment_expression |
| 314 |
; |
| 315 |
*) |
| 316 |
assignment_expression |
| 317 |
: conditional_expression (assignment_operator conditional_expression)* |
| 318 |
; |
| 319 |
|
| 320 |
assignment_operator |
| 321 |
: "=" | "*=" | "/=" | "%=" | "+=" | "-=" | "<<=" | ">>=" | "&=" | "^=" | "|=" |
| 322 |
; |
| 323 |
|
| 324 |
conditional_expression |
| 325 |
: logical_OR_expression ("?" expression ":" conditional_expression)? |
| 326 |
; |
| 327 |
|
| 328 |
constant_expression |
| 329 |
: conditional_expression |
| 330 |
; |
| 331 |
|
| 332 |
logical_OR_expression |
| 333 |
: logical_AND_expression ("||" logical_AND_expression)* |
| 334 |
; |
| 335 |
|
| 336 |
logical_AND_expression |
| 337 |
: inclusive_OR_expression ("&&" inclusive_OR_expression)* |
| 338 |
; |
| 339 |
|
| 340 |
inclusive_OR_expression |
| 341 |
: exclusive_OR_expression ("|" exclusive_OR_expression)* |
| 342 |
; |
| 343 |
|
| 344 |
exclusive_OR_expression |
| 345 |
: AND_expression ("^" AND_expression)* |
| 346 |
; |
| 347 |
|
| 348 |
AND_expression |
| 349 |
: equality_expression ("&" equality_expression)* |
| 350 |
; |
| 351 |
|
| 352 |
equality_expression |
| 353 |
: relational_expression (("==" | "!=") relational_expression)* |
| 354 |
; |
| 355 |
|
| 356 |
relational_expression |
| 357 |
: shift_expression |
| 358 |
(("<" | ">" | "<=" | ">=") shift_expression)* |
| 359 |
; |
| 360 |
|
| 361 |
shift_expression |
| 362 |
: additive_expression (("<<" | ">>") additive_expression)* |
| 363 |
; |
| 364 |
|
| 365 |
additive_expression |
| 366 |
: multiplicative_expression (("+" | "-") multiplicative_expression)* |
| 367 |
; |
| 368 |
|
| 369 |
multiplicative_expression |
| 370 |
: cast_expression (("*" | "/" | "%") cast_expression)* |
| 371 |
; |
| 372 |
|
| 373 |
cast_expression |
| 374 |
: unary_expression |
| 375 |
| "(" type_name ")" cast_expression |
| 376 |
; |
| 377 |
|
| 378 |
unary_expression |
| 379 |
: postfix_expression |
| 380 |
| "++" unary_expression |
| 381 |
| "--" unary_expression |
| 382 |
| ("&" | "*" | "+" | "-" | "~" | "!") cast_expression |
| 383 |
| "sizeof" unary_expression |
| 384 |
| "sizeof" "(" type_name ")" |
| 385 |
; |
| 386 |
|
| 387 |
postfix_expression |
| 388 |
: primary_expression |
| 389 |
( "[" expression "]" |
| 390 |
| "(" argument_expression_list? ")" |
| 391 |
| "." ID |
| 392 |
| "->" ID |
| 393 |
| "++" |
| 394 |
| "--" |
| 395 |
)* |
| 396 |
; |
| 397 |
|
| 398 |
primary_expression |
| 399 |
: ID |
| 400 |
| constant |
| 401 |
| "(" expression ")" |
| 402 |
; |
| 403 |
|
| 404 |
argument_expression_list |
| 405 |
: assignment_expression ("," assignment_expression)* |
| 406 |
; |
| 407 |
|
| 408 |
constant |
| 409 |
: DECNUM |
| 410 |
| REALNUM |
| 411 |
| STRING |
| 412 |
| CCONST |
| 413 |
; |