| [d96fdd6d] | 1 | lexer 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] | 16 | package edu.udel.cis.vsl.civl.civlc.preproc.common;
|
|---|
| [d96fdd6d] | 17 | }
|
|---|
| 18 |
|
|---|
| 19 | @members
|
|---|
| 20 | {
|
|---|
| 21 |
|
|---|
| 22 | public boolean inInclude = false; // are we inside a #include directive?
|
|---|
| 23 | public boolean inCondition = false; // are we inside a #if condition?
|
|---|
| 24 | public boolean atLineStart = true; // are we at start of line + possible WS?
|
|---|
| 25 |
|
|---|
| 26 | @Override
|
|---|
| 27 | public 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 |
|
|---|
| 36 | fragment
|
|---|
| 37 | NotLineStart : {atLineStart = false;} ;
|
|---|
| 38 |
|
|---|
| 39 | PDEFINE : {atLineStart}?=>WS* '#' WS* 'define' NotLineStart;
|
|---|
| 40 | PINCLUDE : {atLineStart}?=>WS* '#' WS* 'include'
|
|---|
| 41 | {inInclude = true; atLineStart=false;}
|
|---|
| 42 | ;
|
|---|
| 43 | PIFDEF : {atLineStart}?=>WS* '#' WS* 'ifdef' NotLineStart;
|
|---|
| 44 | PIFNDEF : {atLineStart}?=>WS* '#' WS* 'ifndef' NotLineStart;
|
|---|
| 45 | PIF : {atLineStart}?=>WS* '#' WS* 'if'
|
|---|
| 46 | {inCondition = true; atLineStart = false;}
|
|---|
| 47 | ;
|
|---|
| 48 | PENDIF : {atLineStart}?=>WS* '#' WS* 'endif' NotLineStart;
|
|---|
| 49 | PELIF : {atLineStart}?=>WS* '#' WS* 'elif'
|
|---|
| 50 | {inCondition = true; atLineStart = false;}
|
|---|
| 51 | ;
|
|---|
| 52 | PELSE : {atLineStart}?=>WS* '#' WS* 'else' NotLineStart;
|
|---|
| 53 | PRAGMA : {atLineStart}?=>WS* '#' WS* 'pragma' NotLineStart;
|
|---|
| 54 | PERROR : {atLineStart}?=>WS* '#' WS* 'error' NotLineStart;
|
|---|
| 55 | PUNDEF : {atLineStart}?=>WS* '#' WS* 'undef' NotLineStart;
|
|---|
| 56 | PLINE : {atLineStart}?=>WS* '#' WS* 'line' NotLineStart;
|
|---|
| 57 | //PBLANK : {atLineStart}?=>WS* '#' WS* NotLineStart;
|
|---|
| 58 | HASH : WS* '#' WS*;
|
|---|
| 59 | DEFINED : {inCondition}?=>'defined' NotLineStart;
|
|---|
| 60 |
|
|---|
| 61 | /****** White space ******/
|
|---|
| 62 |
|
|---|
| 63 | NEWLINE : NewLine;
|
|---|
| 64 |
|
|---|
| 65 | fragment
|
|---|
| 66 | NewLine : '\r'? '\n'
|
|---|
| 67 | {inCondition=false; atLineStart=true;};
|
|---|
| 68 |
|
|---|
| 69 | WS : (' ' | '\t')+;
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 | /****** C keywords, from C11 Sec. 6.4.1 ******/
|
|---|
| 73 |
|
|---|
| 74 | AUTO : 'auto';
|
|---|
| 75 | BREAK : 'break';
|
|---|
| 76 | CASE : 'case';
|
|---|
| 77 | CHAR : 'char';
|
|---|
| 78 | CONST : 'const';
|
|---|
| 79 | CONTINUE : 'continue';
|
|---|
| 80 | DEFAULT : 'default';
|
|---|
| 81 | DO : 'do';
|
|---|
| 82 | DOUBLE : 'double';
|
|---|
| 83 | ELSE : 'else';
|
|---|
| 84 | ENUM : 'enum';
|
|---|
| 85 | EXTERN : 'extern';
|
|---|
| 86 | FLOAT : 'float';
|
|---|
| 87 | FOR : 'for';
|
|---|
| 88 | GOTO : 'goto';
|
|---|
| 89 | IF : 'if';
|
|---|
| 90 | INLINE : 'inline';
|
|---|
| 91 | INT : 'int';
|
|---|
| 92 | LONG : 'long';
|
|---|
| 93 | REGISTER : 'register';
|
|---|
| 94 | RESTRICT : 'restrict';
|
|---|
| 95 | RETURN : 'return';
|
|---|
| 96 | SHORT : 'short';
|
|---|
| 97 | SIGNED : 'signed';
|
|---|
| 98 | SIZEOF : 'sizeof';
|
|---|
| 99 | STATIC : 'static';
|
|---|
| 100 | STRUCT : 'struct';
|
|---|
| 101 | SWITCH : 'switch';
|
|---|
| 102 | TYPEDEF : 'typedef';
|
|---|
| 103 | UNION : 'union';
|
|---|
| 104 | UNSIGNED : 'unsigned';
|
|---|
| 105 | VOID : 'void';
|
|---|
| 106 | VOLATILE : 'volatile';
|
|---|
| 107 | WHILE : 'while';
|
|---|
| 108 | ALIGNAS : '_Alignas';
|
|---|
| 109 | ALIGNOF : '_Alignof';
|
|---|
| 110 | ATOMIC : '_Atomic';
|
|---|
| 111 | BOOL : '_Bool';
|
|---|
| 112 | COMPLEX : '_Complex';
|
|---|
| 113 | GENERIC : '_Generic';
|
|---|
| 114 | IMAGINARY : '_Imaginary';
|
|---|
| 115 | NORETURN : '_Noreturn';
|
|---|
| 116 | STATICASSERT : '_Static_assert';
|
|---|
| 117 | THREADLOCAL : '_Thread_local';
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 | /* Additional keywords in CIVL-C */
|
|---|
| 121 |
|
|---|
| 122 | ASSERT : '_assert';
|
|---|
| 123 | ASSUME : '_assume';
|
|---|
| 124 | AT : '@';
|
|---|
| 125 | CHOOSE : '_choose';
|
|---|
| 126 | COLLECTIVE : '\\collective';
|
|---|
| 127 | INPUT : '_input';
|
|---|
| 128 | INVARIANT : '_invariant';
|
|---|
| 129 | OUTPUT : '_output';
|
|---|
| 130 | PROC : '\\proc';
|
|---|
| 131 | SPAWN : '_spawn';
|
|---|
| 132 | WAIT : '_wait';
|
|---|
| 133 | WHEN : '_when';
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 | /****** Identifiers: C11 Sec. 6.4.2 ******/
|
|---|
| 138 |
|
|---|
| 139 | IDENTIFIER : IdentifierNonDigit
|
|---|
| 140 | (IdentifierNonDigit | Digit)* NotLineStart
|
|---|
| 141 | ;
|
|---|
| 142 |
|
|---|
| 143 | fragment
|
|---|
| 144 | IdentifierNonDigit
|
|---|
| 145 | : NonDigit | UniversalCharacterName ;
|
|---|
| 146 |
|
|---|
| 147 | fragment
|
|---|
| 148 | Zero : '0' ;
|
|---|
| 149 |
|
|---|
| 150 | fragment
|
|---|
| 151 | Digit : Zero | NonZeroDigit ;
|
|---|
| 152 |
|
|---|
| 153 | fragment
|
|---|
| 154 | NonZeroDigit : '1' .. '9' ;
|
|---|
| 155 |
|
|---|
| 156 | fragment
|
|---|
| 157 | NonDigit : 'A'..'Z' | 'a'..'z' | '_' ;
|
|---|
| 158 |
|
|---|
| 159 | fragment
|
|---|
| 160 | UniversalCharacterName
|
|---|
| 161 | : '\\' 'u' HexQuad
|
|---|
| 162 | | '\\' 'U' HexQuad HexQuad
|
|---|
| 163 | ;
|
|---|
| 164 |
|
|---|
| 165 | fragment
|
|---|
| 166 | HexQuad : HexadecimalDigit HexadecimalDigit HexadecimalDigit HexadecimalDigit ;
|
|---|
| 167 |
|
|---|
| 168 | fragment
|
|---|
| 169 | HexadecimalDigit
|
|---|
| 170 | : '0'..'9' | 'a'..'f' | 'A'..'F' ;
|
|---|
| 171 |
|
|---|
| 172 | /****** Sec. 6.4.4.1: Integer constants ******/
|
|---|
| 173 |
|
|---|
| 174 | INTEGER_CONSTANT
|
|---|
| 175 | : DecimalConstant IntegerSuffix?
|
|---|
| 176 | | OctalConstant IntegerSuffix?
|
|---|
| 177 | | HexadecimalConstant IntegerSuffix?
|
|---|
| 178 | ;
|
|---|
| 179 |
|
|---|
| 180 | fragment
|
|---|
| 181 | DecimalConstant : NonZeroDigit Digit* ;
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 | fragment
|
|---|
| 185 | IntegerSuffix : UnsignedSuffix LongSuffix?
|
|---|
| 186 | | UnsignedSuffix LongLongSuffix
|
|---|
| 187 | | LongSuffix UnsignedSuffix?
|
|---|
| 188 | | LongLongSuffix UnsignedSuffix?
|
|---|
| 189 | ;
|
|---|
| 190 |
|
|---|
| 191 | fragment
|
|---|
| 192 | UnsignedSuffix : 'u' | 'U';
|
|---|
| 193 |
|
|---|
| 194 | fragment
|
|---|
| 195 | LongSuffix : 'l' | 'L';
|
|---|
| 196 |
|
|---|
| 197 | fragment
|
|---|
| 198 | LongLongSuffix : 'll' | 'LL';
|
|---|
| 199 |
|
|---|
| 200 | fragment
|
|---|
| 201 | OctalConstant : Zero OctalDigit* IntegerSuffix? NotLineStart;
|
|---|
| 202 |
|
|---|
| 203 | fragment
|
|---|
| 204 | HexadecimalConstant
|
|---|
| 205 | : HexPrefix HexadecimalDigit+ IntegerSuffix? NotLineStart;
|
|---|
| 206 |
|
|---|
| 207 | fragment
|
|---|
| 208 | HexPrefix : Zero ('x' | 'X') ;
|
|---|
| 209 |
|
|---|
| 210 | /****** Sec. 6.4.4.2: Floating Constants ******/
|
|---|
| 211 |
|
|---|
| 212 | FLOATING_CONSTANT
|
|---|
| 213 | : DecimalFloatingConstant
|
|---|
| 214 | | HexadecimalFloatingConstant
|
|---|
| 215 | ;
|
|---|
| 216 |
|
|---|
| 217 | fragment
|
|---|
| 218 | DecimalFloatingConstant
|
|---|
| 219 | : FractionalConstant ExponentPart? FloatingSuffix?
|
|---|
| 220 | | Digit+ ExponentPart FloatingSuffix?
|
|---|
| 221 | ;
|
|---|
| 222 |
|
|---|
| 223 | fragment
|
|---|
| 224 | FractionalConstant
|
|---|
| 225 | : Digit* '.' Digit+
|
|---|
| 226 | | Digit+ '.'
|
|---|
| 227 | ;
|
|---|
| 228 |
|
|---|
| 229 | fragment
|
|---|
| 230 | ExponentPart : ('e' | 'E') ('+' | '-')? Digit+ ;
|
|---|
| 231 |
|
|---|
| 232 | fragment
|
|---|
| 233 | FloatingSuffix : 'f' | 'l' | 'F' | 'L' ;
|
|---|
| 234 |
|
|---|
| 235 | fragment
|
|---|
| 236 | HexadecimalFloatingConstant
|
|---|
| 237 | : HexPrefix HexFractionalConstant BinaryExponentPart
|
|---|
| 238 | FloatingSuffix?
|
|---|
| 239 | | HexPrefix HexadecimalDigit+ BinaryExponentPart
|
|---|
| 240 | FloatingSuffix?
|
|---|
| 241 | ;
|
|---|
| 242 |
|
|---|
| 243 | fragment
|
|---|
| 244 | HexFractionalConstant
|
|---|
| 245 | : HexadecimalDigit* '.' Digit+
|
|---|
| 246 | | HexadecimalDigit+ '.'
|
|---|
| 247 | ;
|
|---|
| 248 |
|
|---|
| 249 | fragment
|
|---|
| 250 | BinaryExponentPart
|
|---|
| 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 | */
|
|---|
| 259 | PP_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 |
|
|---|
| 271 | CHARACTER_CONSTANT
|
|---|
| 272 | : ('L' | 'U' | 'u')? '\'' CChar+ '\'' NotLineStart;
|
|---|
| 273 |
|
|---|
| 274 | fragment
|
|---|
| 275 | CChar : ~('\'' | '\\' | '\n') | EscapeSequence ;
|
|---|
| 276 |
|
|---|
| 277 | fragment
|
|---|
| 278 | EscapeSequence : '\\' ( '\'' | '"' | '\?' | '\\' |
|
|---|
| 279 | 'a' | 'b' | 'f' | 'n' |'r' | 't' | 'v'
|
|---|
| 280 | )
|
|---|
| 281 | | OctalEscape
|
|---|
| 282 | | HexEscape
|
|---|
| 283 | ;
|
|---|
| 284 | fragment
|
|---|
| 285 | OctalEscape : '\\' OctalDigit (OctalDigit OctalDigit?)? ;
|
|---|
| 286 |
|
|---|
| 287 | fragment
|
|---|
| 288 | OctalDigit : '0' .. '7';
|
|---|
| 289 |
|
|---|
| 290 | fragment
|
|---|
| 291 | HexEscape : '\\' 'x' HexadecimalDigit+ ;
|
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 | /****** 6.4.5: String Literals *****/
|
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 | STRING_LITERAL : ('u8' | 'u' | 'U' | 'L')? '"' SChar* '"'
|
|---|
| 298 | NotLineStart
|
|---|
| 299 | ;
|
|---|
| 300 |
|
|---|
| 301 | fragment
|
|---|
| 302 | SChar : ~('"' | '\\' | '\n') | EscapeSequence ;
|
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 | /****** Punctuators: C11 Sec. 6.4.6 ******/
|
|---|
| 306 |
|
|---|
| 307 | fragment ELLIPSIS :;
|
|---|
| 308 |
|
|---|
| 309 | DOT : '.'
|
|---|
| 310 | ( ('..')=> '..' { $type = ELLIPSIS; }
|
|---|
| 311 | |
|
|---|
| 312 | )
|
|---|
| 313 | NotLineStart
|
|---|
| 314 | ;
|
|---|
| 315 |
|
|---|
| 316 | AMPERSAND : '&' NotLineStart;
|
|---|
| 317 | AND : '&&' NotLineStart;
|
|---|
| 318 | ARROW : '->' NotLineStart;
|
|---|
| 319 | ASSIGN : '=' NotLineStart;
|
|---|
| 320 | BITANDEQ : '&=' NotLineStart;
|
|---|
| 321 | BITOR : '|' NotLineStart;
|
|---|
| 322 | BITOREQ : '|=' NotLineStart;
|
|---|
| 323 | BITXOR : '^' NotLineStart;
|
|---|
| 324 | BITXOREQ : '^=' NotLineStart;
|
|---|
| 325 | COLON : ':' NotLineStart;
|
|---|
| 326 | COMMA : ',' NotLineStart;
|
|---|
| 327 | DIV : '/' NotLineStart;
|
|---|
| 328 | DIVEQ : '/=' NotLineStart;
|
|---|
| 329 | EQUALS : '==' NotLineStart;
|
|---|
| 330 | GT : '>' NotLineStart;
|
|---|
| 331 | GTE : '>=' NotLineStart;
|
|---|
| 332 | //HASH : '#' | '%:' NotLineStart;
|
|---|
| 333 | HASHHASH : '##' | '%:%:' NotLineStart;
|
|---|
| 334 | LCURLY : '{' | '<%' NotLineStart;
|
|---|
| 335 | LPAREN : '(' NotLineStart;
|
|---|
| 336 | LSQUARE : '[' | '<:' NotLineStart;
|
|---|
| 337 | LT : '<' NotLineStart;
|
|---|
| 338 | LTE : '<=' NotLineStart;
|
|---|
| 339 | MINUSMINUS : '--' NotLineStart;
|
|---|
| 340 | MOD : '%' NotLineStart;
|
|---|
| 341 | MODEQ : '%=' NotLineStart;
|
|---|
| 342 | NEQ : '!=' NotLineStart;
|
|---|
| 343 | NOT : '!' NotLineStart;
|
|---|
| 344 | OR : '||' NotLineStart;
|
|---|
| 345 | PLUS : '+' NotLineStart;
|
|---|
| 346 | PLUSEQ : '+=' NotLineStart;
|
|---|
| 347 | PLUSPLUS : '++' NotLineStart;
|
|---|
| 348 | QMARK : '?' NotLineStart;
|
|---|
| 349 | RCURLY : '}' | '%>' NotLineStart;
|
|---|
| 350 | RPAREN : ')' NotLineStart;
|
|---|
| 351 | RSQUARE : ']' | ':>' NotLineStart;
|
|---|
| 352 | SEMI : ';' NotLineStart;
|
|---|
| 353 | SHIFTLEFT : '<<' NotLineStart;
|
|---|
| 354 | SHIFTLEFTEQ : '<<=' NotLineStart;
|
|---|
| 355 | SHIFTRIGHT : '>>' NotLineStart;
|
|---|
| 356 | SHIFTRIGHTEQ : '>>=' NotLineStart;
|
|---|
| 357 | STAR : '*' NotLineStart;
|
|---|
| 358 | STAREQ : '*=' NotLineStart;
|
|---|
| 359 | SUB : '-' NotLineStart;
|
|---|
| 360 | SUBEQ : '-=' NotLineStart;
|
|---|
| 361 | TILDE : '~' NotLineStart;
|
|---|
| 362 |
|
|---|
| 363 | /****** Header Names: C11 Sec. 6.4.7 ******/
|
|---|
| 364 |
|
|---|
| 365 | HEADER_NAME : {inInclude}?=>
|
|---|
| 366 | ( '"' (~('\n' | '"'))+ '"'
|
|---|
| 367 | | '<' (~('\n' | '>'))+ '>'
|
|---|
| 368 | )
|
|---|
| 369 | {inInclude=false; atLineStart=false;}
|
|---|
| 370 | ;
|
|---|
| 371 |
|
|---|
| 372 |
|
|---|
| 373 | /* ***** Comments: C11 Sec 6.4.9 ******/
|
|---|
| 374 |
|
|---|
| 375 | COMMENT : '//' ( options {greedy=true;} : ~('\n'|'\r') )*
|
|---|
| 376 | | '/*' ( options {greedy=false;} : . )* '*/'
|
|---|
| 377 | ;
|
|---|
| 378 |
|
|---|
| 379 | /****** Other characters: C11 Sec. 6.4 ******/
|
|---|
| 380 |
|
|---|
| 381 | OTHER : . NotLineStart;
|
|---|