-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpure_c_grammar.y
649 lines (564 loc) · 18.2 KB
/
pure_c_grammar.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/**
* Pure Yacc description of grammar (without semantic actions)
* of the C Programming Language (ISO/IEC 9899:2018).
*
* @authors: Denis Chernikov, Vladislav Kuleykin
*
* Source (ISO/IEC 9899:2017, no critical changes were applied in :2018):
* http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf
*
* Not for commercial use.
*/
%expect 0 // shift/reduce
%expect-rr 0 // reduce/reduce
%start TranslationUnit
// Keywords
// ISO/IEC 9899:2017, 6.4.1 Keywords, page 42
%token AUTO
%token BREAK
%token CASE
%token CHAR
%token CONST
%token CONTINUE
%token DEFAULT
%token DO
%token DOUBLE
%token ELSE
%token ENUM
%token EXTERN
%token FLOAT
%token FOR
%token GOTO
%token IF
%token INLINE
%token INT
%token LONG
%token REGISTER
%token RESTRICT
%token RETURN
%token SHORT
%token SIGNED
%token SIZEOF
%token STATIC
%token STRUCT
%token SWITCH
%token TYPEDEF
%token UNION
%token UNSIGNED
%token VOID
%token VOLATILE
%token WHILE
%token ALIGNAS
%token ALIGNOF
%token ATOMIC
%token BOOL
%token COMPLEX
%token GENERIC
%token IMAGINARY
%token NORETURN
%token STATIC_ASSERT
%token THREAD_LOCAL
/*
* NOTE:
* This nonterminal was introduced
* in order to deal with reduce/reduce
* conflict of TypedefName with PrimaryExpression.
* Lexical analyzer should make distinction
* between this token and IDENTIFIER.
*
* ISO/IEC 9899:2017, 6.7.8 Type definitions, pages 99-100
*/
%token TYPEDEF_NAME
// Literals
// ISO/IEC 9899:2017, 6.4.2 and 6.4.4, pages 43-52
%token IDENTIFIER
%token INTEGER_CONSTANT
%token FLOATING_CONSTANT
%token CHARACTER_CONSTANT
%token STRING_LITERAL
// Punctuators
// ISO/IEC 9899:2017, 6.4.6 Punctuators, page 52
%token LBRACKET
%token RBRACKET
%token LPAREN
%token RPAREN
%token LBRACE
%token RBRACE
%token DOT
%token ARROW
%token DBL_PLUS
%token DBL_MINUS
%token AMPERSAND
%token ASTERISK
%token PLUS
%token MINUS
%token TILDE
%token BANG
%token SLASH
%token PERCENT
%token LSHIFT
%token RSHIFT
%token LS
%token GR
%token LE
%token GE
%token EQ
%token NE
%token CARET
%token VERTICAL
%token LOG_AND
%token LOG_OR
%token QUESTION
%token COLON
%token SEMICOLON
%token ELLIPSIS
%token ASSIGN
%token MUL_ASSIGN
%token DIV_ASSIGN
%token MOD_ASSIGN
%token ADD_ASSIGN
%token SUB_ASSIGN
%token LEFT_ASSIGN
%token RIGHT_ASSIGN
%token AND_ASSIGN
%token XOR_ASSIGN
%token OR_ASSIGN
%token COMMA
// *** PRECEDENCE ASSIGNMENT ***
// Lower precedence
// _Atomic type shift/reduce resolution
// ISO/IEC 9899:2017, 6.7.2.4 Atomic type specifiers (Semantics), page 87
%nonassoc ATOMIC
%nonassoc LPAREN
// "Dangling Else" shift/reduce resolution
// ISO/IEC 9899:2017, 6.8.4.1 The if statement (Semantics), page 108
%nonassoc NO_ELSE // Fake token for precedence
%nonassoc ELSE
// Expression precedence
// ISO/IEC 9899:2017, 6.5.5-6.5.14 Expressions, pages 66-71
%left LOG_OR // Logical OR
%left LOG_AND // Logical AND
%left VERTICAL // Inclusive OR
%left CARET // Exclusive OR
%left AMPERSAND // AND
%left EQ NE // Equality
%left LS GR LE GE // Relational
%left LSHIFT RSHIFT // Shift
%left PLUS MINUS // Additive
%left ASTERISK SLASH PERCENT // Multiplicative
// Higher precedence
%%
// ISO/IEC 9899:2017, 6.9 External definitions, pages 113-116
TranslationUnit
: ExternalDeclaration
| TranslationUnit ExternalDeclaration
;
ExternalDeclaration
: FunctionDefinition
| Declaration
;
FunctionDefinition
: DeclarationSpecifiers Declarator CompoundStatement
| DeclarationSpecifiers Declarator DeclarationList CompoundStatement
;
DeclarationList
: Declaration
| DeclarationList Declaration
;
// ISO/IEC 9899:2017, 6.7 Declarations, pages 78-105
Declaration
: DeclarationSpecifiers SEMICOLON
| DeclarationSpecifiers InitDeclaratorList SEMICOLON
| StaticAssertDeclaration
;
DeclarationSpecifiers
: StorageClassSpecifier
| TypeSpecifier
| TypeQualifier
| FunctionSpecifier
| AlignmentSpecifier
| StorageClassSpecifier DeclarationSpecifiers
| TypeSpecifier DeclarationSpecifiers
| TypeQualifier DeclarationSpecifiers
| FunctionSpecifier DeclarationSpecifiers
| AlignmentSpecifier DeclarationSpecifiers
;
InitDeclaratorList
: InitDeclarator
| InitDeclaratorList COMMA InitDeclarator
;
InitDeclarator
: Declarator
| Declarator ASSIGN Initializer
;
StorageClassSpecifier
: TYPEDEF
| EXTERN
| STATIC
| THREAD_LOCAL
| AUTO
| REGISTER
;
TypeSpecifier
: VOID
| CHAR
| SHORT
| INT
| LONG
| FLOAT
| DOUBLE
| SIGNED
| UNSIGNED
| BOOL
| COMPLEX
| IMAGINARY
| AtomicTypeSpecifier
| StructOrUnionSpecifier
| EnumSpecifier
| TypedefName
;
StructOrUnionSpecifier
: StructOrUnion LBRACE StructDeclarationList RBRACE
| StructOrUnion IDENTIFIER LBRACE StructDeclarationList RBRACE
| StructOrUnion IDENTIFIER
;
StructOrUnion
: STRUCT
| UNION
;
StructDeclarationList
: StructDeclaration
| StructDeclarationList StructDeclaration
;
StructDeclaration
: SpecifierQualifierList SEMICOLON
| SpecifierQualifierList StructDeclaratorList SEMICOLON
| StaticAssertDeclaration
;
SpecifierQualifierList
: TypeSpecifier
| TypeQualifier
| AlignmentSpecifier
| TypeSpecifier SpecifierQualifierList
| TypeQualifier SpecifierQualifierList
| AlignmentSpecifier SpecifierQualifierList
;
StructDeclaratorList
: StructDeclarator
| StructDeclaratorList COMMA StructDeclarator
;
StructDeclarator
: Declarator
| COLON ConstantExpression
| Declarator COLON ConstantExpression
;
EnumSpecifier
: ENUM LBRACE EnumeratorList RBRACE
| ENUM LBRACE EnumeratorList COMMA RBRACE
| ENUM IDENTIFIER LBRACE EnumeratorList RBRACE
| ENUM IDENTIFIER LBRACE EnumeratorList COMMA RBRACE
| ENUM IDENTIFIER
;
EnumeratorList
: Enumerator
| EnumeratorList COMMA Enumerator
;
Enumerator
: IDENTIFIER // EnumerationConstant, reduce/reduce with PrimaryExpression
| IDENTIFIER ASSIGN ConstantExpression
;
AtomicTypeSpecifier
: ATOMIC LPAREN TypeName RPAREN
;
TypeQualifier
: CONST
| RESTRICT
| VOLATILE
| ATOMIC
;
FunctionSpecifier
: INLINE
| NORETURN
;
AlignmentSpecifier
: ALIGNAS LPAREN TypeName RPAREN
| ALIGNAS LPAREN ConstantExpression RPAREN
;
Declarator
: DirectDeclarator
| Pointer DirectDeclarator
;
DirectDeclarator
: IDENTIFIER
| LPAREN Declarator RPAREN
| DirectDeclarator LBRACKET RBRACKET
| DirectDeclarator LBRACKET AssignmentExpression RBRACKET
| DirectDeclarator LBRACKET TypeQualifierList RBRACKET
| DirectDeclarator LBRACKET TypeQualifierList AssignmentExpression RBRACKET
| DirectDeclarator LBRACKET STATIC AssignmentExpression RBRACKET
| DirectDeclarator LBRACKET STATIC TypeQualifierList AssignmentExpression RBRACKET
| DirectDeclarator LBRACKET TypeQualifierList STATIC AssignmentExpression RBRACKET
| DirectDeclarator LBRACKET ASTERISK RBRACKET
| DirectDeclarator LBRACKET TypeQualifierList ASTERISK RBRACKET
| DirectDeclarator LPAREN ParameterTypeList RPAREN
| DirectDeclarator LPAREN RPAREN
| DirectDeclarator LPAREN IdentifierList RPAREN
;
Pointer
: ASTERISK
| ASTERISK TypeQualifierList
| ASTERISK Pointer
| ASTERISK TypeQualifierList Pointer
;
TypeQualifierList
: TypeQualifier
| TypeQualifierList TypeQualifier
;
ParameterTypeList
: ParameterList
| ParameterList COMMA ELLIPSIS
;
ParameterList
: ParameterDeclaration
| ParameterList COMMA ParameterDeclaration
;
ParameterDeclaration
: DeclarationSpecifiers Declarator
| DeclarationSpecifiers
| DeclarationSpecifiers AbstractDeclarator
;
IdentifierList
: IDENTIFIER
| IdentifierList COMMA IDENTIFIER
;
TypeName
: SpecifierQualifierList
| SpecifierQualifierList AbstractDeclarator
;
AbstractDeclarator
: Pointer
| DirectAbstractDeclarator
| Pointer DirectAbstractDeclarator
;
DirectAbstractDeclarator
: LPAREN AbstractDeclarator RPAREN
| LBRACKET RBRACKET
| LBRACKET AssignmentExpression RBRACKET
| LBRACKET TypeQualifierList RBRACKET
| LBRACKET TypeQualifierList AssignmentExpression RBRACKET
| LBRACKET STATIC AssignmentExpression RBRACKET
| LBRACKET STATIC TypeQualifierList AssignmentExpression RBRACKET
| LBRACKET TypeQualifierList STATIC AssignmentExpression RBRACKET
| LBRACKET ASTERISK RBRACKET
| LPAREN RPAREN
| LPAREN ParameterTypeList RPAREN
| DirectAbstractDeclarator LBRACKET RBRACKET
| DirectAbstractDeclarator LBRACKET AssignmentExpression RBRACKET
| DirectAbstractDeclarator LBRACKET TypeQualifierList RBRACKET
| DirectAbstractDeclarator LBRACKET TypeQualifierList AssignmentExpression RBRACKET
| DirectAbstractDeclarator LBRACKET STATIC AssignmentExpression RBRACKET
| DirectAbstractDeclarator LBRACKET STATIC TypeQualifierList AssignmentExpression RBRACKET
| DirectAbstractDeclarator LBRACKET TypeQualifierList STATIC AssignmentExpression RBRACKET
| DirectAbstractDeclarator LBRACKET ASTERISK RBRACKET
| DirectAbstractDeclarator LPAREN RPAREN
| DirectAbstractDeclarator LPAREN ParameterTypeList RPAREN
;
TypedefName
: TYPEDEF_NAME
// | IDENTIFIER // reduce/reduce with PrimaryExpression, resolved using lexical analyzer
;
Initializer
: AssignmentExpression
| LBRACE InitializerList RBRACE
| LBRACE InitializerList COMMA RBRACE
;
InitializerList
: Initializer
| Designation Initializer
| InitializerList COMMA Initializer
| InitializerList COMMA Designation Initializer
;
Designation
: DesignatorList ASSIGN
;
DesignatorList
: Designator
| DesignatorList Designator
;
Designator
: LBRACKET ConstantExpression RBRACKET
| DOT IDENTIFIER
;
StaticAssertDeclaration
: STATIC_ASSERT LPAREN ConstantExpression COMMA STRING_LITERAL RPAREN SEMICOLON
;
// ISO/IEC 9899:2017, 6.8 Statements, page 106-112
Statement
: LabeledStatement
| CompoundStatement
| ExpressionStatement
| SelectionStatement
| IterationStatement
| JumpStatement
;
LabeledStatement
: IDENTIFIER COLON Statement
| CASE ConstantExpression COLON Statement
| DEFAULT COLON Statement
;
CompoundStatement
: LBRACE RBRACE
| LBRACE BlockItemList RBRACE
;
BlockItemList
: BlockItem
| BlockItemList BlockItem
;
BlockItem
: Declaration
| Statement
;
ExpressionStatement
: SEMICOLON
| Expression SEMICOLON
;
SelectionStatement
: IF LPAREN Expression RPAREN Statement %prec NO_ELSE
| IF LPAREN Expression RPAREN Statement ELSE Statement
| SWITCH LPAREN Expression RPAREN Statement
;
IterationStatement
: WHILE LPAREN Expression RPAREN Statement
| DO Statement WHILE LPAREN Expression RPAREN SEMICOLON
| FOR LPAREN SEMICOLON SEMICOLON RPAREN Statement
| FOR LPAREN SEMICOLON SEMICOLON Expression RPAREN Statement
| FOR LPAREN SEMICOLON Expression SEMICOLON RPAREN Statement
| FOR LPAREN SEMICOLON Expression SEMICOLON Expression RPAREN Statement
| FOR LPAREN Expression SEMICOLON SEMICOLON RPAREN Statement
| FOR LPAREN Expression SEMICOLON SEMICOLON Expression RPAREN Statement
| FOR LPAREN Expression SEMICOLON Expression SEMICOLON RPAREN Statement
| FOR LPAREN Expression SEMICOLON Expression SEMICOLON Expression RPAREN Statement
| FOR LPAREN Declaration SEMICOLON RPAREN Statement
| FOR LPAREN Declaration SEMICOLON Expression RPAREN Statement
| FOR LPAREN Declaration Expression SEMICOLON RPAREN Statement
| FOR LPAREN Declaration Expression SEMICOLON Expression RPAREN Statement
;
JumpStatement
: GOTO IDENTIFIER SEMICOLON
| CONTINUE SEMICOLON
| BREAK SEMICOLON
| RETURN SEMICOLON
| RETURN Expression SEMICOLON
;
// ISO/IEC 9899:2017, 6.6 Constant expressions, page 76-77
ConstantExpression
: ConditionalExpression
;
// ISO/IEC 9899:2017, 6.5 Expressions, page 55-75
Expression
: AssignmentExpression
| Expression COMMA AssignmentExpression
;
AssignmentExpression
: ConditionalExpression
| UnaryExpression AssignmentOperator AssignmentExpression
;
AssignmentOperator
: ASSIGN
| MUL_ASSIGN
| DIV_ASSIGN
| MOD_ASSIGN
| ADD_ASSIGN
| SUB_ASSIGN
| LEFT_ASSIGN
| RIGHT_ASSIGN
| AND_ASSIGN
| XOR_ASSIGN
| OR_ASSIGN
;
ConditionalExpression
: ArithmeticalExpression
| ArithmeticalExpression QUESTION Expression COLON ConditionalExpression
;
ArithmeticalExpression
: CastExpression
| ArithmeticalExpression LOG_OR ArithmeticalExpression
| ArithmeticalExpression LOG_AND ArithmeticalExpression
| ArithmeticalExpression VERTICAL ArithmeticalExpression
| ArithmeticalExpression CARET ArithmeticalExpression
| ArithmeticalExpression AMPERSAND ArithmeticalExpression
| ArithmeticalExpression EQ ArithmeticalExpression
| ArithmeticalExpression NE ArithmeticalExpression
| ArithmeticalExpression LS ArithmeticalExpression
| ArithmeticalExpression GR ArithmeticalExpression
| ArithmeticalExpression LE ArithmeticalExpression
| ArithmeticalExpression GE ArithmeticalExpression
| ArithmeticalExpression LSHIFT ArithmeticalExpression
| ArithmeticalExpression RSHIFT ArithmeticalExpression
| ArithmeticalExpression PLUS ArithmeticalExpression
| ArithmeticalExpression MINUS ArithmeticalExpression
| ArithmeticalExpression ASTERISK ArithmeticalExpression
| ArithmeticalExpression SLASH ArithmeticalExpression
| ArithmeticalExpression PERCENT ArithmeticalExpression
;
CastExpression
: UnaryExpression
| LPAREN TypeName RPAREN CastExpression
;
UnaryExpression
: PostfixExpression
| DBL_PLUS UnaryExpression
| DBL_MINUS UnaryExpression
| UnaryOperator CastExpression
| SIZEOF UnaryExpression
| SIZEOF LPAREN TypeName RPAREN
| ALIGNOF LPAREN TypeName RPAREN
;
UnaryOperator
: AMPERSAND
| ASTERISK
| PLUS
| MINUS
| TILDE
| BANG
;
PostfixExpression
: PrimaryExpression
| PostfixExpression LBRACKET Expression RBRACKET
| PostfixExpression LPAREN RPAREN
| PostfixExpression LPAREN ArgumentExpressionList RPAREN
| PostfixExpression DOT IDENTIFIER
| PostfixExpression ARROW IDENTIFIER
| PostfixExpression DBL_PLUS
| PostfixExpression DBL_MINUS
| LPAREN TypeName RPAREN LBRACE InitializerList RBRACE
| LPAREN TypeName RPAREN LBRACE InitializerList COMMA RBRACE
;
ArgumentExpressionList
: AssignmentExpression
| ArgumentExpressionList COMMA AssignmentExpression
;
PrimaryExpression
: IDENTIFIER
| Constant
| STRING_LITERAL
| LPAREN Expression RPAREN
| GenericSelection
;
Constant
: INTEGER_CONSTANT
| FLOATING_CONSTANT
// | EnumerationConstant // reduce/reduce with PrimaryExpression, identical to IDENTIFIER
| CHARACTER_CONSTANT
;
GenericSelection
: GENERIC LPAREN AssignmentExpression COMMA GenericAssocList RPAREN
;
GenericAssocList
: GenericAssociation
| GenericAssocList COMMA GenericAssociation
;
GenericAssociation
: TypeName COLON AssignmentExpression
| DEFAULT COLON AssignmentExpression
;