]>
Commit | Line | Data |
---|---|---|
28bf4b0b | 1 | /*;-*-C-*-; |
2 | ** Copyright (c) Massachusetts Institute of Technology 1994-1998. | |
3 | ** All Rights Reserved. | |
4 | ** Unpublished rights reserved under the copyright laws of | |
5 | ** the United States. | |
6 | ** | |
7 | ** THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED | |
8 | ** OR IMPLIED. ANY USE IS AT YOUR OWN RISK. | |
9 | ** | |
10 | ** This code is distributed freely and may be used freely under the | |
11 | ** following conditions: | |
12 | ** | |
13 | ** 1. This notice may not be removed or altered. | |
14 | ** | |
15 | ** 2. Works derived from this code are not distributed for | |
16 | ** commercial gain without explicit permission from MIT | |
17 | ** (for permission contact lclint-request@sds.lcs.mit.edu). | |
18 | */ | |
19 | /* | |
20 | ** mtgrammar.y | |
21 | ** | |
22 | ** Grammar for .mts files. | |
23 | */ | |
24 | ||
25 | %{ | |
26 | ||
27 | # include "bison.reset" | |
28 | # include "lclintMacros.nf" | |
29 | # include "llbasic.h" | |
30 | # include "mtincludes.h" | |
31 | ||
32 | static /*@exits@*/ void mterror (char *); | |
33 | ||
34 | /*@-noparams@*/ /* Can't list params since YYSTYPE isn't defined yet. */ | |
35 | static void yyprint (/*FILE *p_file, int p_type, YYSTYPE p_value */); | |
36 | /*@=noparams@*/ | |
37 | ||
38 | # define YYPRINT(file, type, value) yyprint (file, type, value) | |
39 | ||
40 | # define YYDEBUG 1 | |
41 | ||
42 | # include "bison.head" | |
43 | ||
44 | %} | |
45 | ||
46 | %pure_parser | |
47 | ||
48 | %union { | |
49 | mttok tok; | |
50 | mtDeclarationNode mtdecl; | |
51 | mtDeclarationPiece mtpiece; | |
52 | mtDeclarationPieces mtpieces; | |
53 | mtContextNode mtcontext; | |
54 | mtValuesNode mtvalues; | |
55 | mtDefaultsNode mtdefaults; | |
56 | mtDefaultsDeclList mtdeflist; | |
57 | mtAnnotationsNode mtannotations; | |
58 | mtAnnotationList mtannotlist; | |
59 | mtAnnotationDecl mtannotdecl; | |
60 | mtMergeNode mtmerge; | |
61 | mtMergeItem mtmergeitem; | |
62 | mtMergeClauseList mtmergeclauselist; | |
63 | mtMergeClause mtmergeclause; | |
64 | mtTransferClauseList mttransferclauselist; | |
65 | mtTransferClause mttransferclause; | |
66 | mtTransferAction mttransferaction; | |
67 | mtLoseReferenceList mtlosereferencelist; | |
68 | mtLoseReference mtlosereference; | |
69 | ||
70 | /*@only@*/ cstringList cstringlist; | |
71 | ctype ctyp; | |
72 | /*@only@*/ qtype qtyp; | |
73 | int count; | |
74 | } | |
75 | ||
76 | /* Don't forget to enter all tokens in mtscanner.c */ | |
77 | %token <tok> MT_BADTOK | |
78 | ||
79 | %token <tok> MT_END | |
80 | %token <tok> MT_STATE MT_GLOBAL | |
81 | ||
82 | %token <tok> MT_CONTEXT | |
83 | %token <tok> MT_ONEOF | |
84 | ||
85 | %token <tok> MT_DEFAULTS MT_DEFAULT | |
86 | %token <tok> MT_REFERENCE MT_PARAMETER MT_CLAUSE | |
87 | ||
88 | %token <tok> MT_ANNOTATIONS | |
89 | %token <tok> MT_ARROW | |
90 | ||
91 | %token <tok> MT_MERGE | |
92 | %token <tok> MT_TRANSFERS MT_PRECONDITIONS MT_POSTCONDITIONS | |
93 | %token <tok> MT_LOSEREFERENCE | |
94 | ||
95 | %token <tok> MT_AS | |
96 | %token <tok> MT_ERROR | |
97 | ||
98 | %token <tok> MT_PLUS MT_STAR MT_BAR | |
99 | %token <tok> MT_LPAREN MT_RPAREN | |
100 | %token <tok> MT_LBRACKET MT_RBRACKET | |
101 | %token <tok> MT_LBRACE MT_RBRACE | |
102 | %token <tok> MT_COMMA | |
103 | ||
104 | %token <tok> MT_CHAR MT_INT MT_FLOAT MT_DOUBLE MT_VOID MT_ANYTYPE MT_INTEGRALTYPE MT_UNSIGNEDINTEGRALTYPE | |
105 | %token <tok> MT_SIGNEDINTEGRALTYPE | |
106 | %token <tok> MT_CONST MT_VOLATILE | |
107 | %token <tok> MT_STRINGLIT | |
108 | %token <tok> MT_IDENT | |
109 | ||
110 | %type <count> pointers | |
111 | %type <ctyp> optType typeSpecifier typeName abstractDecl abstractDeclBase | |
112 | %type <qtyp> typeExpression | |
113 | %type <qtyp> completeType completeTypeAux optCompleteType | |
114 | %type <mtpiece> declarationPiece | |
115 | %type <mtcontext> contextDeclaration | |
116 | %type <mtcontext> contextSelection optContextSelection | |
117 | %type <mtvalues> valuesDeclaration | |
118 | %type <tok> defaultNode | |
119 | %type <mtdefaults> defaultsDeclaration | |
120 | %type <mtdeflist> defaultDeclarationList | |
121 | %type <mtannotations> annotationsDeclaration | |
122 | %type <mtannotlist> annotationsDeclarationList | |
123 | %type <mtannotdecl> annotationDeclaration | |
124 | %type <mtmerge> mergeDeclaration | |
125 | %type <mtmergeitem> mergeItem | |
126 | %type <mtmergeclauselist> mergeClauses | |
127 | %type <mtmergeclause> mergeClause | |
128 | %type <mttransferclauselist> transfersDeclaration transferClauses preconditionsDeclaration postconditionsDeclaration | |
129 | %type <mttransferclause> transferClause | |
130 | %type <mttransferaction> transferAction errorAction | |
131 | %type <mtlosereferencelist> loseReferenceDeclaration lostClauses | |
132 | %type <mtlosereference> lostClause | |
133 | %type <cstringlist> valuesList | |
134 | %type <mtdecl> declarationNode | |
135 | %type <mtpieces> declarationPieces | |
136 | %type <tok> valueChoice | |
137 | ||
138 | ||
139 | %start file | |
140 | ||
141 | %% | |
142 | ||
143 | file | |
144 | : {} | |
145 | | mtsDeclaration {} | |
146 | ||
147 | mtsDeclaration | |
148 | : MT_STATE declarationNode MT_END | |
149 | { mtreader_processDeclaration ($2); } | |
150 | | MT_GLOBAL MT_STATE declarationNode MT_END | |
151 | { mtreader_processGlobalDeclaration ($3); } | |
152 | ||
153 | declarationNode | |
154 | : MT_IDENT declarationPieces | |
155 | { $$ = mtDeclarationNode_create ($1, $2); } | |
156 | ||
157 | declarationPieces | |
158 | : { $$ = mtDeclarationPieces_create (); } | |
159 | | declarationPiece declarationPieces | |
160 | { $$ = mtDeclarationPieces_append ($2, $1); } | |
161 | ||
162 | declarationPiece | |
163 | : contextDeclaration { $$ = mtDeclarationPiece_createContext ($1); } | |
164 | | valuesDeclaration { $$ = mtDeclarationPiece_createValues ($1); } | |
165 | | defaultsDeclaration { $$ = mtDeclarationPiece_createDefaults ($1); } | |
166 | | defaultNode { $$ = mtDeclarationPiece_createValueDefault ($1); } | |
167 | | annotationsDeclaration { $$ = mtDeclarationPiece_createAnnotations ($1); } | |
168 | | mergeDeclaration { $$ = mtDeclarationPiece_createMerge ($1); } | |
169 | | transfersDeclaration { $$ = mtDeclarationPiece_createTransfers ($1); } | |
170 | | preconditionsDeclaration { $$ = mtDeclarationPiece_createPreconditions ($1); } | |
171 | | postconditionsDeclaration { $$ = mtDeclarationPiece_createPostconditions ($1); } | |
172 | | loseReferenceDeclaration { $$ = mtDeclarationPiece_createLosers ($1); } | |
173 | ||
174 | contextDeclaration | |
175 | : MT_CONTEXT contextSelection { $$ = $2; } | |
176 | /* ??? should it be a list? */ | |
177 | ||
178 | optContextSelection | |
179 | : /* empty */ { $$ = mtContextNode_createAny (); } | |
180 | | contextSelection | |
181 | ||
182 | contextSelection | |
183 | : MT_PARAMETER optType { $$ = mtContextNode_createParameter ($2); } | |
184 | | MT_REFERENCE optType { $$ = mtContextNode_createReference ($2); } | |
185 | | MT_CLAUSE optType { $$ = mtContextNode_createClause ($2); } | |
186 | ||
187 | /* | |
188 | ** Wish I could share the C grammar here...cut-and-paste instead. | |
189 | */ | |
190 | ||
191 | optType | |
192 | : /* empty */ { $$ = ctype_unknown; } | |
193 | | typeExpression { DPRINTF (("Type: %s", qtype_unparse ($1))); $$ = qtype_getType ($1); } | |
194 | ||
195 | typeExpression | |
196 | : completeType | |
197 | | completeType abstractDecl { $$ = qtype_newBase ($1, $2); } | |
198 | ||
199 | completeType | |
200 | : completeTypeAux { $$ = $1; } | |
201 | | completeType MT_BAR typeExpression | |
202 | { $$ = qtype_mergeAlt ($1, $3); } | |
203 | ||
204 | completeTypeAux | |
205 | : typeSpecifier optCompleteType { $$ = qtype_combine ($2, $1); } | |
206 | ||
207 | optCompleteType | |
208 | : /* empty */ { $$ = qtype_unknown (); } | |
209 | | completeType { $$ = $1; } | |
210 | ||
211 | ||
212 | abstractDecl | |
213 | : pointers { $$ = ctype_adjustPointers ($1, ctype_unknown); } | |
214 | | abstractDeclBase | |
215 | | pointers abstractDeclBase { $$ = ctype_adjustPointers ($1, $2); } | |
216 | ||
217 | pointers | |
218 | : MT_STAR { $$ = 1; } | |
219 | | MT_STAR innerModsList { $$ = 1; } | |
220 | | MT_STAR pointers { $$ = 1 + $2; } | |
221 | | MT_STAR innerModsList pointers { $$ = 1 + $3; } | |
222 | ||
223 | innerMods | |
224 | : MT_CONST { /* ignored for now */; } | |
225 | | MT_VOLATILE { ; } | |
226 | ||
227 | innerModsList | |
228 | : innerMods { ; } | |
229 | | innerModsList innerMods { ; } | |
230 | ||
231 | abstractDeclBase | |
232 | : MT_LPAREN abstractDecl MT_RPAREN { $$ = ctype_expectFunction ($2); } | |
233 | | MT_LBRACKET MT_RBRACKET { $$ = ctype_makeArray (ctype_unknown); } | |
234 | | abstractDeclBase MT_LBRACKET MT_RBRACKET { $$ = ctype_makeArray ($1); } | |
235 | /* | |
236 | | abstractDeclBase MT_LBRACKET constantExpr MT_RBRACKET | |
237 | { $$ = ctype_makeFixedArray ($1, exprNode_getLongValue ($3)); } | |
238 | */ | |
239 | ||
240 | typeSpecifier | |
241 | : MT_CHAR { $$ = ctype_char; } | |
242 | | MT_INT { $$ = ctype_int; } | |
243 | | MT_FLOAT { $$ = ctype_float; } | |
244 | | MT_DOUBLE { $$ = ctype_double; } | |
245 | | MT_VOID { $$ = ctype_void; } | |
246 | | MT_ANYTYPE { $$ = ctype_unknown; } | |
247 | | MT_INTEGRALTYPE { $$ = ctype_anyintegral; } | |
248 | | MT_UNSIGNEDINTEGRALTYPE { $$ = ctype_unsignedintegral; } | |
249 | | MT_SIGNEDINTEGRALTYPE { $$ = ctype_signedintegral; } | |
250 | | typeName | |
251 | /* | suSpc | |
252 | | enumSpc | |
253 | | typeModifier NotType { $$ = ctype_fromQual ($1); } */ | |
254 | ||
255 | typeName | |
256 | : MT_IDENT { $$ = mtscanner_lookupType ($1); } | |
257 | ||
258 | valuesDeclaration | |
259 | : MT_ONEOF valuesList { $$ = mtValuesNode_create ($2); } | |
260 | ||
261 | valuesList | |
262 | : MT_IDENT { $$ = cstringList_single (mttok_getText ($1)); } | |
263 | | MT_IDENT MT_COMMA valuesList | |
264 | { $$ = cstringList_prepend ($3, mttok_getText ($1)); } | |
265 | ||
266 | defaultNode | |
267 | : MT_DEFAULT valueChoice { $$ = $2; } | |
268 | ||
269 | defaultsDeclaration | |
270 | : MT_DEFAULTS defaultDeclarationList { $$ = mtDefaultsNode_create ($1, $2); } | |
271 | ||
272 | defaultDeclarationList | |
273 | : contextSelection MT_ARROW valueChoice | |
274 | { $$ = mtDefaultsDeclList_single (mtDefaultsDecl_create ($1, $3)); } | |
275 | | contextSelection MT_ARROW valueChoice defaultDeclarationList | |
276 | { $$ = mtDefaultsDeclList_prepend ($4, mtDefaultsDecl_create ($1, $3)); } | |
277 | ||
278 | annotationsDeclaration | |
279 | : MT_ANNOTATIONS annotationsDeclarationList { $$ = mtAnnotationsNode_create ($2); } | |
280 | ||
281 | annotationsDeclarationList | |
282 | : annotationDeclaration { $$ = mtAnnotationList_single ($1); } | |
283 | | annotationDeclaration annotationsDeclarationList | |
284 | { $$ = mtAnnotationList_prepend ($2, $1); } | |
285 | ||
286 | annotationDeclaration | |
287 | : MT_IDENT optContextSelection MT_ARROW valueChoice | |
288 | { $$ = mtAnnotationDecl_create ($1, $2, $4); } | |
289 | ||
290 | mergeDeclaration | |
291 | : MT_MERGE mergeClauses { $$ = mtMergeNode_create ($2); } | |
292 | ||
293 | mergeClauses | |
294 | : mergeClause { $$ = mtMergeClauseList_single ($1); } | |
295 | | mergeClause mergeClauses { $$ = mtMergeClauseList_prepend ($2, $1); } | |
296 | ||
297 | mergeClause | |
298 | : mergeItem MT_PLUS mergeItem MT_ARROW transferAction | |
299 | { $$ = mtMergeClause_create ($1, $3, $5); } | |
300 | ||
301 | mergeItem | |
302 | : valueChoice { $$ = mtMergeItem_createValue ($1); } | |
303 | | MT_STAR { $$ = mtMergeItem_createStar ($1); } | |
304 | ||
305 | preconditionsDeclaration | |
306 | : MT_PRECONDITIONS transferClauses { $$ = $2; } | |
307 | ||
308 | postconditionsDeclaration | |
309 | : MT_POSTCONDITIONS transferClauses { $$ = $2; } | |
310 | ||
311 | transfersDeclaration | |
312 | : MT_TRANSFERS transferClauses { $$ = $2; } | |
313 | ||
314 | loseReferenceDeclaration | |
315 | : MT_LOSEREFERENCE lostClauses { $$ = $2; } | |
316 | ||
317 | lostClauses | |
318 | : lostClause { $$ = mtLoseReferenceList_single ($1); } | |
319 | | lostClause lostClauses { $$ = mtLoseReferenceList_prepend ($2, $1); } | |
320 | ||
321 | lostClause | |
322 | : valueChoice MT_ARROW errorAction { $$ = mtLoseReference_create ($1, $3); } | |
323 | ||
324 | transferClauses | |
325 | : transferClause { $$ = mtTransferClauseList_single ($1); } | |
326 | | transferClause transferClauses { $$ = mtTransferClauseList_prepend ($2, $1); } | |
327 | ||
328 | transferClause | |
329 | : valueChoice MT_AS valueChoice MT_ARROW transferAction | |
330 | { $$ = mtTransferClause_create ($1, $3, $5); } | |
331 | ||
332 | transferAction | |
333 | : valueChoice { $$ = mtTransferAction_createValue ($1); } | |
334 | | errorAction { $$ = $1; } | |
335 | ||
336 | errorAction | |
337 | : MT_ERROR { $$ = mtTransferAction_createError ($1); } | |
338 | | MT_ERROR MT_STRINGLIT { $$ = mtTransferAction_createErrorMessage ($2); } | |
339 | ||
340 | valueChoice | |
341 | : MT_IDENT { $$ = $1; } | |
342 | ||
343 | %% | |
344 | ||
345 | # include "bison.reset" | |
346 | ||
347 | extern char *yytext; | |
348 | ||
349 | static void mterror (char *s) | |
350 | { | |
351 | llfatalbug | |
352 | (cstring_makeLiteral | |
353 | ("There has been a problem in the .mts parser.")); | |
354 | } | |
355 | ||
356 | static void yyprint (FILE *file, int type, YYSTYPE value) | |
357 | { | |
358 | cstring tname = mttok_unparse (value.tok); | |
359 | fprintf (file, " (%s)", cstring_toCharsSafe (tname)); | |
360 | cstring_free (tname); | |
361 | } | |
362 | ||
363 | ||
364 | ||
365 |