depager 0.2.3 → 0.3.0.b20160729

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. checksums.yaml +7 -0
  2. data/README.en +4 -19
  3. data/README.ja +42 -79
  4. data/bin/depager +42 -45
  5. data/examples/action_pl0d/pl0d.action.dr +421 -0
  6. data/examples/action_pl0d/test.pl0ds +49 -0
  7. data/examples/c89/c89.dr +493 -496
  8. data/examples/c89/test.c89 +10 -10
  9. data/examples/extension/astdf.rb +10 -0
  10. data/examples/extension/atree.dr +55 -0
  11. data/examples/{sample_calc → extension}/calc.atree.dr +42 -43
  12. data/examples/extension/calc.simple_action.dr +33 -0
  13. data/examples/extension/paction.dr +16 -15
  14. data/examples/extension/pactiontest.dr +14 -14
  15. data/examples/extension/simple_action.rb +44 -0
  16. data/examples/pl0d/pl0ds.dr +337 -334
  17. data/examples/pl0d/test.pl0ds +33 -33
  18. data/examples/rie_calc/calc.rie.dr +57 -0
  19. data/examples/rie_calc/test.calc +4 -0
  20. data/examples/rie_dcuse/dcuse.rie.dr +71 -0
  21. data/examples/rie_dcuse/test.dcuse +1 -0
  22. data/examples/rie_pl0/orig_ex/exerrdg.pl0 +44 -0
  23. data/examples/rie_pl0/orig_ex/exerrm.pl0 +19 -0
  24. data/examples/rie_pl0/orig_ex/exerrmre.pl0 +20 -0
  25. data/examples/rie_pl0/orig_ex/exerrtok.pl0 +18 -0
  26. data/examples/rie_pl0/orig_ex/exmdg.pl0 +40 -0
  27. data/examples/rie_pl0/orig_ex/exmdgwwl.pl0 +43 -0
  28. data/examples/rie_pl0/orig_ex/exmrw.pl0 +22 -0
  29. data/examples/rie_pl0/orig_ex/exmwwl.pl0 +18 -0
  30. data/examples/rie_pl0/orig_ex/exnorw.pl0 +17 -0
  31. data/examples/rie_pl0/pl0.rie.dr +450 -0
  32. data/examples/rie_pl0/test.pl0 +10 -0
  33. data/examples/sample_calc/calc.action.dr +33 -33
  34. data/examples/sample_calc/calc.ast.action.dr +65 -66
  35. data/examples/sample_calc/calc.ast.dr +55 -55
  36. data/examples/sample_calc/calc.cst.dr +45 -45
  37. data/examples/sample_calc/calc.dr +43 -43
  38. data/examples/sample_calc/calc.lex.dr +29 -29
  39. data/examples/sample_calc/{calc_prec.nvaction.dr → calc_prec.action.dr} +31 -31
  40. data/examples/slex_test/divreg.slex.dr +29 -29
  41. data/examples/slex_test/ljoin.slex.dr +36 -36
  42. data/examples/slex_test/test.divreg +1 -1
  43. data/examples/slex_test/test.ljoin +3 -3
  44. data/lib/depager.rb +582 -670
  45. data/lib/depager/grammar.rb +256 -291
  46. data/lib/depager/lr.rb +574 -579
  47. data/lib/depager/parser.rb +282 -277
  48. data/lib/depager/ruby/plugins/_rie_debug.rb +35 -0
  49. data/lib/depager/ruby/plugins/action.rb +53 -43
  50. data/lib/depager/ruby/plugins/ast.dr +364 -269
  51. data/lib/depager/ruby/plugins/ast.rb +1367 -1308
  52. data/lib/depager/ruby/plugins/cst.dr +172 -180
  53. data/lib/depager/ruby/plugins/cst.rb +587 -626
  54. data/lib/depager/ruby/plugins/lex.dr +85 -89
  55. data/lib/depager/ruby/plugins/lex.rb +310 -336
  56. data/lib/depager/ruby/plugins/rie.dr +723 -0
  57. data/lib/depager/ruby/plugins/rie.rb +1653 -0
  58. data/lib/depager/ruby/plugins/slex.dr +202 -200
  59. data/lib/depager/ruby/plugins/slex.rb +780 -817
  60. data/lib/depager/ruby/plugins/srp.rb +56 -51
  61. data/lib/depager/ruby/templates/extension_lalr_master.erb +46 -51
  62. data/lib/depager/ruby/templates/extension_lalr_slave.erb +99 -107
  63. data/lib/depager/ruby/templates/single_lalr_parser.erb +115 -117
  64. data/lib/depager/utils.rb +148 -318
  65. data/lib/depager/version.rb +4 -3
  66. metadata +52 -60
  67. data/ChangeLog +0 -16
  68. data/data/depager/pre-setup.rb +0 -3
  69. data/examples/c89/c89.tab.rb +0 -7127
  70. data/examples/pl0d/pl0ds.tab.rb +0 -2698
  71. data/examples/sample_calc/calc.action.tab.rb +0 -457
  72. data/examples/sample_calc/calc.ast.action.tab.rb +0 -749
  73. data/examples/sample_calc/calc.ast.tab.rb +0 -665
  74. data/examples/sample_calc/calc.astdf.dr +0 -54
  75. data/examples/sample_calc/calc.astdf.tab.rb +0 -672
  76. data/examples/sample_calc/calc.atree.tab.rb +0 -451
  77. data/examples/sample_calc/calc.cst.tab.rb +0 -644
  78. data/examples/sample_calc/calc.lex.tab.rb +0 -374
  79. data/examples/sample_calc/calc.nvaction.dr +0 -33
  80. data/examples/sample_calc/calc.nvaction.tab.rb +0 -465
  81. data/examples/sample_calc/calc.tab.rb +0 -365
  82. data/examples/sample_calc/calc_prec.nvaction.tab.rb +0 -431
  83. data/examples/slex_test/divreg.slex.tab.rb +0 -303
  84. data/examples/slex_test/ljoin.slex.tab.rb +0 -370
  85. data/lib/depager/ruby/plugins/_ast_tmpl.rb +0 -73
  86. data/lib/depager/ruby/plugins/astdf.rb +0 -6
  87. data/lib/depager/ruby/plugins/atree.dr +0 -55
  88. data/lib/depager/ruby/plugins/atree.rb +0 -347
  89. data/lib/depager/ruby/plugins/nvaction.rb +0 -19
  90. data/lib/depager/ruby/templates/simple.erb +0 -23
  91. data/setup.rb +0 -1585
