Commit | Line | Data |
---|---|---|
faf9a90c | 1 | %{ |
ae4735db C |
2 | (* src: ocamlyaccified from |
3 | * http://www.lysator.liu.se/c/ANSI-C-grammar-y.html | |
faf9a90c C |
4 | *) |
5 | open Common | |
6 | open AbstractSyntax | |
7 | exception 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 | ||
54 | main: translation_unit EOF { [] } | |
55 | ||
56 | /********************************************************************************/ | |
57 | /* | |
58 | expression | |
59 | statement | |
60 | declaration | |
61 | main | |
62 | */ | |
63 | ||
64 | /********************************************************************************/ | |
65 | ||
66 | expr: assign_expr { } | |
67 | | expr TComma assign_expr { } | |
68 | ||
69 | assign_expr: cond_expr { } | |
70 | | unary_expr TAssign assign_expr { } | |
71 | | unary_expr TEq assign_expr { } | |
72 | ||
73 | cond_expr: arith_expr {} | |
74 | | arith_expr TWhy expr TDotDot cond_expr {} | |
75 | ||
76 | arith_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 | ||
98 | cast_expr: unary_expr {} | |
99 | | TOPar type_name TCPar cast_expr {} | |
100 | ||
101 | unary_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 | ||
108 | unary_op: TAnd {} | |
109 | | TMul {} | |
110 | | TPlus {} | |
111 | | TMinus{} | |
112 | | TTilde{} | |
113 | | TBang {} | |
114 | ||
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 {} | |
123 | ||
124 | argument_expr_list: assign_expr { } | |
125 | | argument_expr_list TComma assign_expr {} | |
126 | ||
127 | primary_expr: TIdent {} | |
128 | | TInt {} | |
129 | | TFloat {} | |
130 | | TString {} | |
131 | | TOPar expr TCPar {} | |
132 | ||
133 | const_expr: cond_expr {} | |
134 | /********************************************************************************/ | |
135 | ||
136 | statement: labeled {} | |
137 | | compound {} | |
138 | | expr_statement {} | |
139 | | selection {} | |
140 | | iteration {} | |
141 | | jump TPtVirg {} | |
142 | ||
143 | labeled: TIdent TDotDot statement {} | |
144 | | Tcase const_expr TDotDot statement {} | |
145 | | Tdefault TDotDot statement {} | |
146 | ||
147 | compound: TOBrace TCBrace {} | |
148 | | TOBrace statement_list TCBrace {} | |
149 | | TOBrace decl_list TCBrace {} | |
150 | | TOBrace decl_list statement_list TCBrace {} | |
151 | ||
152 | decl_list: decl {} | |
153 | | decl decl_list {} | |
154 | ||
155 | statement_list: statement {} | |
156 | | statement statement_list {} | |
157 | ||
158 | expr_statement: TPtVirg {} | |
159 | | expr TPtVirg {} | |
160 | ||
161 | selection: Tif TOPar expr TCPar statement {} | |
162 | | Tif TOPar expr TCPar statement Telse statement {} | |
163 | | Tswitch TOPar expr TCPar statement {} | |
164 | ||
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 {} | |
169 | ||
ae4735db | 170 | jump: Tgoto TIdent {} |
faf9a90c C |
171 | | Tcontinue {} |
172 | | Tbreak {} | |
ae4735db | 173 | | Treturn {} |
faf9a90c C |
174 | | Treturn expr {} |
175 | ||
176 | /********************************************************************************/ | |
177 | ||
178 | /*------------------------------------------------------------------------------*/ | |
179 | decl: decl_spec TPtVirg {} | |
180 | | decl_spec init_declarator_list TPtVirg {} | |
181 | ||
182 | /*------------------------------------------------------------------------------*/ | |
183 | decl_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 | ||
190 | storage_class_spec: Tstatic {} | |
191 | | Textern {} | |
192 | | Tauto {} | |
193 | | Tregister {} | |
194 | | Ttypedef {} | |
195 | type_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 | |
209 | type_qualif: Tconst {} | |
210 | | Tvolatile {} | |
211 | ||
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 {} | |
216 | ||
217 | struct_or_union: Tstruct {} | |
218 | | Tunion {} | |
219 | ||
220 | struct_decl_list: struct_decl {} | |
221 | | struct_decl_list struct_decl {} | |
222 | ||
223 | struct_decl: spec_qualif_list struct_declarator_list TPtVirg {} | |
224 | ||
225 | spec_qualif_list: type_spec {} | |
226 | | type_spec spec_qualif_list {} | |
227 | | type_qualif {} | |
228 | | type_qualif spec_qualif_list {} | |
229 | ||
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 {} | |
238 | | Tenum TIdent {} | |
239 | ||
240 | enumerator_list: enumerator {} | |
241 | | enumerator_list TComma enumerator {} | |
242 | ||
243 | enumerator: TIdent {} | |
244 | | TIdent TEq const_expr {} | |
245 | /*------------------------------------------------------------------------------*/ | |
ae4735db | 246 | |
faf9a90c C |
247 | init_declarator_list: init_declarator {} |
248 | | init_declarator_list TComma init_declarator {} | |
249 | ||
250 | init_declarator: declarator {} | |
251 | | declarator TEq initialize {} | |
252 | ||
253 | /*------------------------------------------------------------------------------*/ | |
254 | declarator: pointer direct_declarator {} | |
255 | | direct_declarator {} | |
256 | ||
257 | pointer: TMul {} | |
258 | | TMul type_qualif_list {} | |
259 | | TMul pointer {} | |
260 | | TMul type_qualif_list pointer {} | |
261 | ||
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 {} | |
269 | ||
270 | type_qualif_list: type_qualif {} | |
271 | | type_qualif_list type_qualif {} | |
272 | ||
273 | parameter_type_list: parameter_list {} | |
274 | | parameter_list TComma TEllipsis {} | |
275 | ||
276 | parameter_list: parameter_decl {} | |
277 | | parameter_list TComma parameter_decl {} | |
278 | ||
279 | parameter_decl: decl_spec declarator {} | |
280 | | decl_spec abstract_declarator {} | |
281 | | decl_spec {} | |
282 | identifier_list: TIdent {} | |
283 | | identifier_list TComma TIdent {} | |
284 | /*------------------------------------------------------------------------------*/ | |
285 | ||
286 | type_name: spec_qualif_list {} | |
287 | | spec_qualif_list abstract_declarator {} | |
288 | ||
289 | abstract_declarator: pointer {} | |
290 | | direct_abstract_declarator {} | |
291 | | pointer direct_abstract_declarator {} | |
292 | ||
293 | direct_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 | /*------------------------------------------------------------------------------*/ |
304 | initialize: assign_expr {} | |
305 | | TOBrace initialize_list TCBrace {} | |
306 | | TOBrace initialize_list TComma TCBrace {} | |
307 | ||
308 | initialize_list: initialize {} | |
309 | | initialize_list TComma initialize {} | |
310 | ||
311 | /********************************************************************************/ | |
312 | ||
313 | translation_unit: external_declaration {} | |
314 | | translation_unit external_declaration {} | |
315 | ||
316 | external_declaration: function_definition {} | |
317 | | decl {} | |
318 | ||
319 | function_definition: decl_spec declarator decl_list compound {} | |
320 | | decl_spec declarator compound {} | |
321 | | declarator decl_list compound {} | |
322 | | declarator compound {} | |
323 | ||
324 | ||
325 |