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