@@ -0,0 +1,49 @@
1
+ const m = 7, n = 85;
2
+ var x,y;
3
+
4
+ function puts_ntimes(x, n)
5
+ var i;
6
+ begin
7
+ i := 0;
8
+ while i < n do
9
+ begin
10
+ write x; writeln;
11
+ i := i + 1;
12
+ end;
13
+ return i
14
+ end;
15
+
16
+ function fact(x)
17
+ begin
18
+ write x; writeln;
19
+
20
+ if x = 1 then return 1;
21
+ return x * fact(x - 1)
22
+ end;
23
+
24
+ function outer(x, z)
25
+ function inner(x)
26
+ begin
27
+ write x; write y; write z; writeln;
28
+ return outer(x + 1, z - 1)
29
+ end;
30
+ begin
31
+ if z = 0 then return 0;
32
+ return inner(x)
33
+ end;
34
+
35
+
36
+ begin
37
+ x := m; y := n;
38
+ write x; write y; writeln;
39
+ x := 84; y := 36;
40
+ write x; write y; writeln;
41
+
42
+ y := puts_ntimes(5, 10);
43
+ write y; writeln;
44
+
45
+ y := fact(5);
46
+ write y; writeln;
47
+
48
+ y := outer(5, 5);
49
+ end.
@@ -1,496 +1,493 @@
1
- %class CParser
2
- # from http://www.lysator.liu.se/c/ANSI-C-grammar-y.html and
3
- # http://www.lysator.liu.se/c/ANSI-C-grammar-l.html
4
- %extend Lexer ('plugins/lex.rb')
5
- #%decorate ShiftReducePrinter ('plugins/srp.rb')
6
- %inner{
7
- def initialize
8
- super
9
- @keywords = {
10
- "auto" => :AUTO,
11
- "break" => :BREAK,
12
- "case" => :CASE,
13
- "char" => :CHAR,
14
- "const" => :CONST,
15
- "continue" => :CONTINUE,
16
- "default" => :DEFAULT,
17
- "do" => :DO,
18
- "double" => :DOUBLE,
19
- "else" => :ELSE,
20
- "enum" => :ENUM,
21
- "extern" => :EXTERN,
22
- "float" => :FLOAT,
23
- "for" => :FOR,
24
- "goto" => :GOTO,
25
- "if" => :IF,
26
- "int" => :INT,
27
- "long" => :LONG,
28
- "register" => :REGISTER,
29
- "return" => :RETURN,
30
- "short" => :SHORT,
31
- "signed" => :SIGNED,
32
- "sizeof" => :SIZEOF,
33
- "static" => :STATIC,
34
- "struct" => :STRUCT,
35
- "switch" => :SWITCH,
36
- "typedef" => :TYPEDEF,
37
- "union" => :UNION,
38
- "unsigned" => :UNSIGNED,
39
- "void" => :VOID,
40
- "volatile" => :VOLATILE,
41
- "while" => :WHILE,
42
- }
43
- end
44
- %}
45
- %%
46
- %LEX{
47
- /#.*\n/ { @line0 =~ /^#/ || raise }
48
- '/*'
49
- {
50
- begin
51
- break if @line =~ /\*\//
52
- end while @line = getline
53
- @line = $'
54
- }
55
- /\/\/[^\n]*/ { }
56
-
57
- /[a-zA-Z_][a-zA-Z0-9_]*/ { yield token(@keywords[$&] || :IDENTIFIER) }
58
- /0[xX][A-F]+/ { yield token(:CONSTANT) }
59
- /0[0-9]+/ { yield token(:CONSTANT) }
60
- /[0-9]+/ { yield token(:CONSTANT) }
61
- /[0-9]+/ { yield token(:CONSTANT) }
62
- /[0-9]*\.[0-9]+/ { yield token(:CONSTANT) }
63
- /"(\\.|[^"\\])*"/ { yield token(:STRING_LITERAL) }
64
-
65
- '...' { yield token(:ELLIPSIS) }
66
- '>>=' { yield token(:RIGHT_ASSIGN) }
67
- '<<=' { yield token(:LEFT_ASSIGN) }
68
- '+=' { yield token(:ADD_ASSIGN) }
69
- '-=' { yield token(:SUB_ASSIGN) }
70
- '*=' { yield token(:MUL_ASSIGN) }
71
- '/=' { yield token(:DIV_ASSIGN) }
72
- '%=' { yield token(:MOD_ASSIGN) }
73
- '&=' { yield token(:AND_ASSIGN) }
74
- '^=' { yield token(:XOR_ASSIGN) }
75
- '|=' { yield token(:OR_ASSIGN) }
76
- '>>' { yield token(:RIGHT_OP) }
77
- '<<' { yield token(:LEFT_OP) }
78
- '++' { yield token(:INC_OP) }
79
- '--' { yield token(:DEC_OP) }
80
- '->' { yield token(:PTR_OP) }
81
- '&&' { yield token(:AND_OP) }
82
- '||' { yield token(:OR_OP) }
83
- '<=' { yield token(:LE_OP) }
84
- '>=' { yield token(:GE_OP) }
85
- '==' { yield token(:EQ_OP) }
86
- '!=' { yield token(:NE_OP) }
87
-
88
- /[ \t\v\n\f]/ { }
89
- /./ { yield token($&) }
90
- %}
91
-
92
- #begin-rule
93
- translation_unit
94
- : external_declaration
95
- | translation_unit external_declaration
96
- ;
97
-
98
- external_declaration
99
- : function_definition
100
- | declaration
101
- ;
102
-
103
- function_definition
104
- : declaration_specifiers declarator declaration_list compound_statement
105
- | declaration_specifiers declarator compound_statement
106
- | declarator declaration_list compound_statement
107
- | declarator compound_statement
108
- ;
109
-
110
- primary_expression
111
- : IDENTIFIER
112
- | CONSTANT
113
- | STRING_LITERAL
114
- | '(' expression ')'
115
- ;
116
-
117
- postfix_expression
118
- : primary_expression
119
- | postfix_expression '[' expression ']'
120
- | postfix_expression '(' ')'
121
- | postfix_expression '(' argument_expression_list ')'
122
- | postfix_expression '.' IDENTIFIER
123
- | postfix_expression PTR_OP IDENTIFIER
124
- | postfix_expression INC_OP
125
- | postfix_expression DEC_OP
126
- ;
127
-
128
- argument_expression_list
129
- : assignment_expression
130
- | argument_expression_list ',' assignment_expression
131
- ;
132
-
133
- unary_expression
134
- : postfix_expression
135
- | INC_OP unary_expression
136
- | DEC_OP unary_expression
137
- | unary_operator cast_expression
138
- | SIZEOF unary_expression
139
- | SIZEOF '(' type_name ')'
140
- ;
141
-
142
- unary_operator
143
- : '&'
144
- | '*'
145
- | '+'
146
- | '-'
147
- | '~'
148
- | '!'
149
- ;
150
-
151
- cast_expression
152
- : unary_expression
153
- | '(' type_name ')' cast_expression
154
- ;
155
-
156
- multiplicative_expression
157
- : cast_expression
158
- | multiplicative_expression '*' cast_expression
159
- | multiplicative_expression '/' cast_expression
160
- | multiplicative_expression '%' cast_expression
161
- ;
162
-
163
- additive_expression
164
- : multiplicative_expression
165
- | additive_expression '+' multiplicative_expression
166
- | additive_expression '-' multiplicative_expression
167
- ;
168
-
169
- shift_expression
170
- : additive_expression
171
- | shift_expression LEFT_OP additive_expression
172
- | shift_expression RIGHT_OP additive_expression
173
- ;
174
-
175
- relational_expression
176
- : shift_expression
177
- | relational_expression '<' shift_expression
178
- | relational_expression '>' shift_expression
179
- | relational_expression LE_OP shift_expression
180
- | relational_expression GE_OP shift_expression
181
- ;
182
-
183
- equality_expression
184
- : relational_expression
185
- | equality_expression EQ_OP relational_expression
186
- | equality_expression NE_OP relational_expression
187
- ;
188
-
189
- and_expression
190
- : equality_expression
191
- | and_expression '&' equality_expression
192
- ;
193
-
194
- exclusive_or_expression
195
- : and_expression
196
- | exclusive_or_expression '^' and_expression
197
- ;
198
-
199
- inclusive_or_expression
200
- : exclusive_or_expression
201
- | inclusive_or_expression '|' exclusive_or_expression
202
- ;
203
-
204
- logical_and_expression
205
- : inclusive_or_expression
206
- | logical_and_expression AND_OP inclusive_or_expression
207
- ;
208
-
209
- logical_or_expression
210
- : logical_and_expression
211
- | logical_or_expression OR_OP logical_and_expression
212
- ;
213
-
214
- conditional_expression
215
- : logical_or_expression
216
- | logical_or_expression '?' expression ':' conditional_expression
217
- ;
218
-
219
- assignment_expression
220
- : conditional_expression
221
- | unary_expression assignment_operator assignment_expression
222
- ;
223
-
224
- assignment_operator
225
- : '='
226
- | MUL_ASSIGN
227
- | DIV_ASSIGN
228
- | MOD_ASSIGN
229
- | ADD_ASSIGN
230
- | SUB_ASSIGN
231
- | LEFT_ASSIGN
232
- | RIGHT_ASSIGN
233
- | AND_ASSIGN
234
- | XOR_ASSIGN
235
- | OR_ASSIGN
236
- ;
237
-
238
- expression
239
- : assignment_expression
240
- | expression ',' assignment_expression
241
- ;
242
-
243
- constant_expression
244
- : conditional_expression
245
- ;
246
-
247
- declaration
248
- : declaration_specifiers ';'
249
- | declaration_specifiers init_declarator_list ';'
250
- ;
251
-
252
- declaration_specifiers
253
- : storage_class_specifier
254
- | storage_class_specifier declaration_specifiers
255
- | type_specifier
256
- | type_specifier declaration_specifiers
257
- | type_qualifier
258
- | type_qualifier declaration_specifiers
259
- ;
260
-
261
- init_declarator_list
262
- : init_declarator
263
- | init_declarator_list ',' init_declarator
264
- ;
265
-
266
- init_declarator
267
- : declarator
268
- | declarator '=' initializer
269
- ;
270
-
271
- storage_class_specifier
272
- : TYPEDEF
273
- | EXTERN
274
- | STATIC
275
- | AUTO
276
- | REGISTER
277
- ;
278
-
279
- type_specifier
280
- : VOID
281
- | CHAR
282
- | SHORT
283
- | INT
284
- | LONG
285
- | FLOAT
286
- | DOUBLE
287
- | SIGNED
288
- | UNSIGNED
289
- | struct_or_union_specifier
290
- | enum_specifier
291
- | TYPE_NAME
292
- ;
293
-
294
- struct_or_union_specifier
295
- : struct_or_union IDENTIFIER '{' struct_declaration_list '}'
296
- | struct_or_union '{' struct_declaration_list '}'
297
- | struct_or_union IDENTIFIER
298
- ;
299
-
300
- struct_or_union
301
- : STRUCT
302
- | UNION
303
- ;
304
-
305
- struct_declaration_list
306
- : struct_declaration
307
- | struct_declaration_list struct_declaration
308
- ;
309
-
310
- struct_declaration
311
- : specifier_qualifier_list struct_declarator_list ';'
312
- ;
313
-
314
- specifier_qualifier_list
315
- : type_specifier specifier_qualifier_list
316
- | type_specifier
317
- | type_qualifier specifier_qualifier_list
318
- | type_qualifier
319
- ;
320
-
321
- struct_declarator_list
322
- : struct_declarator
323
- | struct_declarator_list ',' struct_declarator
324
- ;
325
-
326
- struct_declarator
327
- : declarator
328
- | ':' constant_expression
329
- | declarator ':' constant_expression
330
- ;
331
-
332
- enum_specifier
333
- : ENUM '{' enumerator_list '}'
334
- | ENUM IDENTIFIER '{' enumerator_list '}'
335
- | ENUM IDENTIFIER
336
- ;
337
-
338
- enumerator_list
339
- : enumerator
340
- | enumerator_list ',' enumerator
341
- ;
342
-
343
- enumerator
344
- : IDENTIFIER
345
- | IDENTIFIER '=' constant_expression
346
- ;
347
-
348
- type_qualifier
349
- : CONST
350
- | VOLATILE
351
- ;
352
-
353
- declarator
354
- : pointer direct_declarator
355
- | direct_declarator
356
- ;
357
-
358
- direct_declarator
359
- : IDENTIFIER
360
- | '(' declarator ')'
361
- | direct_declarator '[' constant_expression ']'
362
- | direct_declarator '[' ']'
363
- | direct_declarator '(' parameter_type_list ')'
364
- | direct_declarator '(' identifier_list ')'
365
- | direct_declarator '(' ')'
366
- ;
367
-
368
- pointer
369
- : '*'
370
- | '*' type_qualifier_list
371
- | '*' pointer
372
- | '*' type_qualifier_list pointer
373
- ;
374
-
375
- type_qualifier_list
376
- : type_qualifier
377
- | type_qualifier_list type_qualifier
378
- ;
379
-
380
-
381
- parameter_type_list
382
- : parameter_list
383
- | parameter_list ',' ELLIPSIS
384
- ;
385
-
386
- parameter_list
387
- : parameter_declaration
388
- | parameter_list ',' parameter_declaration
389
- ;
390
-
391
- parameter_declaration
392
- : declaration_specifiers declarator
393
- | declaration_specifiers abstract_declarator
394
- | declaration_specifiers
395
- ;
396
-
397
- identifier_list
398
- : IDENTIFIER
399
- | identifier_list ',' IDENTIFIER
400
- ;
401
-
402
- type_name
403
- : specifier_qualifier_list
404
- | specifier_qualifier_list abstract_declarator
405
- ;
406
-
407
- abstract_declarator
408
- : pointer
409
- | direct_abstract_declarator
410
- | pointer direct_abstract_declarator
411
- ;
412
-
413
- direct_abstract_declarator
414
- : '(' abstract_declarator ')'
415
- | '[' ']'
416
- | '[' constant_expression ']'
417
- | direct_abstract_declarator '[' ']'
418
- | direct_abstract_declarator '[' constant_expression ']'
419
- | '(' ')'
420
- | '(' parameter_type_list ')'
421
- | direct_abstract_declarator '(' ')'
422
- | direct_abstract_declarator '(' parameter_type_list ')'
423
- ;
424
-
425
- initializer
426
- : assignment_expression
427
- | '{' initializer_list '}'
428
- | '{' initializer_list ',' '}'
429
- ;
430
-
431
- initializer_list
432
- : initializer
433
- | initializer_list ',' initializer
434
- ;
435
-
436
- statement
437
- : labeled_statement
438
- | compound_statement
439
- | expression_statement
440
- | selection_statement
441
- | iteration_statement
442
- | jump_statement
443
- ;
444
-
445
- labeled_statement
446
- : IDENTIFIER ':' statement
447
- | CASE constant_expression ':' statement
448
- | DEFAULT ':' statement
449
- ;
450
-
451
- compound_statement
452
- : '{' '}'
453
- | '{' statement_list '}'
454
- | '{' declaration_list '}'
455
- | '{' declaration_list statement_list '}'
456
- ;
457
-
458
- declaration_list
459
- : declaration
460
- | declaration_list declaration
461
- ;
462
-
463
- statement_list
464
- : statement
465
- | statement_list statement
466
- ;
467
-
468
- expression_statement
469
- : ';'
470
- | expression ';'
471
- ;
472
-
473
- selection_statement
474
- : IF '(' expression ')' statement
475
- | IF '(' expression ')' statement ELSE statement
476
- | SWITCH '(' expression ')' statement
477
- ;
478
-
479
- iteration_statement
480
- : WHILE '(' expression ')' statement
481
- | DO statement WHILE '(' expression ')' ';'
482
- | FOR '(' expression_statement expression_statement ')' statement
483
- | FOR '(' expression_statement expression_statement expression ')' statement
484
- ;
485
-
486
- jump_statement
487
- : GOTO IDENTIFIER ';'
488
- | CONTINUE ';'
489
- | BREAK ';'
490
- | RETURN ';'
491
- | RETURN expression ';'
492
- ;
493
- #end-rule
494
- %%
495
- p = CParser::createDecoratedParser
496
- p.parse(STDIN)
1
+ %class CParser::Parser
2
+ # from http://www.lysator.liu.se/c/ANSI-C-grammar-y.html and
3
+ # http://www.lysator.liu.se/c/ANSI-C-grammar-l.html
4
+ %extend Depager::Lexer ('plugins/lex.rb')
5
+ #%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
6
+ %inner{
7
+ KEYWORDS = {
8
+ "auto" => :AUTO,
9
+ "break" => :BREAK,
10
+ "case" => :CASE,
11
+ "char" => :CHAR,
12
+ "const" => :CONST,
13
+ "continue" => :CONTINUE,
14
+ "default" => :DEFAULT,
15
+ "do" => :DO,
16
+ "double" => :DOUBLE,
17
+ "else" => :ELSE,
18
+ "enum" => :ENUM,
19
+ "extern" => :EXTERN,
20
+ "float" => :FLOAT,
21
+ "for" => :FOR,
22
+ "goto" => :GOTO,
23
+ "if" => :IF,
24
+ "int" => :INT,
25
+ "long" => :LONG,
26
+ "register" => :REGISTER,
27
+ "return" => :RETURN,
28
+ "short" => :SHORT,
29
+ "signed" => :SIGNED,
30
+ "sizeof" => :SIZEOF,
31
+ "static" => :STATIC,
32
+ "struct" => :STRUCT,
33
+ "switch" => :SWITCH,
34
+ "typedef" => :TYPEDEF,
35
+ "union" => :UNION,
36
+ "unsigned" => :UNSIGNED,
37
+ "void" => :VOID,
38
+ "volatile" => :VOLATILE,
39
+ "while" => :WHILE,
40
+ }
41
+ %}
42
+ %%
43
+ %LEX{
44
+ /#.*\n/ { @line_orig =~ /^#/ || raise }
45
+ '/*'
46
+ {
47
+ begin
48
+ break if @line =~ /\*\//
49
+ end while @line = file.gets
50
+ @line = $'
51
+ }
52
+ /\/\/[^\n]*/ { }
53
+
54
+ /[a-zA-Z_][a-zA-Z0-9_]*/ { yield token(KEYWORDS[$&] || :IDENTIFIER) }
55
+ /0[xX][0-9a-fA-F]+/ { yield token(:CONSTANT) }
56
+ /0[0-9]+/ { yield token(:CONSTANT) }
57
+ /[0-9]+/ { yield token(:CONSTANT) }
58
+ /[0-9]+/ { yield token(:CONSTANT) }
59
+ /[0-9]*\.[0-9]+/ { yield token(:CONSTANT) }
60
+ /"(\\.|[^"\\])*"/ { yield token(:STRING_LITERAL) }
61
+
62
+ '...' { yield token(:ELLIPSIS) }
63
+ '>>=' { yield token(:RIGHT_ASSIGN) }
64
+ '<<=' { yield token(:LEFT_ASSIGN) }
65
+ '+=' { yield token(:ADD_ASSIGN) }
66
+ '-=' { yield token(:SUB_ASSIGN) }
67
+ '*=' { yield token(:MUL_ASSIGN) }
68
+ '/=' { yield token(:DIV_ASSIGN) }
69
+ '%=' { yield token(:MOD_ASSIGN) }
70
+ '&=' { yield token(:AND_ASSIGN) }
71
+ '^=' { yield token(:XOR_ASSIGN) }
72
+ '|=' { yield token(:OR_ASSIGN) }
73
+ '>>' { yield token(:RIGHT_OP) }
74
+ '<<' { yield token(:LEFT_OP) }
75
+ '++' { yield token(:INC_OP) }
76
+ '--' { yield token(:DEC_OP) }
77
+ '->' { yield token(:PTR_OP) }
78
+ '&&' { yield token(:AND_OP) }
79
+ '||' { yield token(:OR_OP) }
80
+ '<=' { yield token(:LE_OP) }
81
+ '>=' { yield token(:GE_OP) }
82
+ '==' { yield token(:EQ_OP) }
83
+ '!=' { yield token(:NE_OP) }
84
+
85
+ /[ \t\v\r\n\f]/ { }
86
+ /./ { yield token($&) }
87
+ %}
88
+
89
+ #begin-rule
90
+ translation_unit
91
+ : external_declaration
92
+ | translation_unit external_declaration
93
+ ;
94
+
95
+ external_declaration
96
+ : function_definition
97
+ | declaration
98
+ ;
99
+
100
+ function_definition
101
+ : declaration_specifiers declarator declaration_list compound_statement
102
+ | declaration_specifiers declarator compound_statement
103
+ | declarator declaration_list compound_statement
104
+ | declarator compound_statement
105
+ ;
106
+
107
+ primary_expression
108
+ : IDENTIFIER
109
+ | CONSTANT
110
+ | STRING_LITERAL
111
+ | '(' expression ')'
112
+ ;
113
+
114
+ postfix_expression
115
+ : primary_expression
116
+ | postfix_expression '[' expression ']'
117
+ | postfix_expression '(' ')'
118
+ | postfix_expression '(' argument_expression_list ')'
119
+ | postfix_expression '.' IDENTIFIER
120
+ | postfix_expression PTR_OP IDENTIFIER
121
+ | postfix_expression INC_OP
122
+ | postfix_expression DEC_OP
123
+ ;
124
+
125
+ argument_expression_list
126
+ : assignment_expression
127
+ | argument_expression_list ',' assignment_expression
128
+ ;
129
+
130
+ unary_expression
131
+ : postfix_expression
132
+ | INC_OP unary_expression
133
+ | DEC_OP unary_expression
134
+ | unary_operator cast_expression
135
+ | SIZEOF unary_expression
136
+ | SIZEOF '(' type_name ')'
137
+ ;
138
+
139
+ unary_operator
140
+ : '&'
141
+ | '*'
142
+ | '+'
143
+ | '-'
144
+ | '~'
145
+ | '!'
146
+ ;
147
+
148
+ cast_expression
149
+ : unary_expression
150
+ | '(' type_name ')' cast_expression
151
+ ;
152
+
153
+ multiplicative_expression
154
+ : cast_expression
155
+ | multiplicative_expression '*' cast_expression
156
+ | multiplicative_expression '/' cast_expression
157
+ | multiplicative_expression '%' cast_expression
158
+ ;
159
+
160
+ additive_expression
161
+ : multiplicative_expression
162
+ | additive_expression '+' multiplicative_expression
163
+ | additive_expression '-' multiplicative_expression
164
+ ;
165
+
166
+ shift_expression
167
+ : additive_expression
168
+ | shift_expression LEFT_OP additive_expression
169
+ | shift_expression RIGHT_OP additive_expression
170
+ ;
171
+
172
+ relational_expression
173
+ : shift_expression
174
+ | relational_expression '<' shift_expression
175
+ | relational_expression '>' shift_expression
176
+ | relational_expression LE_OP shift_expression
177
+ | relational_expression GE_OP shift_expression
178
+ ;
179
+
180
+ equality_expression
181
+ : relational_expression
182
+ | equality_expression EQ_OP relational_expression
183
+ | equality_expression NE_OP relational_expression
184
+ ;
185
+
186
+ and_expression
187
+ : equality_expression
188
+ | and_expression '&' equality_expression
189
+ ;
190
+
191
+ exclusive_or_expression
192
+ : and_expression
193
+ | exclusive_or_expression '^' and_expression
194
+ ;
195
+
196
+ inclusive_or_expression
197
+ : exclusive_or_expression
198
+ | inclusive_or_expression '|' exclusive_or_expression
199
+ ;
200
+
201
+ logical_and_expression
202
+ : inclusive_or_expression
203
+ | logical_and_expression AND_OP inclusive_or_expression
204
+ ;
205
+
206
+ logical_or_expression
207
+ : logical_and_expression
208
+ | logical_or_expression OR_OP logical_and_expression
209
+ ;
210
+
211
+ conditional_expression
212
+ : logical_or_expression
213
+ | logical_or_expression '?' expression ':' conditional_expression
214
+ ;
215
+
216
+ assignment_expression
217
+ : conditional_expression
218
+ | unary_expression assignment_operator assignment_expression
219
+ ;
220
+
221
+ assignment_operator
222
+ : '='
223
+ | MUL_ASSIGN
224
+ | DIV_ASSIGN
225
+ | MOD_ASSIGN
226
+ | ADD_ASSIGN
227
+ | SUB_ASSIGN
228
+ | LEFT_ASSIGN
229
+ | RIGHT_ASSIGN
230
+ | AND_ASSIGN
231
+ | XOR_ASSIGN
232
+ | OR_ASSIGN
233
+ ;
234
+
235
+ expression
236
+ : assignment_expression
237
+ | expression ',' assignment_expression
238
+ ;
239
+
240
+ constant_expression
241
+ : conditional_expression
242
+ ;
243
+
244
+ declaration
245
+ : declaration_specifiers ';'
246
+ | declaration_specifiers init_declarator_list ';'
247
+ ;
248
+
249
+ declaration_specifiers
250
+ : storage_class_specifier
251
+ | storage_class_specifier declaration_specifiers
252
+ | type_specifier
253
+ | type_specifier declaration_specifiers
254
+ | type_qualifier
255
+ | type_qualifier declaration_specifiers
256
+ ;
257
+
258
+ init_declarator_list
259
+ : init_declarator
260
+ | init_declarator_list ',' init_declarator
261
+ ;
262
+
263
+ init_declarator
264
+ : declarator
265
+ | declarator '=' initializer
266
+ ;
267
+
268
+ storage_class_specifier
269
+ : TYPEDEF
270
+ | EXTERN
271
+ | STATIC
272
+ | AUTO
273
+ | REGISTER
274
+ ;
275
+
276
+ type_specifier
277
+ : VOID
278
+ | CHAR
279
+ | SHORT
280
+ | INT
281
+ | LONG
282
+ | FLOAT
283
+ | DOUBLE
284
+ | SIGNED
285
+ | UNSIGNED
286
+ | struct_or_union_specifier
287
+ | enum_specifier
288
+ | TYPE_NAME
289
+ ;
290
+
291
+ struct_or_union_specifier
292
+ : struct_or_union IDENTIFIER '{' struct_declaration_list '}'
293
+ | struct_or_union '{' struct_declaration_list '}'
294
+ | struct_or_union IDENTIFIER
295
+ ;
296
+
297
+ struct_or_union
298
+ : STRUCT
299
+ | UNION
300
+ ;
301
+
302
+ struct_declaration_list
303
+ : struct_declaration
304
+ | struct_declaration_list struct_declaration
305
+ ;
306
+
307
+ struct_declaration
308
+ : specifier_qualifier_list struct_declarator_list ';'
309
+ ;
310
+
311
+ specifier_qualifier_list
312
+ : type_specifier specifier_qualifier_list
313
+ | type_specifier
314
+ | type_qualifier specifier_qualifier_list
315
+ | type_qualifier
316
+ ;
317
+
318
+ struct_declarator_list
319
+ : struct_declarator
320
+ | struct_declarator_list ',' struct_declarator
321
+ ;
322
+
323
+ struct_declarator
324
+ : declarator
325
+ | ':' constant_expression
326
+ | declarator ':' constant_expression
327
+ ;
328
+
329
+ enum_specifier
330
+ : ENUM '{' enumerator_list '}'
331
+ | ENUM IDENTIFIER '{' enumerator_list '}'
332
+ | ENUM IDENTIFIER
333
+ ;
334
+
335
+ enumerator_list
336
+ : enumerator
337
+ | enumerator_list ',' enumerator
338
+ ;
339
+
340
+ enumerator
341
+ : IDENTIFIER
342
+ | IDENTIFIER '=' constant_expression
343
+ ;
344
+
345
+ type_qualifier
346
+ : CONST
347
+ | VOLATILE
348
+ ;
349
+
350
+ declarator
351
+ : pointer direct_declarator
352
+ | direct_declarator
353
+ ;
354
+
355
+ direct_declarator
356
+ : IDENTIFIER
357
+ | '(' declarator ')'
358
+ | direct_declarator '[' constant_expression ']'
359
+ | direct_declarator '[' ']'
360
+ | direct_declarator '(' parameter_type_list ')'
361
+ | direct_declarator '(' identifier_list ')'
362
+ | direct_declarator '(' ')'
363
+ ;
364
+
365
+ pointer
366
+ : '*'
367
+ | '*' type_qualifier_list
368
+ | '*' pointer
369
+ | '*' type_qualifier_list pointer
370
+ ;
371
+
372
+ type_qualifier_list
373
+ : type_qualifier
374
+ | type_qualifier_list type_qualifier
375
+ ;
376
+
377
+
378
+ parameter_type_list
379
+ : parameter_list
380
+ | parameter_list ',' ELLIPSIS
381
+ ;
382
+
383
+ parameter_list
384
+ : parameter_declaration
385
+ | parameter_list ',' parameter_declaration
386
+ ;
387
+
388
+ parameter_declaration
389
+ : declaration_specifiers declarator
390
+ | declaration_specifiers abstract_declarator
391
+ | declaration_specifiers
392
+ ;
393
+
394
+ identifier_list
395
+ : IDENTIFIER
396
+ | identifier_list ',' IDENTIFIER
397
+ ;
398
+
399
+ type_name
400
+ : specifier_qualifier_list
401
+ | specifier_qualifier_list abstract_declarator
402
+ ;
403
+
404
+ abstract_declarator
405
+ : pointer
406
+ | direct_abstract_declarator
407
+ | pointer direct_abstract_declarator
408
+ ;
409
+
410
+ direct_abstract_declarator
411
+ : '(' abstract_declarator ')'
412
+ | '[' ']'
413
+ | '[' constant_expression ']'
414
+ | direct_abstract_declarator '[' ']'
415
+ | direct_abstract_declarator '[' constant_expression ']'
416
+ | '(' ')'
417
+ | '(' parameter_type_list ')'
418
+ | direct_abstract_declarator '(' ')'
419
+ | direct_abstract_declarator '(' parameter_type_list ')'
420
+ ;
421
+
422
+ initializer
423
+ : assignment_expression
424
+ | '{' initializer_list '}'
425
+ | '{' initializer_list ',' '}'
426
+ ;
427
+
428
+ initializer_list
429
+ : initializer
430
+ | initializer_list ',' initializer
431
+ ;
432
+
433
+ statement
434
+ : labeled_statement
435
+ | compound_statement
436
+ | expression_statement
437
+ | selection_statement
438
+ | iteration_statement
439
+ | jump_statement
440
+ ;
441
+
442
+ labeled_statement
443
+ : IDENTIFIER ':' statement
444
+ | CASE constant_expression ':' statement
445
+ | DEFAULT ':' statement
446
+ ;
447
+
448
+ compound_statement
449
+ : '{' '}'
450
+ | '{' statement_list '}'
451
+ | '{' declaration_list '}'
452
+ | '{' declaration_list statement_list '}'
453
+ ;
454
+
455
+ declaration_list
456
+ : declaration
457
+ | declaration_list declaration
458
+ ;
459
+
460
+ statement_list
461
+ : statement
462
+ | statement_list statement
463
+ ;
464
+
465
+ expression_statement
466
+ : ';'
467
+ | expression ';'
468
+ ;
469
+
470
+ selection_statement
471
+ : IF '(' expression ')' statement
472
+ | IF '(' expression ')' statement ELSE statement
473
+ | SWITCH '(' expression ')' statement
474
+ ;
475
+
476
+ iteration_statement
477
+ : WHILE '(' expression ')' statement
478
+ | DO statement WHILE '(' expression ')' ';'
479
+ | FOR '(' expression_statement expression_statement ')' statement
480
+ | FOR '(' expression_statement expression_statement expression ')' statement
481
+ ;
482
+
483
+ jump_statement
484
+ : GOTO IDENTIFIER ';'
485
+ | CONTINUE ';'
486
+ | BREAK ';'
487
+ | RETURN ';'
488
+ | RETURN expression ';'
489
+ ;
490
+ #end-rule
491
+ %%
492
+ p = CParser.create_decorated_parser
493
+ p.parse(STDIN)