permit multiline comments and strings in macros
[bpt/coccinelle.git] / parsing_c / orig.mly
CommitLineData
faf9a90c 1%{
ae4735db
C
2(* src: ocamlyaccified from
3 * http://www.lysator.liu.se/c/ANSI-C-grammar-y.html
faf9a90c
C
4 *)
5open Common
6open AbstractSyntax
7exception Parsing of string
8%}
9
10%token <string * AbstractSyntax.fullType> TString
11%token <string> TIdent
12%token <int * AbstractSyntax.intType> TInt
13%token <float * AbstractSyntax.floatType> TFloat
14
15/*(* conflicts *)*/
16%token <string> TypedefIdent
17
ae4735db
C
18%token TOPar TCPar TOBrace TCBrace TOCro TCCro
19%token TDot TComma TPtrOp
faf9a90c 20%token TInc TDec
ae4735db 21%token <AbstractSyntax.assignOp> TAssign
faf9a90c
C
22%token TEq
23%token TWhy TDotDot TPtVirg TTilde TBang
24%token TEllipsis
25
ae4735db 26%token TOrLog TAndLog TOrIncl TOrExcl TAnd TEqEq TNotEq TInf TSup TInfEq TSupEq TShl TShr
1b9ae606 27 TPlus TMinus TMul TDiv TMod TMin TMax
faf9a90c
C
28
29%token Tchar Tshort Tint Tdouble Tfloat Tlong Tunsigned Tsigned Tvoid
ae4735db 30 Tauto Tregister Textern Tstatic
faf9a90c
C
31 Tconst Tvolatile
32 Tstruct Tenum Ttypedef Tunion
33 Tbreak Telse Tswitch Tcase Tcontinue Tfor Tdo Tif Twhile Treturn Tgoto Tdefault
ae4735db 34 Tsizeof
faf9a90c
C
35
36%token EOF
37
38
39%left TOrLog
40%left TAndLog
41%left TOrIncl
42%left TOrExcl
ae4735db 43%left TAnd
faf9a90c 44%left TEqEq TNotEq
ae4735db 45%left TInf TSup TInfEq TSupEq
faf9a90c
C
46%left TShl TShr
47%left TPlus TMinus
1b9ae606 48%left TMul TDiv TMod TMin TMax
faf9a90c
C
49
50%start main
51%type <int list> main
52%%
53
54main: translation_unit EOF { [] }
55
56/********************************************************************************/
57/*
58 expression
59 statement
60 declaration
61 main
62*/
63
64/********************************************************************************/
65
66expr: assign_expr { }
67 | expr TComma assign_expr { }
68
69assign_expr: cond_expr { }
70 | unary_expr TAssign assign_expr { }
71 | unary_expr TEq assign_expr { }
72
73cond_expr: arith_expr {}
74 | arith_expr TWhy expr TDotDot cond_expr {}
75
76arith_expr: cast_expr {}
77 | arith_expr TMul arith_expr {}
78 | arith_expr TDiv arith_expr {}
1b9ae606
C
79 | arith_expr TMin arith_expr {}
80 | arith_expr TMax arith_expr {}
faf9a90c
C
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 {}
97
98cast_expr: unary_expr {}
99 | TOPar type_name TCPar cast_expr {}
100
101unary_expr: postfix_expr {}
102 | TInc unary_expr {}
103 | TDec unary_expr {}
104 | unary_op cast_expr {}
105 | Tsizeof unary_expr {}
106 | Tsizeof TOPar type_name TCPar {}
107
108unary_op: TAnd {}
109 | TMul {}
110 | TPlus {}
111 | TMinus{}
112 | TTilde{}
113 | TBang {}
114
115postfix_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 {}
123
124argument_expr_list: assign_expr { }
125 | argument_expr_list TComma assign_expr {}
126
127primary_expr: TIdent {}
128 | TInt {}
129 | TFloat {}
130 | TString {}
131 | TOPar expr TCPar {}
132
133const_expr: cond_expr {}
134/********************************************************************************/
135
136statement: labeled {}
137 | compound {}
138 | expr_statement {}
139 | selection {}
140 | iteration {}
141 | jump TPtVirg {}
142
143labeled: TIdent TDotDot statement {}
144 | Tcase const_expr TDotDot statement {}
145 | Tdefault TDotDot statement {}
146
147compound: TOBrace TCBrace {}
148 | TOBrace statement_list TCBrace {}
149 | TOBrace decl_list TCBrace {}
150 | TOBrace decl_list statement_list TCBrace {}
151
152decl_list: decl {}
153 | decl decl_list {}
154
155statement_list: statement {}
156 | statement statement_list {}
157
158expr_statement: TPtVirg {}
159 | expr TPtVirg {}
160
161selection: Tif TOPar expr TCPar statement {}
162 | Tif TOPar expr TCPar statement Telse statement {}
163 | Tswitch TOPar expr TCPar statement {}
164
165iteration: 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 {}
169
ae4735db 170jump: Tgoto TIdent {}
faf9a90c
C
171 | Tcontinue {}
172 | Tbreak {}
ae4735db 173 | Treturn {}
faf9a90c
C
174 | Treturn expr {}
175
176/********************************************************************************/
177
178/*------------------------------------------------------------------------------*/
179decl: decl_spec TPtVirg {}
180 | decl_spec init_declarator_list TPtVirg {}
181
182/*------------------------------------------------------------------------------*/
183decl_spec: storage_class_spec {}
184 | storage_class_spec decl_spec {}
185 | type_spec {}
186 | type_spec decl_spec {}
187 | type_qualif {}
188 | type_qualif decl_spec {}
189
190storage_class_spec: Tstatic {}
191 | Textern {}
192 | Tauto {}
193 | Tregister {}
194 | Ttypedef {}
195type_spec: Tvoid {}
196 | Tchar {}
197 | Tshort {}
198 | Tint {}
199 | Tlong {}
200 | Tfloat {}
201 | Tdouble {}
202 | Tsigned {}
203 | Tunsigned {}
204 | struct_or_union_spec {}
205 | enum_spec {}
206/*TODO | TIdent {} */
ae4735db 207 | TypedefIdent {}
faf9a90c
C
208
209type_qualif: Tconst {}
210 | Tvolatile {}
211
212/*------------------------------------------------------------------------------*/
213struct_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 {}
216
217struct_or_union: Tstruct {}
218 | Tunion {}
219
220struct_decl_list: struct_decl {}
221 | struct_decl_list struct_decl {}
222
223struct_decl: spec_qualif_list struct_declarator_list TPtVirg {}
224
225spec_qualif_list: type_spec {}
226 | type_spec spec_qualif_list {}
227 | type_qualif {}
228 | type_qualif spec_qualif_list {}
229
230struct_declarator_list: struct_declarator {}
231 | struct_declarator_list TComma struct_declarator {}
232struct_declarator: declarator {}
233 | TDotDot const_expr {}
234 | declarator TDotDot const_expr {}
235/*------------------------------------------------------------------------------*/
236enum_spec: Tenum TOBrace enumerator_list TCBrace {}
237 | Tenum TIdent TOBrace enumerator_list TCBrace {}
238 | Tenum TIdent {}
239
240enumerator_list: enumerator {}
241 | enumerator_list TComma enumerator {}
242
243enumerator: TIdent {}
244 | TIdent TEq const_expr {}
245/*------------------------------------------------------------------------------*/
ae4735db 246
faf9a90c
C
247init_declarator_list: init_declarator {}
248 | init_declarator_list TComma init_declarator {}
249
250init_declarator: declarator {}
251 | declarator TEq initialize {}
252
253/*------------------------------------------------------------------------------*/
254declarator: pointer direct_declarator {}
255 | direct_declarator {}
256
257pointer: TMul {}
258 | TMul type_qualif_list {}
259 | TMul pointer {}
260 | TMul type_qualif_list pointer {}
261
262direct_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 {}
269
270type_qualif_list: type_qualif {}
271 | type_qualif_list type_qualif {}
272
273parameter_type_list: parameter_list {}
274 | parameter_list TComma TEllipsis {}
275
276parameter_list: parameter_decl {}
277 | parameter_list TComma parameter_decl {}
278
279parameter_decl: decl_spec declarator {}
280 | decl_spec abstract_declarator {}
281 | decl_spec {}
282identifier_list: TIdent {}
283 | identifier_list TComma TIdent {}
284/*------------------------------------------------------------------------------*/
285
286type_name: spec_qualif_list {}
287 | spec_qualif_list abstract_declarator {}
288
289abstract_declarator: pointer {}
290 | direct_abstract_declarator {}
291 | pointer direct_abstract_declarator {}
292
293direct_abstract_declarator: TOPar abstract_declarator TCPar {}
294 | TOCro TCCro {}
295 | TOCro const_expr TCCro {}
296 | direct_abstract_declarator TOCro TCCro {}
297 | direct_abstract_declarator TOCro const_expr TCCro {}
298 | TOPar TCPar {}
299 | TOPar parameter_type_list TCPar {}
300 | direct_abstract_declarator TOPar TCPar {}
301 | direct_abstract_declarator TOPar parameter_type_list TCPar {}
ae4735db 302
faf9a90c
C
303/*------------------------------------------------------------------------------*/
304initialize: assign_expr {}
305 | TOBrace initialize_list TCBrace {}
306 | TOBrace initialize_list TComma TCBrace {}
307
308initialize_list: initialize {}
309 | initialize_list TComma initialize {}
310
311/********************************************************************************/
312
313translation_unit: external_declaration {}
314 | translation_unit external_declaration {}
315
316external_declaration: function_definition {}
317 | decl {}
318
319function_definition: decl_spec declarator decl_list compound {}
320 | decl_spec declarator compound {}
321 | declarator decl_list compound {}
322 | declarator compound {}
323
324
325