source: CIVL/grammar/PreprocessorLexer.g@ 844ebd8

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since 844ebd8 was 59b5362, checked in by Stephen Siegel <siegel@…>, 14 years ago

Updated CIVL-C grammar to use all \ and contracts.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@12 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 8.2 KB
RevLine 
[d96fdd6d]1lexer grammar PreprocessorLexer;
2
3/*
4 * Author: Stephen F. Siegel, University of Delaware
5 * Last changed: June 2012
6 *
7 * This is a grammar for lexical analysis for a preprocessor
8 * file. It follows the C11 Standard. This grammar assumes
9 * that the stream of characters being scanned has already
10 * gone through translation phases 1 and 2. In particular
11 * backslash followed by newline sequences have been removed.
12 */
13
14@header
15{
[af8a6cb]16package edu.udel.cis.vsl.civl.civlc.preproc.common;
[d96fdd6d]17}
18
19@members
20{
21
22public boolean inInclude = false; // are we inside a #include directive?
23public boolean inCondition = false; // are we inside a #if condition?
24public boolean atLineStart = true; // are we at start of line + possible WS?
25
26@Override
27public void emitErrorMessage(String msg) { // don't try to recover!
28 throw new RuntimeException(msg);
29}
30
31}
32
33
34/* Preprocessor directives and pragmas */
35
36fragment
37NotLineStart : {atLineStart = false;} ;
38
39PDEFINE : {atLineStart}?=>WS* '#' WS* 'define' NotLineStart;
40PINCLUDE : {atLineStart}?=>WS* '#' WS* 'include'
41 {inInclude = true; atLineStart=false;}
42 ;
43PIFDEF : {atLineStart}?=>WS* '#' WS* 'ifdef' NotLineStart;
44PIFNDEF : {atLineStart}?=>WS* '#' WS* 'ifndef' NotLineStart;
45PIF : {atLineStart}?=>WS* '#' WS* 'if'
46 {inCondition = true; atLineStart = false;}
47 ;
48PENDIF : {atLineStart}?=>WS* '#' WS* 'endif' NotLineStart;
49PELIF : {atLineStart}?=>WS* '#' WS* 'elif'
50 {inCondition = true; atLineStart = false;}
51 ;
52PELSE : {atLineStart}?=>WS* '#' WS* 'else' NotLineStart;
53PRAGMA : {atLineStart}?=>WS* '#' WS* 'pragma' NotLineStart;
54PERROR : {atLineStart}?=>WS* '#' WS* 'error' NotLineStart;
55PUNDEF : {atLineStart}?=>WS* '#' WS* 'undef' NotLineStart;
56PLINE : {atLineStart}?=>WS* '#' WS* 'line' NotLineStart;
57//PBLANK : {atLineStart}?=>WS* '#' WS* NotLineStart;
58HASH : WS* '#' WS*;
59DEFINED : {inCondition}?=>'defined' NotLineStart;
60
61/****** White space ******/
62
63NEWLINE : NewLine;
64
65fragment
66NewLine : '\r'? '\n'
67 {inCondition=false; atLineStart=true;};
68
69WS : (' ' | '\t')+;
70
71
72/****** C keywords, from C11 Sec. 6.4.1 ******/
73
74AUTO : 'auto';
75BREAK : 'break';
76CASE : 'case';
77CHAR : 'char';
78CONST : 'const';
79CONTINUE : 'continue';
80DEFAULT : 'default';
81DO : 'do';
82DOUBLE : 'double';
83ELSE : 'else';
84ENUM : 'enum';
85EXTERN : 'extern';
86FLOAT : 'float';
87FOR : 'for';
88GOTO : 'goto';
89IF : 'if';
90INLINE : 'inline';
91INT : 'int';
92LONG : 'long';
93REGISTER : 'register';
94RESTRICT : 'restrict';
95RETURN : 'return';
96SHORT : 'short';
97SIGNED : 'signed';
98SIZEOF : 'sizeof';
99STATIC : 'static';
100STRUCT : 'struct';
101SWITCH : 'switch';
102TYPEDEF : 'typedef';
103UNION : 'union';
104UNSIGNED : 'unsigned';
105VOID : 'void';
106VOLATILE : 'volatile';
107WHILE : 'while';
108ALIGNAS : '_Alignas';
109ALIGNOF : '_Alignof';
110ATOMIC : '_Atomic';
111BOOL : '_Bool';
112COMPLEX : '_Complex';
113GENERIC : '_Generic';
114IMAGINARY : '_Imaginary';
115NORETURN : '_Noreturn';
116STATICASSERT : '_Static_assert';
117THREADLOCAL : '_Thread_local';
118
119
[59b5362]120/* Additional keywords and symbols used in CIVL-C */
[d96fdd6d]121
[59b5362]122ASSERT : '\\assert';
123ASSUME : '\\assume';
[d96fdd6d]124AT : '@';
[59b5362]125CHOOSE : '\\choose';
[d96fdd6d]126COLLECTIVE : '\\collective';
[59b5362]127ENSURES : '\\ensures';
128FALSE : '\\false';
129INPUT : '\\input';
130INVARIANT : '\\invariant';
131OUTPUT : '\\output';
132PROC : '\\proc';
133REQUIRES : '\\requires';
134RESULT : '\\result';
135SELF : '\\self';
136SPAWN : '\\spawn';
137TRUE : '\\true';
138WAIT : '\\wait';
139WHEN : '\\when';
[d96fdd6d]140
141
142
143/****** Identifiers: C11 Sec. 6.4.2 ******/
144
145IDENTIFIER : IdentifierNonDigit
146 (IdentifierNonDigit | Digit)* NotLineStart
147 ;
148
149fragment
150IdentifierNonDigit
151 : NonDigit | UniversalCharacterName ;
152
153fragment
154Zero : '0' ;
155
156fragment
157Digit : Zero | NonZeroDigit ;
158
159fragment
160NonZeroDigit : '1' .. '9' ;
161
162fragment
163NonDigit : 'A'..'Z' | 'a'..'z' | '_' ;
164
165fragment
166UniversalCharacterName
167 : '\\' 'u' HexQuad
168 | '\\' 'U' HexQuad HexQuad
169 ;
170
171fragment
172HexQuad : HexadecimalDigit HexadecimalDigit HexadecimalDigit HexadecimalDigit ;
173
174fragment
175HexadecimalDigit
176 : '0'..'9' | 'a'..'f' | 'A'..'F' ;
177
178/****** Sec. 6.4.4.1: Integer constants ******/
179
180INTEGER_CONSTANT
181 : DecimalConstant IntegerSuffix?
182 | OctalConstant IntegerSuffix?
183 | HexadecimalConstant IntegerSuffix?
184 ;
185
186fragment
187DecimalConstant : NonZeroDigit Digit* ;
188
189
190fragment
191IntegerSuffix : UnsignedSuffix LongSuffix?
192 | UnsignedSuffix LongLongSuffix
193 | LongSuffix UnsignedSuffix?
194 | LongLongSuffix UnsignedSuffix?
195 ;
196
197fragment
198UnsignedSuffix : 'u' | 'U';
199
200fragment
201LongSuffix : 'l' | 'L';
202
203fragment
204LongLongSuffix : 'll' | 'LL';
205
206fragment
207OctalConstant : Zero OctalDigit* IntegerSuffix? NotLineStart;
208
209fragment
210HexadecimalConstant
211 : HexPrefix HexadecimalDigit+ IntegerSuffix? NotLineStart;
212
213fragment
214HexPrefix : Zero ('x' | 'X') ;
215
216/****** Sec. 6.4.4.2: Floating Constants ******/
217
218FLOATING_CONSTANT
219 : DecimalFloatingConstant
220 | HexadecimalFloatingConstant
221 ;
222
223fragment
224DecimalFloatingConstant
225 : FractionalConstant ExponentPart? FloatingSuffix?
226 | Digit+ ExponentPart FloatingSuffix?
227 ;
228
229fragment
230FractionalConstant
231 : Digit* '.' Digit+
232 | Digit+ '.'
233 ;
234
235fragment
236ExponentPart : ('e' | 'E') ('+' | '-')? Digit+ ;
237
238fragment
239FloatingSuffix : 'f' | 'l' | 'F' | 'L' ;
240
241fragment
242HexadecimalFloatingConstant
243 : HexPrefix HexFractionalConstant BinaryExponentPart
244 FloatingSuffix?
245 | HexPrefix HexadecimalDigit+ BinaryExponentPart
246 FloatingSuffix?
247 ;
248
249fragment
250HexFractionalConstant
251 : HexadecimalDigit* '.' Digit+
252 | HexadecimalDigit+ '.'
253 ;
254
255fragment
256BinaryExponentPart
257 : ('p' | 'P') ('+' | '-')? Digit+ ;
258
259
260/****** Preprocessing Numbers: C11 Sec 6.4.8 ******/
261
262/* PP_NUMBER should be anything that doesn't match the previous
263 * rules but does match this one.
264 */
265PP_NUMBER : '.'? Digit
266 ( '.'
267 | IdentifierNonDigit
268 | Digit
269 | ('e' | 'E' | 'p' | 'P') ('+' | '-')
270 )*
271 NotLineStart
272 ;
273
274
275/****** Sec. 6.4.4.4: Character Constants ******/
276
277CHARACTER_CONSTANT
278 : ('L' | 'U' | 'u')? '\'' CChar+ '\'' NotLineStart;
279
280fragment
281CChar : ~('\'' | '\\' | '\n') | EscapeSequence ;
282
283fragment
284EscapeSequence : '\\' ( '\'' | '"' | '\?' | '\\' |
285 'a' | 'b' | 'f' | 'n' |'r' | 't' | 'v'
286 )
287 | OctalEscape
288 | HexEscape
289 ;
290fragment
291OctalEscape : '\\' OctalDigit (OctalDigit OctalDigit?)? ;
292
293fragment
294OctalDigit : '0' .. '7';
295
296fragment
297HexEscape : '\\' 'x' HexadecimalDigit+ ;
298
299
300/****** 6.4.5: String Literals *****/
301
302
303STRING_LITERAL : ('u8' | 'u' | 'U' | 'L')? '"' SChar* '"'
304 NotLineStart
305 ;
306
307fragment
308SChar : ~('"' | '\\' | '\n') | EscapeSequence ;
309
310
311/****** Punctuators: C11 Sec. 6.4.6 ******/
312
313fragment ELLIPSIS :;
314
315DOT : '.'
316 ( ('..')=> '..' { $type = ELLIPSIS; }
317 |
318 )
319 NotLineStart
320 ;
321
322AMPERSAND : '&' NotLineStart;
323AND : '&&' NotLineStart;
324ARROW : '->' NotLineStart;
325ASSIGN : '=' NotLineStart;
326BITANDEQ : '&=' NotLineStart;
327BITOR : '|' NotLineStart;
328BITOREQ : '|=' NotLineStart;
329BITXOR : '^' NotLineStart;
330BITXOREQ : '^=' NotLineStart;
331COLON : ':' NotLineStart;
332COMMA : ',' NotLineStart;
333DIV : '/' NotLineStart;
334DIVEQ : '/=' NotLineStart;
335EQUALS : '==' NotLineStart;
336GT : '>' NotLineStart;
337GTE : '>=' NotLineStart;
338//HASH : '#' | '%:' NotLineStart;
339HASHHASH : '##' | '%:%:' NotLineStart;
340LCURLY : '{' | '<%' NotLineStart;
341LPAREN : '(' NotLineStart;
342LSQUARE : '[' | '<:' NotLineStart;
343LT : '<' NotLineStart;
344LTE : '<=' NotLineStart;
345MINUSMINUS : '--' NotLineStart;
346MOD : '%' NotLineStart;
347MODEQ : '%=' NotLineStart;
348NEQ : '!=' NotLineStart;
349NOT : '!' NotLineStart;
350OR : '||' NotLineStart;
351PLUS : '+' NotLineStart;
352PLUSEQ : '+=' NotLineStart;
353PLUSPLUS : '++' NotLineStart;
354QMARK : '?' NotLineStart;
355RCURLY : '}' | '%>' NotLineStart;
356RPAREN : ')' NotLineStart;
357RSQUARE : ']' | ':>' NotLineStart;
358SEMI : ';' NotLineStart;
359SHIFTLEFT : '<<' NotLineStart;
360SHIFTLEFTEQ : '<<=' NotLineStart;
361SHIFTRIGHT : '>>' NotLineStart;
362SHIFTRIGHTEQ : '>>=' NotLineStart;
363STAR : '*' NotLineStart;
364STAREQ : '*=' NotLineStart;
365SUB : '-' NotLineStart;
366SUBEQ : '-=' NotLineStart;
367TILDE : '~' NotLineStart;
368
369/****** Header Names: C11 Sec. 6.4.7 ******/
370
371HEADER_NAME : {inInclude}?=>
372 ( '"' (~('\n' | '"'))+ '"'
373 | '<' (~('\n' | '>'))+ '>'
374 )
375 {inInclude=false; atLineStart=false;}
376 ;
377
378
379/* ***** Comments: C11 Sec 6.4.9 ******/
380
381COMMENT : '//' ( options {greedy=true;} : ~('\n'|'\r') )*
382 | '/*' ( options {greedy=false;} : . )* '*/'
383 ;
384
385/****** Other characters: C11 Sec. 6.4 ******/
386
387OTHER : . NotLineStart;
Note: See TracBrowser for help on using the repository browser.