2 (* src: ocamlyaccified from
3 * http://www.lysator.liu.se/c/ANSI-C-grammar-y.html
7 exception Parsing of string
10 %token <string * AbstractSyntax.fullType> TString
11 %token <string> TIdent
12 %token <int * AbstractSyntax.intType> TInt
13 %token <float * AbstractSyntax.floatType> TFloat
16 %token <string> TypedefIdent
18 %token TOPar TCPar TOBrace TCBrace TOCro TCCro
19 %token TDot TComma TPtrOp
21 %token <AbstractSyntax.assignOp> TAssign
23 %token TWhy TDotDot TPtVirg TTilde TBang
26 %token TOrLog TAndLog TOrIncl TOrExcl TAnd TEqEq TNotEq TInf TSup TInfEq TSupEq TShl TShr
27 TPlus TMinus TMul TDiv TMod TMin TMax
29 %token Tchar Tshort Tint Tdouble Tfloat Tlong Tunsigned Tsigned Tvoid
30 Tauto Tregister Textern Tstatic
32 Tstruct Tenum Ttypedef Tunion
33 Tbreak Telse Tswitch Tcase Tcontinue Tfor Tdo Tif Twhile Treturn Tgoto Tdefault
45 %left TInf TSup TInfEq TSupEq
48 %left TMul TDiv TMod TMin TMax
54 main: translation_unit EOF { [] }
56 /********************************************************************************/
64 /********************************************************************************/
67 | expr TComma assign_expr { }
69 assign_expr: cond_expr { }
70 | unary_expr TAssign assign_expr { }
71 | unary_expr TEq assign_expr { }
73 cond_expr: arith_expr {}
74 | arith_expr TWhy expr TDotDot cond_expr {}
76 arith_expr: cast_expr {}
77 | arith_expr TMul arith_expr {}
78 | arith_expr TDiv arith_expr {}
79 | arith_expr TMin arith_expr {}
80 | arith_expr TMax arith_expr {}
81 | arith_expr TMod arith_expr {}
82 | arith_expr TPlus arith_expr {}
83 | arith_expr TMinus arith_expr {}
84 | arith_expr TShl arith_expr {}
85 | arith_expr TShr arith_expr {}
86 | arith_expr TInf arith_expr {}
87 | arith_expr TSup arith_expr {}
88 | arith_expr TInfEq arith_expr {}
89 | arith_expr TSupEq arith_expr {}
90 | arith_expr TEqEq arith_expr {}
91 | arith_expr TNotEq arith_expr {}
92 | arith_expr TAnd arith_expr {}
93 | arith_expr TOrExcl arith_expr {}
94 | arith_expr TOrIncl arith_expr {}
95 | arith_expr TAndLog arith_expr {}
96 | arith_expr TOrLog arith_expr {}
98 cast_expr: unary_expr {}
99 | TOPar type_name TCPar cast_expr {}
101 unary_expr: postfix_expr {}
104 | unary_op cast_expr {}
105 | Tsizeof unary_expr {}
106 | Tsizeof TOPar type_name TCPar {}
115 postfix_expr: primary_expr {}
116 | postfix_expr TOCro expr TCCro {}
117 | postfix_expr TOPar argument_expr_list TCPar {}
118 | postfix_expr TOPar TCPar {}
119 | postfix_expr TDot TIdent {}
120 | postfix_expr TPtrOp TIdent {}
121 | postfix_expr TInc {}
122 | postfix_expr TDec {}
124 argument_expr_list: assign_expr { }
125 | argument_expr_list TComma assign_expr {}
127 primary_expr: TIdent {}
131 | TOPar expr TCPar {}
133 const_expr: cond_expr {}
134 /********************************************************************************/
136 statement: labeled {}
143 labeled: TIdent TDotDot statement {}
144 | Tcase const_expr TDotDot statement {}
145 | Tdefault TDotDot statement {}
147 compound: TOBrace TCBrace {}
148 | TOBrace statement_list TCBrace {}
149 | TOBrace decl_list TCBrace {}
150 | TOBrace decl_list statement_list TCBrace {}
155 statement_list: statement {}
156 | statement statement_list {}
158 expr_statement: TPtVirg {}
161 selection: Tif TOPar expr TCPar statement {}
162 | Tif TOPar expr TCPar statement Telse statement {}
163 | Tswitch TOPar expr TCPar statement {}
165 iteration: Twhile TOPar expr TCPar statement {}
166 | Tdo statement Twhile TOPar expr TCPar TPtVirg {}
167 | Tfor TOPar expr_statement expr_statement TCPar statement {}
168 | Tfor TOPar expr_statement expr_statement expr TCPar statement {}
170 jump: Tgoto TIdent {}
176 /********************************************************************************/
178 /*------------------------------------------------------------------------------*/
179 decl: decl_spec TPtVirg {}
180 | decl_spec init_declarator_list TPtVirg {}
182 /*------------------------------------------------------------------------------*/
183 decl_spec: storage_class_spec {}
184 | storage_class_spec decl_spec {}
186 | type_spec decl_spec {}
188 | type_qualif decl_spec {}
190 storage_class_spec: Tstatic {}
204 | struct_or_union_spec {}
206 /*TODO | TIdent {} */
209 type_qualif: Tconst {}
212 /*------------------------------------------------------------------------------*/
213 struct_or_union_spec: struct_or_union TIdent TOBrace struct_decl_list TCBrace {}
214 | struct_or_union TOBrace struct_decl_list TCBrace {}
215 | struct_or_union TIdent {}
217 struct_or_union: Tstruct {}
220 struct_decl_list: struct_decl {}
221 | struct_decl_list struct_decl {}
223 struct_decl: spec_qualif_list struct_declarator_list TPtVirg {}
225 spec_qualif_list: type_spec {}
226 | type_spec spec_qualif_list {}
228 | type_qualif spec_qualif_list {}
230 struct_declarator_list: struct_declarator {}
231 | struct_declarator_list TComma struct_declarator {}
232 struct_declarator: declarator {}
233 | TDotDot const_expr {}
234 | declarator TDotDot const_expr {}
235 /*------------------------------------------------------------------------------*/
236 enum_spec: Tenum TOBrace enumerator_list TCBrace {}
237 | Tenum TIdent TOBrace enumerator_list TCBrace {}
240 enumerator_list: enumerator {}
241 | enumerator_list TComma enumerator {}
243 enumerator: TIdent {}
244 | TIdent TEq const_expr {}
245 /*------------------------------------------------------------------------------*/
247 init_declarator_list: init_declarator {}
248 | init_declarator_list TComma init_declarator {}
250 init_declarator: declarator {}
251 | declarator TEq initialize {}
253 /*------------------------------------------------------------------------------*/
254 declarator: pointer direct_declarator {}
255 | direct_declarator {}
258 | TMul type_qualif_list {}
260 | TMul type_qualif_list pointer {}
262 direct_declarator: TIdent {}
263 | TOPar declarator TCPar {}
264 | direct_declarator TOCro const_expr TCCro {}
265 | direct_declarator TOCro TCCro {}
266 | direct_declarator TOPar TCPar {}
267 | direct_declarator TOPar parameter_type_list TCPar {}
268 | direct_declarator TOPar identifier_list TCPar {}
270 type_qualif_list: type_qualif {}
271 | type_qualif_list type_qualif {}
273 parameter_type_list: parameter_list {}
274 | parameter_list TComma TEllipsis {}
276 parameter_list: parameter_decl {}
277 | parameter_list TComma parameter_decl {}
279 parameter_decl: decl_spec declarator {}
280 | decl_spec abstract_declarator {}
282 identifier_list: TIdent {}
283 | identifier_list TComma TIdent {}
284 /*------------------------------------------------------------------------------*/
286 type_name: spec_qualif_list {}
287 | spec_qualif_list abstract_declarator {}
289 abstract_declarator: pointer {}
290 | direct_abstract_declarator {}
291 | pointer direct_abstract_declarator {}
293 direct_abstract_declarator: TOPar abstract_declarator TCPar {}
295 | TOCro const_expr TCCro {}
296 | direct_abstract_declarator TOCro TCCro {}
297 | direct_abstract_declarator TOCro const_expr TCCro {}
299 | TOPar parameter_type_list TCPar {}
300 | direct_abstract_declarator TOPar TCPar {}
301 | direct_abstract_declarator TOPar parameter_type_list TCPar {}
303 /*------------------------------------------------------------------------------*/
304 initialize: assign_expr {}
305 | TOBrace initialize_list TCBrace {}
306 | TOBrace initialize_list TComma TCBrace {}
308 initialize_list: initialize {}
309 | initialize_list TComma initialize {}
311 /********************************************************************************/
313 translation_unit: external_declaration {}
314 | translation_unit external_declaration {}
316 external_declaration: function_definition {}
319 function_definition: decl_spec declarator decl_list compound {}
320 | decl_spec declarator compound {}
321 | declarator decl_list compound {}
322 | declarator compound {}