-
Notifications
You must be signed in to change notification settings - Fork 0
/
C99_EBNF
336 lines (249 loc) · 11.1 KB
/
C99_EBNF
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
#############################################################################
# Copyright (c) 2016 张量奇. Released under the GNU General Public license.
#############################################################################
# 6.5 Expressions
## 6.5.01 Primary expressions // 主表达式
primary_expr = ID
| constant
| string_literal
| '(' expression ')'
## 6.5.02 Postfix operators // 后缀操作符
postfix_expr = primary_expr postfix_expr_tail
| '(' type ')' '{' init_list '}' postfix_expr_tail
| '(' type ')' '{' init_list ',' '}' postfix_expr_tail
postfix_expr_tail = '[' expression ']' postfix_expr_tail
| '(' [argument_expr_list] ')' postfix_expr_tail
| '.' ID postfix_expr_tail
| '->' ID postfix_expr_tail
| '++' postfix_expr_tail
| '--' postfix_expr_tail
| empty
argument_expr_list = assignment_expr {',' assignment_expr}
## 6.5.03 Unary operators // 一元运算符
unary_expr = postfix_expr
| '++' unary_expr
| '--' unary_expr
| unary_operator cast_expr
| 'sizeof' unary_expr
| 'sizeof' '(' type ')'
unary_operator = '&' | '*' | '+' | '-' | '˜' | '!'
## 6.5.04 Cast operators // 强制类型转换
cast_expr = unary_expr
| '(' type ')' cast_expr
## 6.5.05 Multiplicative operators // 乘除模运算符
multi_expr = cast_expr multi_expr_tail
multi_expr_tail = '*' cast_expr multi_expr_tail
| '/' cast_expr multi_expr_tail
| '%' cast_expr multi_expr_tail
| empty
## 6.5.06 Additive operators // 加减运算
add_expr = multi_expr add_expr_tail
add_expr_tail = '+' multi_expr add_expr_tail
| '-' multi_expr add_expr_tail
| empty
## 6.5.07 Bitwise shift operators // 移位运算
shift_expr = add_expr shift_expr_tail
shift_expr_tail = '<<' add_expr shift_expr_tail
| '>>' add_expr shift_expr_tail
| empty
## 6.5.08 Relational operators // 比较运算
relational_expr = shift_expr relational_expr_tail
relational_expr_tail = '<' shift_expr relational_expr_tail
| '>' shift_expr relational_expr_tail
| '<=' shift_expr relational_expr_tail
| '>=' shift_expr relational_expr_tail
| empty
## 6.5.09 Equality operators // 相等运算
equal_expr = relational_expr equal_expr_tail
equal_expr_tail = '==' relational_expr equal_expr_tail
| '!=' relational_expr equal_expr_tail
| empty
## 6.5.10 Bitwise AND operator // 位&运算
bit_and_expr = equal_expr {'&' equal_expr}
## 6.5.11 Bitwise exclusive OR operator
bit_xor_expr = bit_and_expr {'ˆ' bit_and_expr}
## 6.5.12 Bitwise inclusive OR operator
bit_or_expr = bit_xor_expr {'|' bit_xor_expr}
## 6.5.13 Logical AND operator
logical_AND_expr = bit_or_expr {'&&' bit_or_expr}
## 6.5.14 Logical OR operator
logical_OR_expr = logical_AND_expr {'||' logical_AND_expr}
## 6.5.15 Conditional operator // ?:
conditional_expr = logical_OR_expr
| logical_OR_expr '?' expression ':' conditional_expr
## 6.5.16 Assignment operators
assignment_expr = conditional_expr
| unary_expr assignment_operator assignment_expr
assignment_operator = '=' | '*=' | '/=' | '%=' | '+=' | '-=' | '<<=' | '>>=' | '&=' | 'ˆ=' | '|='
## 6.5.17 Comma operator
expression = assignment_expr {',' assignment_expr}
# 6.6 Constant expressions // 常量表达式
const_expr = conditional_expr
# 6.7 Declarations // 声明
declaration = decl_specifiers [init_decl_list] ';'
decl_specifiers = storage_class_specifier [decl_specifiers]
| type_specifier [decl_specifiers]
| type_qualifier [decl_specifiers]
| function_specifier [decl_specifiers]
init_decl_list = init_declarator {',' init_declarator}
// 初始化说明符
init_declarator = declarator
| declarator '=' initializer
## 6.7.1 Storage-class specifiers //
storage_class_specifier = 'typedef' | 'extern' | 'static' | 'auto' | 'register'
## 6.7.2 Type specifiers // 类型说明符
type_specifier = 'void' | 'char' | 'short' | 'int' | 'long'
| 'float' | 'double' | 'signed' | 'unsigned'
| '_Bool' | '_Complex' | '_Imaginary'
| struct_or_union_specifier
| enum_specifier
#| typedef_name
### 6.7.2.1 Structure and union specifiers // 说明符
struct_or_union_specifier = struct_or_union [ID] '{' struct_decl_list '}'
| struct_or_union ID
struct_or_union = 'struct' | 'union'
struct_decl_list = struct_decl {struct_decl}
struct_decl = specifier_qualifier_list struct_declarator_list ';'
specifier_qualifier_list = type_specifier [specifier_qualifier_list]
| type_qualifier [specifier_qualifier_list]
struct_declarator_list = struct_declarator {',' struct_declarator}
struct_declarator = declarator | [declarator] ':' const_expr
### 6.7.2.2 Enumeration specifiers
enum_specifier = 'enum' [ID] '{' enumerator_list '}'
| 'enum' [ID] '{' enumerator_list ',' '}'
| 'enum' ID
enumerator_list = enumerator {',' enumerator}
enumerator = enumeration_constant
| enumeration_constant '=' const_expr
### 6.7.2.3 Tags
## 6.7.3 Type qualifiers
type_qualifier = 'const' | 'restrict' | 'volatile'
## 6.7.4 Function specifiers
function_specifier = 'inline'
## 6.7.5 Declarators
declarator = [pointer] direct_declarator
direct_declarator = ID direct_declarator_tail
| '(' declarator ')' direct_declarator_tail
direct_declarator_tail = '[' [type_qualifier_list] [assignment_expr] ']' direct_declarator_tail
| '[' 'static' [type_qualifier_list] assignment_expr ']' direct_declarator_tail
| '[' type_qualifier_list 'static' assignment_expr ']' direct_declarator_tail
| '[' [type_qualifier_list] '*' ']' direct_declarator_tail
| '(' param_type_list ')' direct_declarator_tail
| '(' [ID_list] ')' direct_declarator_tail
| empty
pointer = '*' [type_qualifier_list]
| '*' [type_qualifier_list] pointer
type_qualifier_list = type_qualifier {type_qualifier}
param_type_list = param_list | param_list ',' '...'
param_list = param_decl {',' param_decl}
param_decl = decl_specifiers declarator | decl_specifiers [abstract_declarator]
ID_list = ID {',' ID}
## 6.7.6 Type names
type = specifier_qualifier_list [abstract_declarator]
abstract_declarator = pointer | [pointer] direct_abstract_declarator
direct_abstract_declarator = '(' abstract_declaratorr ')' direct_abstract_declarator_tail
| '[' [assignment_expr] ']' direct_abstract_declarator_tail
| '[' '*' ']' direct_abstract_declarator_tail
| '(' [param_type_list] ')' direct_abstract_declarator_tail
direct_abstract_declarator_tail = '[' [assignment_expr] ']' direct_abstract_declarator_tail
| '[' '*' ']' direct_abstract_declarator_tail
| '(' [param_type_list] ')' direct_abstract_declarator_tail
| empty
## 6.7.7 Type definitions
typedef_name = ID
## 6.7.8 Initialization
initializer = assignment_expr
| '{' init_list [','] '}'
init_list = [designator_list '='] initializer {',' [designator_list '='] initializer}
designator_list = designator {designator}
designator = '[' const_expr ']'
| '.' ID
# 6.8 Statements and blocks
statement = labeled_stmt
| compound_stmt
| if_stmt
| switch_stmt
| for_stmt
| while_stmt
| do_while_stmt
| expr_stmt
| goto_stmt
| continue_stmt
| return_stmt
| break_stmt
| case_label_stmt
| default_label_stmt
## 6.8.1 Labeled statements
case_label_stmt = 'case' const_expr ':' statement
default_label_stmt = 'default' ':' statement
labeled_stmt = ID ':' statement
## 6.8.2 Compound statement
compound_stmt = '{' {(declaration | statement)} '}'
## 6.8.3 Expression and null statements
expr_stmt = ';' | expression ';'
## 6.8.4 Selection statements
if_stmt = 'if' '(' expression ')' statement
| 'if' '(' expression ')' statement 'else' statement
switch_stmt = 'switch' '(' expression ')' statement
## 6.8.5 Iteration statements
while_stmt = 'while' '(' expression ')' statement
do_while_stmt = 'do' statement 'while' '(' expression ')' ';'
for_stmt = 'for' '(' [expression] ';' [expression] ';' [expression] ')' statement
| 'for' '(' declaration [expression] ';' [expression] ')' statement
## 6.8.6 Jump statements
goto_stmt = 'goto' ID ';'
return_stmt = 'return' [expression] ';'
continue_stmt = 'continue' ';'
break_stmt = 'break' ';'
# 6.9 External definitions // 外部定义
translation_unit = { func_definition | declaration }
## 6.9.1 // 函数声明
func_definition = decl_specifiers declarator [decl_list] compound_stmt
decl_list = declaration {declaration}
# 6.10 Preprocessing directives // 预处理
preprocessing_file = [group]
group = group_part {group_part}
group_part = if_section | control_line | text_line | '#' non_directive
if_section = if_group [elif_groups] [else_group] endif_line
if_group = '#' 'if' const_expr new_line groupopt
| '#' 'ifdef' ID new_line groupopt
| '#' 'ifndef' ID new_line groupopt
elif_groups = elif_group {elif_group}
elif_group = '#' 'elif' const_expr new_line groupopt
else_group = '#' 'else' new_line [group]
endif_line = '#' 'endif' new_line
control_line = '#' 'include' pp_tokens new_line
| '#' 'define' ID replacement_list new_line
| '#' 'define' ID lparen [ID_list] ) replacement_list new_line
| '#' 'define' ID lparen ... ) replacement_list new_line
| '#' 'define' ID lparen ID_list , ... ) replacement_list new_line
| '#' 'undef' ID new_line
| '#' 'line' pp_tokens new_line
| '#' 'error' [pp_tokens] new_line
| '#' 'pragma' [pp_tokens] new_line
| '#' new_line
text_line = [pp_tokens] new_line
non_directive = pp_tokens new_line
lparen = a ( character not immediately preceded by white_space
replacement_list = [pp_tokens]
pp_tokens = preprocessing_token {preprocessing_token}
new_line = the new_line character
preprocessing_token = header_name
| ID
| pp_number
| character_constant
| string_literal
| punctuator
## 6.4.7 Header names
header_name = '<' h_char_sequence '>'
| '"' q_char_sequence '"'
## 6.4.8 Preprocessing numbers
pp_number = digit
| '.' digit
| pp_number digit
| pp_number identifier_nondigit
| pp_number 'e' sign
| pp_number 'E' sign
| pp_number 'p' sign
| pp_number 'P' sign
| pp_number '.'