| 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 | * This lexer grammar will not contain C keywords and ones for
|
|---|
| 14 | * CIVL-C, ACSL, GNU and CUDA extensions of C.
|
|---|
| 15 | * Those keywords are defined in
|
|---|
| 16 | * dev.civl.abc.front.c.parse.PP2CivlcTokenCConverter
|
|---|
| 17 | * A private function named as `initCKeywordMap` shall identify
|
|---|
| 18 | * and set tokens in proprocessed streams as its corresponding
|
|---|
| 19 | * token-types.
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | @header
|
|---|
| 23 | {
|
|---|
| 24 | package dev.civl.abc.front.c.preproc;
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | @members
|
|---|
| 28 | {
|
|---|
| 29 | @Override
|
|---|
| 30 | public void emitErrorMessage(String msg) { // don't try to recover!
|
|---|
| 31 | throw new RuntimeException(msg);
|
|---|
| 32 | }
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | /****** White space ******/
|
|---|
| 36 | NEWLINE : '\r'? '\n' ;
|
|---|
| 37 | WS : ' ' | '\t' ;
|
|---|
| 38 |
|
|---|
| 39 | /* Words that are used in both C and the preprocessor */
|
|---|
| 40 | IF : 'if' ;
|
|---|
| 41 | ELSE : 'else' ;
|
|---|
| 42 |
|
|---|
| 43 | /* Words used in preprocessor but not in C */
|
|---|
| 44 | DEFINE : 'define' ;
|
|---|
| 45 | DEFINED : 'defined' ;
|
|---|
| 46 | ELIF : 'elif' ;
|
|---|
| 47 | ENDIF : 'endif' ;
|
|---|
| 48 | ERROR : 'error' ;
|
|---|
| 49 | IFDEF : 'ifdef' ;
|
|---|
| 50 | IFNDEF : 'ifndef' ;
|
|---|
| 51 | INCLUDE : 'include' ;
|
|---|
| 52 | LINE : 'line' ;
|
|---|
| 53 | PRAGMA : 'pragma' ;
|
|---|
| 54 | UNDEF : 'undef' ;
|
|---|
| 55 |
|
|---|
| 56 | /****** Punctuators: C11 Sec. 6.4.6 ******/
|
|---|
| 57 | ELLIPSIS : '...' ;
|
|---|
| 58 | DOTDOT : '..' ;
|
|---|
| 59 | DOT : '.' ;
|
|---|
| 60 | AMPERSAND : '&' ;
|
|---|
| 61 | AND : '&&' ;
|
|---|
| 62 | ARROW : '->' ;
|
|---|
| 63 | ASSIGN : '=' ;
|
|---|
| 64 | BITANDEQ : '&=' ;
|
|---|
| 65 | BITOR : '|' ;
|
|---|
| 66 | BITOREQ : '|=' ;
|
|---|
| 67 | BITXOR : '^' ;
|
|---|
| 68 | BITXOREQ : '^=' ;
|
|---|
| 69 | COLON : ':' ;
|
|---|
| 70 | COMMA : ',' ;
|
|---|
| 71 | DIV : '/' ;
|
|---|
| 72 | DIVEQ : '/=' ;
|
|---|
| 73 | EQUALS : '==' ;
|
|---|
| 74 | GT : '>' ;
|
|---|
| 75 | GTE : '>=' ;
|
|---|
| 76 | HASH : '#' | '%:' ;
|
|---|
| 77 | HASHHASH : '##' | '%:%:' ;
|
|---|
| 78 | LCURLY : '{' | '<%' ;
|
|---|
| 79 | LPAREN : '(' ;
|
|---|
| 80 | LSQUARE : '[' | '<:' ;
|
|---|
| 81 | LT : '<' ;
|
|---|
| 82 | LTE : '<=' ;
|
|---|
| 83 | MINUSMINUS : '--' ;
|
|---|
| 84 | MOD : '%' ;
|
|---|
| 85 | MODEQ : '%=' ;
|
|---|
| 86 | NEQ : '!=' ;
|
|---|
| 87 | NOT : '!' ;
|
|---|
| 88 | OR : '||' ;
|
|---|
| 89 | PLUS : '+' ;
|
|---|
| 90 | PLUSEQ : '+=' ;
|
|---|
| 91 | PLUSPLUS : '++' ;
|
|---|
| 92 | QMARK : '?' ;
|
|---|
| 93 | RCURLY : '}' | '%>' ;
|
|---|
| 94 | RPAREN : ')' ;
|
|---|
| 95 | RSQUARE : ']' | ':>' ;
|
|---|
| 96 | SEMI : ';' ;
|
|---|
| 97 | SHIFTLEFT : '<<' ;
|
|---|
| 98 | SHIFTLEFTEQ : '<<=' ;
|
|---|
| 99 | SHIFTRIGHT : '>>' ;
|
|---|
| 100 | SHIFTRIGHTEQ : '>>=' ;
|
|---|
| 101 | STAR : '*' ;
|
|---|
| 102 | STAREQ : '*=' ;
|
|---|
| 103 | SUB : '-' ;
|
|---|
| 104 | SUBEQ : '-=' ;
|
|---|
| 105 | TILDE : '~' ;
|
|---|
| 106 |
|
|---|
| 107 | /* CIVL-C and ACSL Punctuators */
|
|---|
| 108 | ANNOTATION_START : '/*@' ;
|
|---|
| 109 | ANNOTATION_END : '*/' ;
|
|---|
| 110 | AT : '@' ;
|
|---|
| 111 | EQUIV_ACSL : '<==>' ;
|
|---|
| 112 | IMPLIES : '=>' ;
|
|---|
| 113 | IMPLIES_ACSL : '==>' ;
|
|---|
| 114 | INLINE_ANNOTATION_START : '//@' ;
|
|---|
| 115 | // LSLIST and RSLIST enclose a scope list
|
|---|
| 116 | LSLIST : '<|' ;
|
|---|
| 117 | RSLIST : '|>' ;
|
|---|
| 118 | XOR_ACSL : '^^' ;
|
|---|
| 119 |
|
|---|
| 120 | /* CUDA Punctuators */
|
|---|
| 121 | LEXCON : '<<<' ;
|
|---|
| 122 | REXCON : '>>>' ;
|
|---|
| 123 |
|
|---|
| 124 | /****** Identifiers: C11 Sec. 6.4.2 ******/
|
|---|
| 125 | IDENTIFIER : IdentifierNonDigit
|
|---|
| 126 | (IdentifierNonDigit | Digit)*
|
|---|
| 127 | ;
|
|---|
| 128 |
|
|---|
| 129 | fragment
|
|---|
| 130 | IdentifierNonDigit
|
|---|
| 131 | : NonDigit | UniversalCharacterName ;
|
|---|
| 132 |
|
|---|
| 133 | fragment
|
|---|
| 134 | Zero : '0' ;
|
|---|
| 135 |
|
|---|
| 136 | fragment
|
|---|
| 137 | Digit : Zero | NonZeroDigit ;
|
|---|
| 138 |
|
|---|
| 139 | fragment
|
|---|
| 140 | NonZeroDigit : '1' .. '9' ;
|
|---|
| 141 |
|
|---|
| 142 | fragment
|
|---|
| 143 | NonDigit : 'A'..'Z' | 'a'..'z' | '_' | '$';
|
|---|
| 144 |
|
|---|
| 145 | fragment
|
|---|
| 146 | UniversalCharacterName
|
|---|
| 147 | : '\\' 'u' HexQuad
|
|---|
| 148 | | '\\' 'U' HexQuad HexQuad
|
|---|
| 149 | ;
|
|---|
| 150 |
|
|---|
| 151 | fragment
|
|---|
| 152 | HexQuad : HexadecimalDigit HexadecimalDigit HexadecimalDigit HexadecimalDigit ;
|
|---|
| 153 |
|
|---|
| 154 | fragment
|
|---|
| 155 | HexadecimalDigit
|
|---|
| 156 | : '0'..'9' | 'a'..'f' | 'A'..'F' ;
|
|---|
| 157 |
|
|---|
| 158 | /****** Sec. 6.4.4.1: Integer constants ******/
|
|---|
| 159 | INTEGER_CONSTANT
|
|---|
| 160 | : DecimalConstant IntegerSuffix?
|
|---|
| 161 | | OctalConstant IntegerSuffix?
|
|---|
| 162 | | HexadecimalConstant IntegerSuffix?
|
|---|
| 163 | ;
|
|---|
| 164 |
|
|---|
| 165 | fragment
|
|---|
| 166 | DecimalConstant : NonZeroDigit Digit* ;
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 | fragment
|
|---|
| 170 | IntegerSuffix : UnsignedSuffix LongSuffix?
|
|---|
| 171 | | UnsignedSuffix LongLongSuffix
|
|---|
| 172 | | LongSuffix UnsignedSuffix?
|
|---|
| 173 | | LongLongSuffix UnsignedSuffix?
|
|---|
| 174 | ;
|
|---|
| 175 |
|
|---|
| 176 | fragment
|
|---|
| 177 | UnsignedSuffix : 'u' | 'U' ;
|
|---|
| 178 |
|
|---|
| 179 | fragment
|
|---|
| 180 | LongSuffix : 'l' | 'L' ;
|
|---|
| 181 |
|
|---|
| 182 | fragment
|
|---|
| 183 | LongLongSuffix : 'll' | 'LL' ;
|
|---|
| 184 |
|
|---|
| 185 | fragment
|
|---|
| 186 | OctalConstant : Zero OctalDigit* IntegerSuffix? ;
|
|---|
| 187 |
|
|---|
| 188 | fragment
|
|---|
| 189 | HexadecimalConstant
|
|---|
| 190 | : HexPrefix HexadecimalDigit+ IntegerSuffix? ;
|
|---|
| 191 |
|
|---|
| 192 | fragment
|
|---|
| 193 | HexPrefix : Zero ('x' | 'X') ;
|
|---|
| 194 |
|
|---|
| 195 | /****** Sec. 6.4.4.2: Floating Constants ******/
|
|---|
| 196 |
|
|---|
| 197 | FLOATING_CONSTANT
|
|---|
| 198 | : DecimalFloatingConstant
|
|---|
| 199 | | HexadecimalFloatingConstant
|
|---|
| 200 | ;
|
|---|
| 201 |
|
|---|
| 202 | fragment
|
|---|
| 203 | DecimalFloatingConstant
|
|---|
| 204 | : FractionalConstant ExponentPart? FloatingSuffix?
|
|---|
| 205 | | Digit+ ExponentPart FloatingSuffix?
|
|---|
| 206 | ;
|
|---|
| 207 |
|
|---|
| 208 | fragment
|
|---|
| 209 | FractionalConstant
|
|---|
| 210 | : Digit* DOT Digit+
|
|---|
| 211 | | Digit+ DOT
|
|---|
| 212 | ;
|
|---|
| 213 |
|
|---|
| 214 | fragment
|
|---|
| 215 | ExponentPart : ('e' | 'E') ('+' | '-')? Digit+ ;
|
|---|
| 216 |
|
|---|
| 217 | fragment
|
|---|
| 218 | FloatingSuffix : 'f' | 'l' | 'F' | 'L' ;
|
|---|
| 219 |
|
|---|
| 220 | fragment
|
|---|
| 221 | HexadecimalFloatingConstant
|
|---|
| 222 | : HexPrefix HexFractionalConstant BinaryExponentPart
|
|---|
| 223 | FloatingSuffix?
|
|---|
| 224 | | HexPrefix HexadecimalDigit+ BinaryExponentPart
|
|---|
| 225 | FloatingSuffix?
|
|---|
| 226 | ;
|
|---|
| 227 |
|
|---|
| 228 | fragment
|
|---|
| 229 | HexFractionalConstant
|
|---|
| 230 | : HexadecimalDigit* DOT HexadecimalDigit+
|
|---|
| 231 | | HexadecimalDigit+ DOT
|
|---|
| 232 | ;
|
|---|
| 233 |
|
|---|
| 234 | fragment
|
|---|
| 235 | BinaryExponentPart
|
|---|
| 236 | : ('p' | 'P') ('+' | '-')? Digit+ ;
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 | /****** Preprocessing Numbers: C11 Sec 6.4.8 ******/
|
|---|
| 240 |
|
|---|
| 241 | /* PP_NUMBER should be anything that doesn't match the previous
|
|---|
| 242 | * rules but does match this one.
|
|---|
| 243 | */
|
|---|
| 244 | PP_NUMBER : '.'? Digit
|
|---|
| 245 | ( '.'
|
|---|
| 246 | | IdentifierNonDigit
|
|---|
| 247 | | Digit
|
|---|
| 248 | | ('e' | 'E' | 'p' | 'P') ('+' | '-')
|
|---|
| 249 | )*
|
|---|
| 250 | ;
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 | /****** Sec. 6.4.4.4: Character Constants ******/
|
|---|
| 254 |
|
|---|
| 255 | CHARACTER_CONSTANT
|
|---|
| 256 | : ('L' | 'U' | 'u')? '\'' CChar+ '\'' ;
|
|---|
| 257 |
|
|---|
| 258 | fragment
|
|---|
| 259 | CChar : ~('\'' | '\\' | '\n') | EscapeSequence ;
|
|---|
| 260 |
|
|---|
| 261 | fragment
|
|---|
| 262 | EscapeSequence : '\\' ( '\'' | '"' | '\?' | '\\' |
|
|---|
| 263 | 'a' | 'b' | 'f' | 'n' |'r' | 't' | 'v'
|
|---|
| 264 | )
|
|---|
| 265 | | OctalEscape
|
|---|
| 266 | | HexEscape
|
|---|
| 267 | ;
|
|---|
| 268 | fragment
|
|---|
| 269 | OctalEscape : '\\' OctalDigit (OctalDigit OctalDigit?)? ;
|
|---|
| 270 |
|
|---|
| 271 | fragment
|
|---|
| 272 | OctalDigit : '0' .. '7';
|
|---|
| 273 |
|
|---|
| 274 | fragment
|
|---|
| 275 | HexEscape : '\\' 'x' HexadecimalDigit+ ;
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 | /****** 6.4.5: String Literals *****/
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 | STRING_LITERAL : ('u8' | 'u' | 'U' | 'L')? '"' SChar* '"'
|
|---|
| 282 | ;
|
|---|
| 283 |
|
|---|
| 284 | fragment
|
|---|
| 285 | SChar : ~('"' | '\\' | '\n') | EscapeSequence ;
|
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 | /* ***** Comments: C11 Sec 6.4.9 ******/
|
|---|
| 290 |
|
|---|
| 291 | // the following is not quite perfect because in the case of the \n or \r
|
|---|
| 292 | // immediately following the // it counts that white space as part of the
|
|---|
| 293 | // comment, otherwise it doesn't. Would like to make the \n or \r NOT
|
|---|
| 294 | // part of the comment always, but how --- need to look ahead one character?
|
|---|
| 295 |
|
|---|
| 296 | fragment
|
|---|
| 297 | INLINE_COMMENT : '//'
|
|---|
| 298 | ( (~('@' | '\n' | '\r') ( options {greedy=true;} : ~('\n'|'\r') )*)
|
|---|
| 299 | | NEWLINE
|
|---|
| 300 | | EOF
|
|---|
| 301 | )
|
|---|
| 302 | ;
|
|---|
| 303 |
|
|---|
| 304 | fragment
|
|---|
| 305 | BLOCK_COMMENT : '/*'
|
|---|
| 306 | ( '*/' | ~('@') ( options {greedy=false;} : . )* '*/')
|
|---|
| 307 | ;
|
|---|
| 308 |
|
|---|
| 309 | COMMENT : INLINE_COMMENT | BLOCK_COMMENT ;
|
|---|
| 310 |
|
|---|
| 311 | /* Special keywords starting with backslash reserved for extensions
|
|---|
| 312 | * such as ACSL */
|
|---|
| 313 | EXTENDED_IDENTIFIER
|
|---|
| 314 | :
|
|---|
| 315 | '\\' IdentifierNonDigit (IdentifierNonDigit | Digit)*
|
|---|
| 316 | ;
|
|---|
| 317 |
|
|---|
| 318 | /****** Other characters: C11 Sec. 6.4 ******/
|
|---|
| 319 | OTHER : . ;
|
|---|