source: CIVL/grammar/PreprocessorLexer.g@ af8a6cb

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

Cleaning up build for grammars.

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

  • Property mode set to 100644
File size: 8.0 KB
Line 
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{
16package edu.udel.cis.vsl.civl.civlc.preproc.common;
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
120/* Additional keywords in CIVL-C */
121
122ASSERT : '_assert';
123ASSUME : '_assume';
124AT : '@';
125CHOOSE : '_choose';
126COLLECTIVE : '\\collective';
127INPUT : '_input';
128INVARIANT : '_invariant';
129OUTPUT : '_output';
130PROC : '\\proc';
131SPAWN : '_spawn';
132WAIT : '_wait';
133WHEN : '_when';
134
135
136
137/****** Identifiers: C11 Sec. 6.4.2 ******/
138
139IDENTIFIER : IdentifierNonDigit
140 (IdentifierNonDigit | Digit)* NotLineStart
141 ;
142
143fragment
144IdentifierNonDigit
145 : NonDigit | UniversalCharacterName ;
146
147fragment
148Zero : '0' ;
149
150fragment
151Digit : Zero | NonZeroDigit ;
152
153fragment
154NonZeroDigit : '1' .. '9' ;
155
156fragment
157NonDigit : 'A'..'Z' | 'a'..'z' | '_' ;
158
159fragment
160UniversalCharacterName
161 : '\\' 'u' HexQuad
162 | '\\' 'U' HexQuad HexQuad
163 ;
164
165fragment
166HexQuad : HexadecimalDigit HexadecimalDigit HexadecimalDigit HexadecimalDigit ;
167
168fragment
169HexadecimalDigit
170 : '0'..'9' | 'a'..'f' | 'A'..'F' ;
171
172/****** Sec. 6.4.4.1: Integer constants ******/
173
174INTEGER_CONSTANT
175 : DecimalConstant IntegerSuffix?
176 | OctalConstant IntegerSuffix?
177 | HexadecimalConstant IntegerSuffix?
178 ;
179
180fragment
181DecimalConstant : NonZeroDigit Digit* ;
182
183
184fragment
185IntegerSuffix : UnsignedSuffix LongSuffix?
186 | UnsignedSuffix LongLongSuffix
187 | LongSuffix UnsignedSuffix?
188 | LongLongSuffix UnsignedSuffix?
189 ;
190
191fragment
192UnsignedSuffix : 'u' | 'U';
193
194fragment
195LongSuffix : 'l' | 'L';
196
197fragment
198LongLongSuffix : 'll' | 'LL';
199
200fragment
201OctalConstant : Zero OctalDigit* IntegerSuffix? NotLineStart;
202
203fragment
204HexadecimalConstant
205 : HexPrefix HexadecimalDigit+ IntegerSuffix? NotLineStart;
206
207fragment
208HexPrefix : Zero ('x' | 'X') ;
209
210/****** Sec. 6.4.4.2: Floating Constants ******/
211
212FLOATING_CONSTANT
213 : DecimalFloatingConstant
214 | HexadecimalFloatingConstant
215 ;
216
217fragment
218DecimalFloatingConstant
219 : FractionalConstant ExponentPart? FloatingSuffix?
220 | Digit+ ExponentPart FloatingSuffix?
221 ;
222
223fragment
224FractionalConstant
225 : Digit* '.' Digit+
226 | Digit+ '.'
227 ;
228
229fragment
230ExponentPart : ('e' | 'E') ('+' | '-')? Digit+ ;
231
232fragment
233FloatingSuffix : 'f' | 'l' | 'F' | 'L' ;
234
235fragment
236HexadecimalFloatingConstant
237 : HexPrefix HexFractionalConstant BinaryExponentPart
238 FloatingSuffix?
239 | HexPrefix HexadecimalDigit+ BinaryExponentPart
240 FloatingSuffix?
241 ;
242
243fragment
244HexFractionalConstant
245 : HexadecimalDigit* '.' Digit+
246 | HexadecimalDigit+ '.'
247 ;
248
249fragment
250BinaryExponentPart
251 : ('p' | 'P') ('+' | '-')? Digit+ ;
252
253
254/****** Preprocessing Numbers: C11 Sec 6.4.8 ******/
255
256/* PP_NUMBER should be anything that doesn't match the previous
257 * rules but does match this one.
258 */
259PP_NUMBER : '.'? Digit
260 ( '.'
261 | IdentifierNonDigit
262 | Digit
263 | ('e' | 'E' | 'p' | 'P') ('+' | '-')
264 )*
265 NotLineStart
266 ;
267
268
269/****** Sec. 6.4.4.4: Character Constants ******/
270
271CHARACTER_CONSTANT
272 : ('L' | 'U' | 'u')? '\'' CChar+ '\'' NotLineStart;
273
274fragment
275CChar : ~('\'' | '\\' | '\n') | EscapeSequence ;
276
277fragment
278EscapeSequence : '\\' ( '\'' | '"' | '\?' | '\\' |
279 'a' | 'b' | 'f' | 'n' |'r' | 't' | 'v'
280 )
281 | OctalEscape
282 | HexEscape
283 ;
284fragment
285OctalEscape : '\\' OctalDigit (OctalDigit OctalDigit?)? ;
286
287fragment
288OctalDigit : '0' .. '7';
289
290fragment
291HexEscape : '\\' 'x' HexadecimalDigit+ ;
292
293
294/****** 6.4.5: String Literals *****/
295
296
297STRING_LITERAL : ('u8' | 'u' | 'U' | 'L')? '"' SChar* '"'
298 NotLineStart
299 ;
300
301fragment
302SChar : ~('"' | '\\' | '\n') | EscapeSequence ;
303
304
305/****** Punctuators: C11 Sec. 6.4.6 ******/
306
307fragment ELLIPSIS :;
308
309DOT : '.'
310 ( ('..')=> '..' { $type = ELLIPSIS; }
311 |
312 )
313 NotLineStart
314 ;
315
316AMPERSAND : '&' NotLineStart;
317AND : '&&' NotLineStart;
318ARROW : '->' NotLineStart;
319ASSIGN : '=' NotLineStart;
320BITANDEQ : '&=' NotLineStart;
321BITOR : '|' NotLineStart;
322BITOREQ : '|=' NotLineStart;
323BITXOR : '^' NotLineStart;
324BITXOREQ : '^=' NotLineStart;
325COLON : ':' NotLineStart;
326COMMA : ',' NotLineStart;
327DIV : '/' NotLineStart;
328DIVEQ : '/=' NotLineStart;
329EQUALS : '==' NotLineStart;
330GT : '>' NotLineStart;
331GTE : '>=' NotLineStart;
332//HASH : '#' | '%:' NotLineStart;
333HASHHASH : '##' | '%:%:' NotLineStart;
334LCURLY : '{' | '<%' NotLineStart;
335LPAREN : '(' NotLineStart;
336LSQUARE : '[' | '<:' NotLineStart;
337LT : '<' NotLineStart;
338LTE : '<=' NotLineStart;
339MINUSMINUS : '--' NotLineStart;
340MOD : '%' NotLineStart;
341MODEQ : '%=' NotLineStart;
342NEQ : '!=' NotLineStart;
343NOT : '!' NotLineStart;
344OR : '||' NotLineStart;
345PLUS : '+' NotLineStart;
346PLUSEQ : '+=' NotLineStart;
347PLUSPLUS : '++' NotLineStart;
348QMARK : '?' NotLineStart;
349RCURLY : '}' | '%>' NotLineStart;
350RPAREN : ')' NotLineStart;
351RSQUARE : ']' | ':>' NotLineStart;
352SEMI : ';' NotLineStart;
353SHIFTLEFT : '<<' NotLineStart;
354SHIFTLEFTEQ : '<<=' NotLineStart;
355SHIFTRIGHT : '>>' NotLineStart;
356SHIFTRIGHTEQ : '>>=' NotLineStart;
357STAR : '*' NotLineStart;
358STAREQ : '*=' NotLineStart;
359SUB : '-' NotLineStart;
360SUBEQ : '-=' NotLineStart;
361TILDE : '~' NotLineStart;
362
363/****** Header Names: C11 Sec. 6.4.7 ******/
364
365HEADER_NAME : {inInclude}?=>
366 ( '"' (~('\n' | '"'))+ '"'
367 | '<' (~('\n' | '>'))+ '>'
368 )
369 {inInclude=false; atLineStart=false;}
370 ;
371
372
373/* ***** Comments: C11 Sec 6.4.9 ******/
374
375COMMENT : '//' ( options {greedy=true;} : ~('\n'|'\r') )*
376 | '/*' ( options {greedy=false;} : . )* '*/'
377 ;
378
379/****** Other characters: C11 Sec. 6.4 ******/
380
381OTHER : . NotLineStart;
Note: See TracBrowser for help on using the repository browser.