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