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 C |
26 | %token TOrLog TAndLog TOrIncl TOrExcl TAnd TEqEq TNotEq TInf TSup TInfEq TSupEq TShl TShr |
27 | TPlus TMinus TMul TDiv TMod | |
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 | |
ae4735db | 48 | %left TMul TDiv TMod |
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 {} | |
79 | | arith_expr TMod arith_expr {} | |
80 | | arith_expr TPlus arith_expr {} | |
81 | | arith_expr TMinus arith_expr {} | |
82 | | arith_expr TShl arith_expr {} | |
83 | | arith_expr TShr arith_expr {} | |
84 | | arith_expr TInf arith_expr {} | |
85 | | arith_expr TSup arith_expr {} | |
86 | | arith_expr TInfEq arith_expr {} | |
87 | | arith_expr TSupEq arith_expr {} | |
88 | | arith_expr TEqEq arith_expr {} | |
89 | | arith_expr TNotEq arith_expr {} | |
90 | | arith_expr TAnd arith_expr {} | |
91 | | arith_expr TOrExcl arith_expr {} | |
92 | | arith_expr TOrIncl arith_expr {} | |
93 | | arith_expr TAndLog arith_expr {} | |
94 | | arith_expr TOrLog arith_expr {} | |
95 | ||
96 | cast_expr: unary_expr {} | |
97 | | TOPar type_name TCPar cast_expr {} | |
98 | ||
99 | unary_expr: postfix_expr {} | |
100 | | TInc unary_expr {} | |
101 | | TDec unary_expr {} | |
102 | | unary_op cast_expr {} | |
103 | | Tsizeof unary_expr {} | |
104 | | Tsizeof TOPar type_name TCPar {} | |
105 | ||
106 | unary_op: TAnd {} | |
107 | | TMul {} | |
108 | | TPlus {} | |
109 | | TMinus{} | |
110 | | TTilde{} | |
111 | | TBang {} | |
112 | ||
113 | postfix_expr: primary_expr {} | |
114 | | postfix_expr TOCro expr TCCro {} | |
115 | | postfix_expr TOPar argument_expr_list TCPar {} | |
116 | | postfix_expr TOPar TCPar {} | |
117 | | postfix_expr TDot TIdent {} | |
118 | | postfix_expr TPtrOp TIdent {} | |
119 | | postfix_expr TInc {} | |
120 | | postfix_expr TDec {} | |
121 | ||
122 | argument_expr_list: assign_expr { } | |
123 | | argument_expr_list TComma assign_expr {} | |
124 | ||
125 | primary_expr: TIdent {} | |
126 | | TInt {} | |
127 | | TFloat {} | |
128 | | TString {} | |
129 | | TOPar expr TCPar {} | |
130 | ||
131 | const_expr: cond_expr {} | |
132 | /********************************************************************************/ | |
133 | ||
134 | statement: labeled {} | |
135 | | compound {} | |
136 | | expr_statement {} | |
137 | | selection {} | |
138 | | iteration {} | |
139 | | jump TPtVirg {} | |
140 | ||
141 | labeled: TIdent TDotDot statement {} | |
142 | | Tcase const_expr TDotDot statement {} | |
143 | | Tdefault TDotDot statement {} | |
144 | ||
145 | compound: TOBrace TCBrace {} | |
146 | | TOBrace statement_list TCBrace {} | |
147 | | TOBrace decl_list TCBrace {} | |
148 | | TOBrace decl_list statement_list TCBrace {} | |
149 | ||
150 | decl_list: decl {} | |
151 | | decl decl_list {} | |
152 | ||
153 | statement_list: statement {} | |
154 | | statement statement_list {} | |
155 | ||
156 | expr_statement: TPtVirg {} | |
157 | | expr TPtVirg {} | |
158 | ||
159 | selection: Tif TOPar expr TCPar statement {} | |
160 | | Tif TOPar expr TCPar statement Telse statement {} | |
161 | | Tswitch TOPar expr TCPar statement {} | |
162 | ||
163 | iteration: Twhile TOPar expr TCPar statement {} | |
164 | | Tdo statement Twhile TOPar expr TCPar TPtVirg {} | |
165 | | Tfor TOPar expr_statement expr_statement TCPar statement {} | |
166 | | Tfor TOPar expr_statement expr_statement expr TCPar statement {} | |
167 | ||
ae4735db | 168 | jump: Tgoto TIdent {} |
faf9a90c C |
169 | | Tcontinue {} |
170 | | Tbreak {} | |
ae4735db | 171 | | Treturn {} |
faf9a90c C |
172 | | Treturn expr {} |
173 | ||
174 | /********************************************************************************/ | |
175 | ||
176 | /*------------------------------------------------------------------------------*/ | |
177 | decl: decl_spec TPtVirg {} | |
178 | | decl_spec init_declarator_list TPtVirg {} | |
179 | ||
180 | /*------------------------------------------------------------------------------*/ | |
181 | decl_spec: storage_class_spec {} | |
182 | | storage_class_spec decl_spec {} | |
183 | | type_spec {} | |
184 | | type_spec decl_spec {} | |
185 | | type_qualif {} | |
186 | | type_qualif decl_spec {} | |
187 | ||
188 | storage_class_spec: Tstatic {} | |
189 | | Textern {} | |
190 | | Tauto {} | |
191 | | Tregister {} | |
192 | | Ttypedef {} | |
193 | type_spec: Tvoid {} | |
194 | | Tchar {} | |
195 | | Tshort {} | |
196 | | Tint {} | |
197 | | Tlong {} | |
198 | | Tfloat {} | |
199 | | Tdouble {} | |
200 | | Tsigned {} | |
201 | | Tunsigned {} | |
202 | | struct_or_union_spec {} | |
203 | | enum_spec {} | |
204 | /*TODO | TIdent {} */ | |
ae4735db | 205 | | TypedefIdent {} |
faf9a90c C |
206 | |
207 | type_qualif: Tconst {} | |
208 | | Tvolatile {} | |
209 | ||
210 | /*------------------------------------------------------------------------------*/ | |
211 | struct_or_union_spec: struct_or_union TIdent TOBrace struct_decl_list TCBrace {} | |
212 | | struct_or_union TOBrace struct_decl_list TCBrace {} | |
213 | | struct_or_union TIdent {} | |
214 | ||
215 | struct_or_union: Tstruct {} | |
216 | | Tunion {} | |
217 | ||
218 | struct_decl_list: struct_decl {} | |
219 | | struct_decl_list struct_decl {} | |
220 | ||
221 | struct_decl: spec_qualif_list struct_declarator_list TPtVirg {} | |
222 | ||
223 | spec_qualif_list: type_spec {} | |
224 | | type_spec spec_qualif_list {} | |
225 | | type_qualif {} | |
226 | | type_qualif spec_qualif_list {} | |
227 | ||
228 | struct_declarator_list: struct_declarator {} | |
229 | | struct_declarator_list TComma struct_declarator {} | |
230 | struct_declarator: declarator {} | |
231 | | TDotDot const_expr {} | |
232 | | declarator TDotDot const_expr {} | |
233 | /*------------------------------------------------------------------------------*/ | |
234 | enum_spec: Tenum TOBrace enumerator_list TCBrace {} | |
235 | | Tenum TIdent TOBrace enumerator_list TCBrace {} | |
236 | | Tenum TIdent {} | |
237 | ||
238 | enumerator_list: enumerator {} | |
239 | | enumerator_list TComma enumerator {} | |
240 | ||
241 | enumerator: TIdent {} | |
242 | | TIdent TEq const_expr {} | |
243 | /*------------------------------------------------------------------------------*/ | |
ae4735db | 244 | |
faf9a90c C |
245 | init_declarator_list: init_declarator {} |
246 | | init_declarator_list TComma init_declarator {} | |
247 | ||
248 | init_declarator: declarator {} | |
249 | | declarator TEq initialize {} | |
250 | ||
251 | /*------------------------------------------------------------------------------*/ | |
252 | declarator: pointer direct_declarator {} | |
253 | | direct_declarator {} | |
254 | ||
255 | pointer: TMul {} | |
256 | | TMul type_qualif_list {} | |
257 | | TMul pointer {} | |
258 | | TMul type_qualif_list pointer {} | |
259 | ||
260 | direct_declarator: TIdent {} | |
261 | | TOPar declarator TCPar {} | |
262 | | direct_declarator TOCro const_expr TCCro {} | |
263 | | direct_declarator TOCro TCCro {} | |
264 | | direct_declarator TOPar TCPar {} | |
265 | | direct_declarator TOPar parameter_type_list TCPar {} | |
266 | | direct_declarator TOPar identifier_list TCPar {} | |
267 | ||
268 | type_qualif_list: type_qualif {} | |
269 | | type_qualif_list type_qualif {} | |
270 | ||
271 | parameter_type_list: parameter_list {} | |
272 | | parameter_list TComma TEllipsis {} | |
273 | ||
274 | parameter_list: parameter_decl {} | |
275 | | parameter_list TComma parameter_decl {} | |
276 | ||
277 | parameter_decl: decl_spec declarator {} | |
278 | | decl_spec abstract_declarator {} | |
279 | | decl_spec {} | |
280 | identifier_list: TIdent {} | |
281 | | identifier_list TComma TIdent {} | |
282 | /*------------------------------------------------------------------------------*/ | |
283 | ||
284 | type_name: spec_qualif_list {} | |
285 | | spec_qualif_list abstract_declarator {} | |
286 | ||
287 | abstract_declarator: pointer {} | |
288 | | direct_abstract_declarator {} | |
289 | | pointer direct_abstract_declarator {} | |
290 | ||
291 | direct_abstract_declarator: TOPar abstract_declarator TCPar {} | |
292 | | TOCro TCCro {} | |
293 | | TOCro const_expr TCCro {} | |
294 | | direct_abstract_declarator TOCro TCCro {} | |
295 | | direct_abstract_declarator TOCro const_expr TCCro {} | |
296 | | TOPar TCPar {} | |
297 | | TOPar parameter_type_list TCPar {} | |
298 | | direct_abstract_declarator TOPar TCPar {} | |
299 | | direct_abstract_declarator TOPar parameter_type_list TCPar {} | |
ae4735db | 300 | |
faf9a90c C |
301 | /*------------------------------------------------------------------------------*/ |
302 | initialize: assign_expr {} | |
303 | | TOBrace initialize_list TCBrace {} | |
304 | | TOBrace initialize_list TComma TCBrace {} | |
305 | ||
306 | initialize_list: initialize {} | |
307 | | initialize_list TComma initialize {} | |
308 | ||
309 | /********************************************************************************/ | |
310 | ||
311 | translation_unit: external_declaration {} | |
312 | | translation_unit external_declaration {} | |
313 | ||
314 | external_declaration: function_definition {} | |
315 | | decl {} | |
316 | ||
317 | function_definition: decl_spec declarator decl_list compound {} | |
318 | | decl_spec declarator compound {} | |
319 | | declarator decl_list compound {} | |
320 | | declarator compound {} | |
321 | ||
322 | ||
323 |