~ubuntu-branches/ubuntu/trusty/horae/trusty

« back to all changes in this revision

Viewing changes to 0CPAN/Parse-RecDescent-1.94/demo/demo_Cgrammar.pl

  • Committer: Bazaar Package Importer
  • Author(s): Carlo Segre
  • Date: 2008-02-23 23:13:02 UTC
  • mfrom: (2.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080223231302-mnyyxs3icvrus4ke
Tags: 066-3
Apply patch to athena_parts/misc.pl for compatibility with 
perl-tk 804.28.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/local/bin/perl -w
2
 
 
3
 
# The full monty
4
 
 
5
 
use Parse::RecDescent;
6
 
 
7
 
local $/;
8
 
my $grammar = <DATA>;
9
 
my $parser = Parse::RecDescent->new($grammar);
10
 
 
11
 
my $text = <>;
12
 
 
13
 
$tree = $parser->translation_unit($text) or die "bad C code";
14
 
 
15
 
use Data::Dumper 'Dumper';
16
 
print Dumper [ $tree ];
17
 
 
18
 
__DATA__
19
 
 
20
 
<autotree>
21
 
 
22
 
primary_expression:
23
 
          IDENTIFIER
24
 
        | CONSTANT
25
 
        | STRING_LITERAL
26
 
        | '(' expression ')'
27
 
 
28
 
postfix_expression:
29
 
          primary_expression
30
 
        | (primary_expression)(s) '[' expression ']'
31
 
        | (primary_expression)(s) '(' ')'
32
 
        | (primary_expression)(s) '(' argument_expression_list ')'
33
 
        | (primary_expression)(s) '.' IDENTIFIER
34
 
        | (primary_expression)(s) PTR_OP IDENTIFIER
35
 
        | (primary_expression)(s) INC_OP
36
 
        | (primary_expression)(s) DEC_OP
37
 
 
38
 
argument_expression_list:
39
 
          (assignment_expression ',')(s?) assignment_expression
40
 
 
41
 
unary_expression:
42
 
          postfix_expression
43
 
        | INC_OP unary_expression
44
 
        | DEC_OP unary_expression
45
 
        | unary_operator cast_expression
46
 
        | SIZEOF unary_expression
47
 
        | SIZEOF '(' type_name ')'
48
 
 
49
 
unary_operator:
50
 
          '&'
51
 
        | '*'
52
 
        | '+'
53
 
        | '-'
54
 
        | '~'
55
 
        | '!'
56
 
 
57
 
cast_expression:
58
 
          unary_expression
59
 
        | '(' type_name ')' cast_expression
60
 
 
61
 
multiplicative_expression:
62
 
          (cast_expression mul_ex_op)(s?) cast_expression
63
 
 
64
 
mul_ex_op : '*' | '/' | '%' 
65
 
 
66
 
additive_expression:
67
 
          (multiplicative_expression add_op)(s?) multiplicative_expression
68
 
 
69
 
add_op : '+' | '-' 
70
 
 
71
 
shift_expression:
72
 
          (additive_expression shift_op )(s?) additive_expression
73
 
 
74
 
shift_op : LEFT_OP | RIGHT_OP 
75
 
 
76
 
relational_expression:
77
 
          (shift_expression rel_op)(s?) shift_expression
78
 
 
79
 
rel_op: '<' | '>' | LE_OP | GE_OP
80
 
 
81
 
equality_expression:
82
 
          (relational_expression eq_ex_op)(s?) relational_expression
83
 
 
84
 
eq_ex_op : EQ_OP | NE_OP 
85
 
 
86
 
and_expression:
87
 
          (equality_expression '&')(s?) equality_expression
88
 
 
89
 
exclusive_or_expression:
90
 
          (and_expression '^')(s?) and_expression
91
 
 
92
 
inclusive_or_expression:
93
 
          (exclusive_or_expression '|')(s?) exclusive_or_expression
94
 
 
95
 
logical_and_expression:
96
 
          (inclusive_or_expression AND_OP)(s?) inclusive_or_expression
97
 
 
98
 
logical_or_expression:
99
 
          (logical_and_expression OR_OP)(s?) logical_and_expression
100
 
 
101
 
conditional_expression:
102
 
          logical_or_expression
103
 
        | logical_or_expression '?' expression ':' conditional_expression
104
 
 
105
 
assignment_expression:
106
 
          conditional_expression
107
 
        | unary_expression assignment_operator assignment_expression
108
 
 
109
 
assignment_operator:
110
 
          '='
111
 
        | MUL_ASSIGN
112
 
        | DIV_ASSIGN
113
 
        | MOD_ASSIGN
114
 
        | ADD_ASSIGN
115
 
        | SUB_ASSIGN
116
 
        | LEFT_ASSIGN
117
 
        | RIGHT_ASSIGN
118
 
        | AND_ASSIGN
119
 
        | XOR_ASSIGN
120
 
        | OR_ASSIGN
121
 
 
122
 
expression:
123
 
          (assignment_expression ',')(s?) assignment_expression
124
 
 
125
 
constant_expression:
126
 
          conditional_expression
127
 
 
128
 
declaration:
129
 
          declaration_specifiers ';'
130
 
          { print "We have a match!\n"; }
131
 
        | declaration_specifiers init_declarator_list ';'
132
 
 
133
 
declaration_specifiers:
134
 
          storage_class_specifier
135
 
        | storage_class_specifier declaration_specifiers
136
 
        | type_specifier
137
 
        | type_specifier declaration_specifiers
138
 
        | type_qualifier
139
 
        | type_qualifier declaration_specifiers
140
 
 
141
 
init_declarator_list:
142
 
          (init_declarator ',')(s?) init_declarator
143
 
 
144
 
init_declarator:
145
 
          declarator
146
 
        | declarator '=' initializer
147
 
 
148
 
storage_class_specifier:
149
 
          TYPEDEF
150
 
        | EXTERN
151
 
        | STATIC
152
 
        | AUTO
153
 
        | REGISTER
154
 
 
155
 
type_specifier:
156
 
          VOID
157
 
        | CHAR
158
 
        | SHORT
159
 
        | INT
160
 
        | LONG
161
 
        | FLOAT
162
 
        | DOUBLE
163
 
        | SIGNED
164
 
        | UNSIGNED
165
 
        | struct_or_union_specifier
166
 
        | enum_specifier
167
 
        | TYPE_NAME
168
 
 
169
 
struct_or_union_specifier:
170
 
          struct_or_union IDENTIFIER '{' struct_declaration_list '}'
171
 
        | struct_or_union '{' struct_declaration_list '}'
172
 
        | struct_or_union IDENTIFIER
173
 
 
174
 
struct_or_union:
175
 
          STRUCT
176
 
        | UNION
177
 
 
178
 
struct_declaration_list:
179
 
          struct_declaration(s)
180
 
 
181
 
struct_declaration:
182
 
          specifier_qualifier_list struct_declarator_list ';'
183
 
 
184
 
specifier_qualifier_list:
185
 
          type_specifier specifier_qualifier_list
186
 
        | type_specifier
187
 
        | type_qualifier specifier_qualifier_list
188
 
        | type_qualifier
189
 
 
190
 
struct_declarator_list:
191
 
          (struct_declarator ',')(s?) struct_declarator
192
 
 
193
 
struct_declarator:
194
 
          declarator
195
 
        | ':' constant_expression
196
 
        | declarator ':' constant_expression
197
 
 
198
 
enum_specifier:
199
 
          ENUM '{' enumerator_list '}'
200
 
        | ENUM IDENTIFIER '{' enumerator_list '}'
201
 
        | ENUM IDENTIFIER
202
 
 
203
 
enumerator_list:
204
 
          (enumerator ',')(s?) enumerator
205
 
 
206
 
enumerator:
207
 
          IDENTIFIER
208
 
        | IDENTIFIER '=' constant_expression
209
 
 
210
 
type_qualifier:
211
 
          CONST
212
 
        | VOLATILE
213
 
 
214
 
declarator:
215
 
          pointer direct_declarator
216
 
        | direct_declarator
217
 
 
218
 
direct_declarator:
219
 
          IDENTIFIER
220
 
        | '(' declarator ')'
221
 
        | (IDENTIFIER)(s?) ('(' declarator ')')(s?) '[' constant_expression ']'
222
 
        | (IDENTIFIER)(s?) ('(' declarator ')')(s?) '[' ']'
223
 
        | (IDENTIFIER)(s?) ('(' declarator ')')(s?) '(' parameter_type_list ')'
224
 
        | (IDENTIFIER)(s?) ('(' declarator ')')(s?) '(' identifier_list ')'
225
 
        | (IDENTIFIER)(s?) ('(' declarator ')')(s?) '(' ')'
226
 
 
227
 
pointer:
228
 
          '*'
229
 
        | '*' type_qualifier_list
230
 
        | '*' pointer
231
 
        | '*' type_qualifier_list pointer
232
 
 
233
 
type_qualifier_list:
234
 
          type_qualifier(s)
235
 
 
236
 
parameter_type_list:
237
 
          parameter_list
238
 
        | parameter_list ',' ELLIPSIS
239
 
 
240
 
parameter_list:
241
 
          (parameter_declaration ',')(s?) parameter_declaration
242
 
 
243
 
parameter_declaration:
244
 
          declaration_specifiers declarator
245
 
        | declaration_specifiers abstract_declarator
246
 
        | declaration_specifiers
247
 
 
248
 
identifier_list:
249
 
          (IDENTIFIER ',')(s?) IDENTIFIER
250
 
 
251
 
type_name:
252
 
          specifier_qualifier_list
253
 
        | specifier_qualifier_list abstract_declarator
254
 
 
255
 
abstract_declarator:
256
 
          pointer
257
 
        | direct_abstract_declarator
258
 
        | pointer direct_abstract_declarator
259
 
 
260
 
direct_abstract_declarator:
261
 
          '(' abstract_declarator ')'
262
 
        | '[' ']'
263
 
        | '[' constant_expression ']'
264
 
        | DAD '[' ']'
265
 
        | DAD '[' constant_expression ']'
266
 
        | '(' ')'
267
 
        | '(' parameter_type_list ')'
268
 
        | DAD '(' ')'
269
 
        | DAD '(' parameter_type_list ')'
270
 
 
271
 
DAD:    #macro for direct_abstract_declarator 
272
 
          ( '(' abstract_declarator ')' )(s?)
273
 
          ( '[' ']' )(s?)
274
 
          ( '[' constant_expression ']' )(s?)
275
 
          ( '(' ')' )(s?)
276
 
          ( '(' parameter_type_list ')' )(s?)
277
 
 
278
 
initializer:
279
 
          assignment_expression
280
 
        | '{' initializer_list '}'
281
 
        | '{' initializer_list ',' '}'
282
 
 
283
 
initializer_list:
284
 
          (initializer ',')(s?) initializer
285
 
 
286
 
statement:
287
 
          labeled_statement
288
 
        | compound_statement
289
 
        | expression_statement
290
 
        | selection_statement
291
 
        | iteration_statement
292
 
        | jump_statement
293
 
 
294
 
labeled_statement:
295
 
          IDENTIFIER ':' statement
296
 
        | CASE constant_expression ':' statement
297
 
        | DEFAULT ':' statement
298
 
 
299
 
compound_statement:
300
 
          '{' '}'
301
 
        | '{' statement_list '}'
302
 
        | '{' declaration_list '}'
303
 
        | '{' declaration_list statement_list '}'
304
 
 
305
 
declaration_list:
306
 
          declaration(s)
307
 
 
308
 
statement_list:
309
 
          statement(s)
310
 
 
311
 
expression_statement:
312
 
          ';'
313
 
        | expression ';'
314
 
 
315
 
selection_statement:
316
 
          IF '(' expression ')' statement
317
 
        | IF '(' expression ')' statement ELSE statement
318
 
        | SWITCH '(' expression ')' statement
319
 
 
320
 
iteration_statement:
321
 
          WHILE '(' expression ')' statement
322
 
        | DO statement WHILE '(' expression ')' ';'
323
 
        | FOR '(' expression_statement expression_statement ')' statement
324
 
        | FOR '(' expression_statement expression_statement expression ')' statement
325
 
 
326
 
jump_statement:
327
 
          GOTO IDENTIFIER ';'
328
 
        | CONTINUE ';'
329
 
        | BREAK ';'
330
 
        | RETURN ';'
331
 
        | RETURN expression ';'
332
 
 
333
 
translation_unit:
334
 
          external_declaration(s)
335
 
 
336
 
external_declaration:
337
 
          function_definition
338
 
        | declaration
339
 
 
340
 
function_definition:
341
 
          declaration_specifiers declarator declaration_list compound_statement
342
 
        | declaration_specifiers declarator compound_statement
343
 
        | declarator declaration_list compound_statement
344
 
        | declarator compound_statement
345
 
 
346
 
# TERMINALS
347
 
 
348
 
reserved_word:
349
 
        AUTO     | BREAK   | CASE     | CHAR   | CONST    |
350
 
        CONTINUE | DEFAULT | DO       | DOUBLE | ENUM     |
351
 
        EXTERN   | FLOAT   | FOR      | GOTO   | IF       |
352
 
        INT      | LONG    | REGISTER | RETURN | SHORT    | 
353
 
        SIGNED   | SIZEOF  | STATIC   | STRUCT | SWITCH   |
354
 
        TYPEDEF  | UNION   | UNSIGNED | VOID   | VOLATILE |
355
 
        WHILE
356
 
 
357
 
 
358
 
ADD_ASSIGN:     '+='
359
 
AND_ASSIGN:     '&='
360
 
AND_OP:         '&&'
361
 
AUTO:           'auto'
362
 
BREAK:          'break'
363
 
CASE:           'case'
364
 
CHAR:           'char'
365
 
CONST:          'const'
366
 
CONSTANT:       /[+-]?(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?/
367
 
CONTINUE:       'continue'
368
 
DEC_OP:         '--'
369
 
DEFAULT:        'default'
370
 
DIV_ASSIGN:     '/='
371
 
DO:             'do'
372
 
DOUBLE:         'double'
373
 
ELLIPSIS:       '...'
374
 
ELSE:           'else'
375
 
ENUM:           'enum'
376
 
EQ_OP:          '=='
377
 
EXTERN:         'extern'
378
 
FLOAT:          'float'
379
 
FOR:            'for'
380
 
GE_OP:          '>='
381
 
GOTO:           'goto'
382
 
IDENTIFIER:     ...!reserved_word /[a-z]\w*/i
383
 
IF:             'if'
384
 
INC_OP:         '++'
385
 
INT:            'int'
386
 
LEFT_ASSIGN:    '<<='
387
 
LEFT_OP:        '<<'
388
 
LE_OP:          '<='
389
 
LONG:           'long'
390
 
MOD_ASSIGN:     '%='
391
 
MUL_ASSIGN:     '*='
392
 
NE_OP:          '!='
393
 
OR_ASSIGN:      '|='
394
 
OR_OP:          '||'
395
 
PTR_OP:         '->'
396
 
REGISTER:       'register'
397
 
RETURN:         'return'
398
 
RIGHT_ASSIGN:   '>>='
399
 
RIGHT_OP:       '>>'
400
 
SHORT:          'short'
401
 
SIGNED:         'signed'
402
 
SIZEOF:         'sizeof'
403
 
STATIC:         'static'
404
 
STRING_LITERAL: { extract_delimited($text,'"') }
405
 
STRUCT:         'struct'
406
 
SUB_ASSIGN:     '-='
407
 
SWITCH:         'switch'
408
 
TYPEDEF:        'typedef'
409
 
TYPE_NAME:      # NONE YET
410
 
UNION:          'union'
411
 
UNSIGNED:       'unsigned'
412
 
VOID:           'void'
413
 
VOLATILE:       'volatile'
414
 
WHILE:          'while'
415
 
XOR_ASSIGN:     '^='