[b87a5ed] | 1 | /* |
---|
| 2 | * Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | * |
---|
| 4 | * The contents of this file are covered under the licence agreement in the |
---|
| 5 | * file "LICENCE" distributed with Cforall. |
---|
[8f60f0b] | 6 | * |
---|
[6016c87] | 7 | * lex.ll -- |
---|
[8f60f0b] | 8 | * |
---|
[51b7345] | 9 | * Author : Peter A. Buhr |
---|
| 10 | * Created On : Sat Sep 22 08:58:10 2001 |
---|
[926af74] | 11 | * Last Modified By : Peter A. Buhr |
---|
[5b2edbc] | 12 | * Last Modified On : Tue Aug 22 22:43:39 2017 |
---|
| 13 | * Update Count : 558 |
---|
[51b7345] | 14 | */ |
---|
| 15 | |
---|
| 16 | %option yylineno |
---|
[4e9c7c1] | 17 | %option noyywrap |
---|
[5f2f2d7] | 18 | %option nounput |
---|
[51b7345] | 19 | |
---|
| 20 | %{ |
---|
[de62360d] | 21 | // This lexer assumes the program has been preprocessed by cpp. Hence, all user level preprocessor directive have been |
---|
| 22 | // performed and removed from the source. The only exceptions are preprocessor directives passed to the compiler (e.g., |
---|
| 23 | // line-number directives) and C/C++ style comments, which are ignored. |
---|
[51b7345] | 24 | |
---|
[8c17ab0] | 25 | //**************************** Includes and Defines **************************** |
---|
[51b7345] | 26 | |
---|
| 27 | #include <string> |
---|
[f487962] | 28 | #include <cstdio> // FILENAME_MAX |
---|
[51b7345] | 29 | |
---|
[984dce6] | 30 | #include "ParseNode.h" |
---|
| 31 | #include "TypedefTable.h" |
---|
[51b7345] | 32 | |
---|
| 33 | char *yyfilename; |
---|
[b87a5ed] | 34 | std::string *strtext; // accumulate parts of character and string constant value |
---|
[51b7345] | 35 | |
---|
[de62360d] | 36 | #define RETURN_LOCN(x) yylval.tok.loc.file = yyfilename; yylval.tok.loc.line = yylineno; return( x ) |
---|
| 37 | #define RETURN_VAL(x) yylval.tok.str = new std::string( yytext ); RETURN_LOCN( x ) |
---|
[926af74] | 38 | #define RETURN_CHAR(x) yylval.tok.str = nullptr; RETURN_LOCN( x ) |
---|
[de62360d] | 39 | #define RETURN_STR(x) yylval.tok.str = strtext; RETURN_LOCN( x ) |
---|
[5f2f2d7] | 40 | |
---|
[f487962] | 41 | #define WHITE_RETURN(x) // do nothing |
---|
[de62360d] | 42 | #define NEWLINE_RETURN() WHITE_RETURN( '\n' ) |
---|
| 43 | #define ASCIIOP_RETURN() RETURN_CHAR( (int)yytext[0] ) // single character operator |
---|
[f487962] | 44 | #define NAMEDOP_RETURN(x) RETURN_CHAR( x ) // multichar operator, with a name |
---|
[de62360d] | 45 | #define NUMERIC_RETURN(x) rm_underscore(); RETURN_VAL( x ) // numeric constant |
---|
| 46 | #define KEYWORD_RETURN(x) RETURN_CHAR( x ) // keyword |
---|
[5b2edbc] | 47 | #define QKEYWORD_RETURN(x) typedefTable.isKind( yytext ); RETURN_VAL(x); // quasi-keyword |
---|
[984dce6] | 48 | #define IDENTIFIER_RETURN() RETURN_VAL( typedefTable.isKind( yytext ) ) |
---|
[de62360d] | 49 | #define ATTRIBUTE_RETURN() RETURN_VAL( ATTR_IDENTIFIER ) |
---|
[51b7345] | 50 | |
---|
[3848e0e] | 51 | void rm_underscore() { |
---|
[e7aed49] | 52 | // Remove underscores in numeric constant by copying the non-underscore characters to the front of the string. |
---|
| 53 | yyleng = 0; |
---|
[b87a5ed] | 54 | for ( int i = 0; yytext[i] != '\0'; i += 1 ) { |
---|
| 55 | if ( yytext[i] != '_' ) { |
---|
[e7aed49] | 56 | yytext[yyleng] = yytext[i]; |
---|
| 57 | yyleng += 1; |
---|
[b87a5ed] | 58 | } // if |
---|
| 59 | } // for |
---|
| 60 | yytext[yyleng] = '\0'; |
---|
[51b7345] | 61 | } |
---|
| 62 | |
---|
[7812f1d1] | 63 | // Stop warning due to incorrectly generated flex code. |
---|
| 64 | #pragma GCC diagnostic ignored "-Wsign-compare" |
---|
[51b7345] | 65 | %} |
---|
| 66 | |
---|
| 67 | octal [0-7] |
---|
| 68 | nonzero [1-9] |
---|
| 69 | decimal [0-9] |
---|
| 70 | hex [0-9a-fA-F] |
---|
[3848e0e] | 71 | universal_char "\\"((u"_"?{hex_quad})|(U"_"?{hex_quad}{2})) |
---|
[51b7345] | 72 | |
---|
[b87a5ed] | 73 | // identifier, GCC: $ in identifier |
---|
[51b7345] | 74 | identifier ([a-zA-Z_$]|{universal_char})([0-9a-zA-Z_$]|{universal_char})* |
---|
| 75 | |
---|
[b87a5ed] | 76 | // attribute identifier, GCC: $ in identifier |
---|
[51b7345] | 77 | attr_identifier "@"{identifier} |
---|
| 78 | |
---|
[b87a5ed] | 79 | // numeric constants, CFA: '_' in constant |
---|
[3848e0e] | 80 | hex_quad {hex}("_"?{hex}){3} |
---|
[ba2356b] | 81 | integer_suffix "_"?(([uU](("ll"|"LL"|[lL])[iI]|[iI]?("ll"|"LL"|[lL])?))|([iI](("ll"|"LL"|[lL])[uU]|[uU]?("ll"|"LL"|[lL])?))|(("ll"|"LL"|[lL])([iI][uU]|[uU]?[iI]?))) |
---|
[51b7345] | 82 | |
---|
| 83 | octal_digits ({octal})|({octal}({octal}|"_")*{octal}) |
---|
| 84 | octal_prefix "0""_"? |
---|
| 85 | octal_constant (("0")|({octal_prefix}{octal_digits})){integer_suffix}? |
---|
| 86 | |
---|
| 87 | nonzero_digits ({nonzero})|({nonzero}({decimal}|"_")*{decimal}) |
---|
| 88 | decimal_constant {nonzero_digits}{integer_suffix}? |
---|
| 89 | |
---|
| 90 | hex_digits ({hex})|({hex}({hex}|"_")*{hex}) |
---|
| 91 | hex_prefix "0"[xX]"_"? |
---|
| 92 | hex_constant {hex_prefix}{hex_digits}{integer_suffix}? |
---|
| 93 | |
---|
| 94 | decimal_digits ({decimal})|({decimal}({decimal}|"_")*{decimal}) |
---|
[0213af6] | 95 | real_decimal {decimal_digits}"."{exponent}?{floating_suffix}? |
---|
| 96 | real_fraction "."{decimal_digits}{exponent}?{floating_suffix}? |
---|
| 97 | real_constant {decimal_digits}{real_fraction} |
---|
[51b7345] | 98 | exponent "_"?[eE]"_"?[+-]?{decimal_digits} |
---|
[ba2356b] | 99 | // GCC: D (double) and iI (imaginary) suffixes, and DL (long double) |
---|
| 100 | floating_suffix "_"?([fFdDlL][iI]?|[iI][lLfFdD]?|"DL") |
---|
[1b29996] | 101 | floating_constant (({real_constant}{exponent}?)|({decimal_digits}{exponent})){floating_suffix}? |
---|
[51b7345] | 102 | |
---|
| 103 | binary_exponent "_"?[pP]"_"?[+-]?{decimal_digits} |
---|
| 104 | hex_fractional_constant ({hex_digits}?"."{hex_digits})|({hex_digits}".") |
---|
| 105 | hex_floating_constant {hex_prefix}(({hex_fractional_constant}{binary_exponent})|({hex_digits}{binary_exponent})){floating_suffix}? |
---|
| 106 | |
---|
[b87a5ed] | 107 | // character escape sequence, GCC: \e => esc character |
---|
[51b7345] | 108 | simple_escape "\\"[abefnrtv'"?\\] |
---|
[b87a5ed] | 109 | // ' stop highlighting |
---|
[3848e0e] | 110 | octal_escape "\\"{octal}("_"?{octal}){0,2} |
---|
| 111 | hex_escape "\\""x""_"?{hex_digits} |
---|
[51b7345] | 112 | escape_seq {simple_escape}|{octal_escape}|{hex_escape}|{universal_char} |
---|
[59db689] | 113 | cwide_prefix "L"|"U"|"u" |
---|
| 114 | swide_prefix {cwide_prefix}|"u8" |
---|
[51b7345] | 115 | |
---|
[b87a5ed] | 116 | // display/white-space characters |
---|
[51b7345] | 117 | h_tab [\011] |
---|
| 118 | form_feed [\014] |
---|
| 119 | v_tab [\013] |
---|
| 120 | c_return [\015] |
---|
| 121 | h_white [ ]|{h_tab} |
---|
| 122 | |
---|
[e7aed49] | 123 | // overloadable operators |
---|
[51b7345] | 124 | op_unary_only "~"|"!" |
---|
| 125 | op_unary_binary "+"|"-"|"*" |
---|
| 126 | op_unary_pre_post "++"|"--" |
---|
| 127 | op_unary {op_unary_only}|{op_unary_binary}|{op_unary_pre_post} |
---|
| 128 | |
---|
[994d080] | 129 | op_binary_only "/"|"%"|"\\"|"^"|"&"|"|"|"<"|">"|"="|"=="|"!="|"<<"|">>"|"<="|">="|"+="|"-="|"*="|"/="|"%="|"\\="|"&="|"|="|"^="|"<<="|">>=" |
---|
[51b7345] | 130 | op_binary_over {op_unary_binary}|{op_binary_only} |
---|
[e7aed49] | 131 | // op_binary_not_over "?"|"->"|"."|"&&"|"||"|"@=" |
---|
| 132 | // operator {op_unary_pre_post}|{op_binary_over}|{op_binary_not_over} |
---|
[51b7345] | 133 | |
---|
| 134 | %x COMMENT |
---|
[3848e0e] | 135 | %x BKQUOTE |
---|
| 136 | %x QUOTE |
---|
| 137 | %x STRING |
---|
[51b7345] | 138 | |
---|
| 139 | %% |
---|
[994d080] | 140 | /* line directives */ |
---|
[4040425] | 141 | ^{h_white}*"#"{h_white}*[0-9]+{h_white}*["][^"\n]+["].*"\n" { |
---|
[8c17ab0] | 142 | /* " stop highlighting */ |
---|
[c1c1112] | 143 | static char filename[FILENAME_MAX]; // temporarily store current source-file name |
---|
[51b7345] | 144 | char *end_num; |
---|
| 145 | char *begin_string, *end_string; |
---|
| 146 | long lineno, length; |
---|
| 147 | lineno = strtol( yytext + 1, &end_num, 0 ); |
---|
| 148 | begin_string = strchr( end_num, '"' ); |
---|
[f487962] | 149 | if ( begin_string ) { // file name ? |
---|
| 150 | end_string = strchr( begin_string + 1, '"' ); // look for ending delimiter |
---|
| 151 | assert( end_string ); // closing quote ? |
---|
| 152 | length = end_string - begin_string - 1; // file-name length without quotes or sentinel |
---|
| 153 | assert( length < FILENAME_MAX ); // room for sentinel ? |
---|
| 154 | memcpy( &filename, begin_string + 1, length ); // copy file name from yytext |
---|
| 155 | filename[ length ] = '\0'; // terminate string with sentinel |
---|
| 156 | //std::cout << "file " << filename << " line " << lineno << std::endl; |
---|
| 157 | yylineno = lineno; |
---|
[c1c1112] | 158 | yyfilename = filename; |
---|
[b87a5ed] | 159 | } // if |
---|
[51b7345] | 160 | } |
---|
| 161 | |
---|
[b87a5ed] | 162 | /* ignore preprocessor directives (for now) */ |
---|
[51b7345] | 163 | ^{h_white}*"#"[^\n]*"\n" ; |
---|
| 164 | |
---|
[cd623a4] | 165 | /* ignore C style comments (ALSO HANDLED BY CPP) */ |
---|
[3848e0e] | 166 | "/*" { BEGIN COMMENT; } |
---|
[cd623a4] | 167 | <COMMENT>.|\n ; |
---|
| 168 | <COMMENT>"*/" { BEGIN 0; } |
---|
[51b7345] | 169 | |
---|
[cd623a4] | 170 | /* ignore C++ style comments (ALSO HANDLED BY CPP) */ |
---|
| 171 | "//"[^\n]*"\n" ; |
---|
[51b7345] | 172 | |
---|
[b87a5ed] | 173 | /* ignore whitespace */ |
---|
[3848e0e] | 174 | {h_white}+ { WHITE_RETURN(' '); } |
---|
| 175 | ({v_tab}|{c_return}|{form_feed})+ { WHITE_RETURN(' '); } |
---|
| 176 | ({h_white}|{v_tab}|{c_return}|{form_feed})*"\n" { NEWLINE_RETURN(); } |
---|
[51b7345] | 177 | |
---|
[b87a5ed] | 178 | /* keywords */ |
---|
| 179 | _Alignas { KEYWORD_RETURN(ALIGNAS); } // C11 |
---|
| 180 | _Alignof { KEYWORD_RETURN(ALIGNOF); } // C11 |
---|
| 181 | __alignof { KEYWORD_RETURN(ALIGNOF); } // GCC |
---|
| 182 | __alignof__ { KEYWORD_RETURN(ALIGNOF); } // GCC |
---|
| 183 | asm { KEYWORD_RETURN(ASM); } |
---|
| 184 | __asm { KEYWORD_RETURN(ASM); } // GCC |
---|
| 185 | __asm__ { KEYWORD_RETURN(ASM); } // GCC |
---|
[02e5ab6] | 186 | _At { KEYWORD_RETURN(AT); } // CFA |
---|
[b87a5ed] | 187 | _Atomic { KEYWORD_RETURN(ATOMIC); } // C11 |
---|
| 188 | __attribute { KEYWORD_RETURN(ATTRIBUTE); } // GCC |
---|
| 189 | __attribute__ { KEYWORD_RETURN(ATTRIBUTE); } // GCC |
---|
[3848e0e] | 190 | auto { KEYWORD_RETURN(AUTO); } |
---|
[b87a5ed] | 191 | _Bool { KEYWORD_RETURN(BOOL); } // C99 |
---|
[3848e0e] | 192 | break { KEYWORD_RETURN(BREAK); } |
---|
| 193 | case { KEYWORD_RETURN(CASE); } |
---|
[b87a5ed] | 194 | catch { KEYWORD_RETURN(CATCH); } // CFA |
---|
[02e5ab6] | 195 | catchResume { KEYWORD_RETURN(CATCHRESUME); } // CFA |
---|
[3848e0e] | 196 | char { KEYWORD_RETURN(CHAR); } |
---|
[b87a5ed] | 197 | choose { KEYWORD_RETURN(CHOOSE); } // CFA |
---|
| 198 | _Complex { KEYWORD_RETURN(COMPLEX); } // C99 |
---|
| 199 | __complex { KEYWORD_RETURN(COMPLEX); } // GCC |
---|
| 200 | __complex__ { KEYWORD_RETURN(COMPLEX); } // GCC |
---|
[3848e0e] | 201 | const { KEYWORD_RETURN(CONST); } |
---|
[b87a5ed] | 202 | __const { KEYWORD_RETURN(CONST); } // GCC |
---|
| 203 | __const__ { KEYWORD_RETURN(CONST); } // GCC |
---|
[3848e0e] | 204 | continue { KEYWORD_RETURN(CONTINUE); } |
---|
[e04b636] | 205 | coroutine { KEYWORD_RETURN(COROUTINE); } // CFA |
---|
[3848e0e] | 206 | default { KEYWORD_RETURN(DEFAULT); } |
---|
[02e5ab6] | 207 | disable { KEYWORD_RETURN(DISABLE); } // CFA |
---|
[b87a5ed] | 208 | do { KEYWORD_RETURN(DO); } |
---|
[3848e0e] | 209 | double { KEYWORD_RETURN(DOUBLE); } |
---|
[b87a5ed] | 210 | dtype { KEYWORD_RETURN(DTYPE); } // CFA |
---|
[3848e0e] | 211 | else { KEYWORD_RETURN(ELSE); } |
---|
[02e5ab6] | 212 | enable { KEYWORD_RETURN(ENABLE); } // CFA |
---|
[3848e0e] | 213 | enum { KEYWORD_RETURN(ENUM); } |
---|
[b87a5ed] | 214 | __extension__ { KEYWORD_RETURN(EXTENSION); } // GCC |
---|
[3848e0e] | 215 | extern { KEYWORD_RETURN(EXTERN); } |
---|
[08061589] | 216 | fallthrough { KEYWORD_RETURN(FALLTHRU); } // CFA |
---|
[b87a5ed] | 217 | fallthru { KEYWORD_RETURN(FALLTHRU); } // CFA |
---|
| 218 | finally { KEYWORD_RETURN(FINALLY); } // CFA |
---|
[3848e0e] | 219 | float { KEYWORD_RETURN(FLOAT); } |
---|
[b87a5ed] | 220 | __float128 { KEYWORD_RETURN(FLOAT); } // GCC |
---|
| 221 | for { KEYWORD_RETURN(FOR); } |
---|
| 222 | forall { KEYWORD_RETURN(FORALL); } // CFA |
---|
[3848e0e] | 223 | fortran { KEYWORD_RETURN(FORTRAN); } |
---|
[b87a5ed] | 224 | ftype { KEYWORD_RETURN(FTYPE); } // CFA |
---|
| 225 | _Generic { KEYWORD_RETURN(GENERIC); } // C11 |
---|
[3848e0e] | 226 | goto { KEYWORD_RETURN(GOTO); } |
---|
[b87a5ed] | 227 | if { KEYWORD_RETURN(IF); } |
---|
| 228 | _Imaginary { KEYWORD_RETURN(IMAGINARY); } // C99 |
---|
| 229 | __imag { KEYWORD_RETURN(IMAGINARY); } // GCC |
---|
| 230 | __imag__ { KEYWORD_RETURN(IMAGINARY); } // GCC |
---|
| 231 | inline { KEYWORD_RETURN(INLINE); } // C99 |
---|
| 232 | __inline { KEYWORD_RETURN(INLINE); } // GCC |
---|
| 233 | __inline__ { KEYWORD_RETURN(INLINE); } // GCC |
---|
| 234 | int { KEYWORD_RETURN(INT); } |
---|
| 235 | __int128 { KEYWORD_RETURN(INT); } // GCC |
---|
[b15f6cf] | 236 | __int128_t { KEYWORD_RETURN(INT); } // GCC |
---|
[b87a5ed] | 237 | __label__ { KEYWORD_RETURN(LABEL); } // GCC |
---|
[3848e0e] | 238 | long { KEYWORD_RETURN(LONG); } |
---|
[6016c87] | 239 | monitor { KEYWORD_RETURN(MONITOR); } // CFA |
---|
[a7c90d4] | 240 | mutex { KEYWORD_RETURN(MUTEX); } // CFA |
---|
[b87a5ed] | 241 | _Noreturn { KEYWORD_RETURN(NORETURN); } // C11 |
---|
[5721a6d] | 242 | __builtin_offsetof { KEYWORD_RETURN(OFFSETOF); } // GCC |
---|
[3a2128f] | 243 | one_t { NUMERIC_RETURN(ONE_T); } // CFA |
---|
[4040425] | 244 | otype { KEYWORD_RETURN(OTYPE); } // CFA |
---|
[3848e0e] | 245 | register { KEYWORD_RETURN(REGISTER); } |
---|
[b87a5ed] | 246 | restrict { KEYWORD_RETURN(RESTRICT); } // C99 |
---|
| 247 | __restrict { KEYWORD_RETURN(RESTRICT); } // GCC |
---|
| 248 | __restrict__ { KEYWORD_RETURN(RESTRICT); } // GCC |
---|
[3848e0e] | 249 | return { KEYWORD_RETURN(RETURN); } |
---|
| 250 | short { KEYWORD_RETURN(SHORT); } |
---|
| 251 | signed { KEYWORD_RETURN(SIGNED); } |
---|
[b87a5ed] | 252 | __signed { KEYWORD_RETURN(SIGNED); } // GCC |
---|
| 253 | __signed__ { KEYWORD_RETURN(SIGNED); } // GCC |
---|
[3848e0e] | 254 | sizeof { KEYWORD_RETURN(SIZEOF); } |
---|
| 255 | static { KEYWORD_RETURN(STATIC); } |
---|
[b87a5ed] | 256 | _Static_assert { KEYWORD_RETURN(STATICASSERT); } // C11 |
---|
[3848e0e] | 257 | struct { KEYWORD_RETURN(STRUCT); } |
---|
| 258 | switch { KEYWORD_RETURN(SWITCH); } |
---|
[bd4d011] | 259 | thread { KEYWORD_RETURN(THREAD); } // C11 |
---|
[b87a5ed] | 260 | _Thread_local { KEYWORD_RETURN(THREADLOCAL); } // C11 |
---|
| 261 | throw { KEYWORD_RETURN(THROW); } // CFA |
---|
[02e5ab6] | 262 | throwResume { KEYWORD_RETURN(THROWRESUME); } // CFA |
---|
[5b2edbc] | 263 | timeout { QKEYWORD_RETURN(TIMEOUT); } // CFA |
---|
[4040425] | 264 | trait { KEYWORD_RETURN(TRAIT); } // CFA |
---|
[b87a5ed] | 265 | try { KEYWORD_RETURN(TRY); } // CFA |
---|
[8f60f0b] | 266 | ttype { KEYWORD_RETURN(TTYPE); } // CFA |
---|
[3848e0e] | 267 | typedef { KEYWORD_RETURN(TYPEDEF); } |
---|
[b87a5ed] | 268 | typeof { KEYWORD_RETURN(TYPEOF); } // GCC |
---|
| 269 | __typeof { KEYWORD_RETURN(TYPEOF); } // GCC |
---|
| 270 | __typeof__ { KEYWORD_RETURN(TYPEOF); } // GCC |
---|
[b15f6cf] | 271 | __uint128_t { KEYWORD_RETURN(INT); } // GCC |
---|
[3848e0e] | 272 | union { KEYWORD_RETURN(UNION); } |
---|
| 273 | unsigned { KEYWORD_RETURN(UNSIGNED); } |
---|
[90c3b1c] | 274 | __builtin_va_list { KEYWORD_RETURN(VALIST); } // GCC |
---|
[72457b6] | 275 | virtual { KEYWORD_RETURN(VIRTUAL); } // CFA |
---|
[3848e0e] | 276 | void { KEYWORD_RETURN(VOID); } |
---|
| 277 | volatile { KEYWORD_RETURN(VOLATILE); } |
---|
[b87a5ed] | 278 | __volatile { KEYWORD_RETURN(VOLATILE); } // GCC |
---|
| 279 | __volatile__ { KEYWORD_RETURN(VOLATILE); } // GCC |
---|
[5b2edbc] | 280 | waitfor { KEYWORD_RETURN(WAITFOR); } |
---|
| 281 | or { QKEYWORD_RETURN(WOR); } // CFA |
---|
| 282 | when { KEYWORD_RETURN(WHEN); } |
---|
[3848e0e] | 283 | while { KEYWORD_RETURN(WHILE); } |
---|
[8b47e50] | 284 | with { KEYWORD_RETURN(WITH); } // CFA |
---|
[3a2128f] | 285 | zero_t { NUMERIC_RETURN(ZERO_T); } // CFA |
---|
[51b7345] | 286 | |
---|
[b87a5ed] | 287 | /* identifier */ |
---|
| 288 | {identifier} { IDENTIFIER_RETURN(); } |
---|
| 289 | {attr_identifier} { ATTRIBUTE_RETURN(); } |
---|
[c6b1105] | 290 | "`" { BEGIN BKQUOTE; } |
---|
[b87a5ed] | 291 | <BKQUOTE>{identifier} { IDENTIFIER_RETURN(); } |
---|
| 292 | <BKQUOTE>"`" { BEGIN 0; } |
---|
[51b7345] | 293 | |
---|
[b87a5ed] | 294 | /* numeric constants */ |
---|
[59db689] | 295 | {decimal_constant} { NUMERIC_RETURN(INTEGERconstant); } |
---|
| 296 | {octal_constant} { NUMERIC_RETURN(INTEGERconstant); } |
---|
| 297 | {hex_constant} { NUMERIC_RETURN(INTEGERconstant); } |
---|
[1b29996] | 298 | {real_decimal} { NUMERIC_RETURN(REALDECIMALconstant); } // must appear before floating_constant |
---|
| 299 | {real_fraction} { NUMERIC_RETURN(REALFRACTIONconstant); } // must appear before floating_constant |
---|
[3848e0e] | 300 | {floating_constant} { NUMERIC_RETURN(FLOATINGconstant); } |
---|
| 301 | {hex_floating_constant} { NUMERIC_RETURN(FLOATINGconstant); } |
---|
[51b7345] | 302 | |
---|
[b87a5ed] | 303 | /* character constant, allows empty value */ |
---|
[c1c1112] | 304 | ({cwide_prefix}[_]?)?['] { BEGIN QUOTE; rm_underscore(); strtext = new std::string( yytext, yyleng ); } |
---|
| 305 | <QUOTE>[^'\\\n]* { strtext->append( yytext, yyleng ); } |
---|
| 306 | <QUOTE>['\n] { BEGIN 0; strtext->append( yytext, yyleng ); RETURN_STR(CHARACTERconstant); } |
---|
[b87a5ed] | 307 | /* ' stop highlighting */ |
---|
[51b7345] | 308 | |
---|
[b87a5ed] | 309 | /* string constant */ |
---|
[c1c1112] | 310 | ({swide_prefix}[_]?)?["] { BEGIN STRING; rm_underscore(); strtext = new std::string( yytext, yyleng ); } |
---|
| 311 | <STRING>[^"\\\n]* { strtext->append( yytext, yyleng ); } |
---|
| 312 | <STRING>["\n] { BEGIN 0; strtext->append( yytext, yyleng ); RETURN_STR(STRINGliteral); } |
---|
[b87a5ed] | 313 | /* " stop highlighting */ |
---|
[51b7345] | 314 | |
---|
[59db689] | 315 | /* common character/string constant */ |
---|
[c1c1112] | 316 | <QUOTE,STRING>{escape_seq} { rm_underscore(); strtext->append( yytext, yyleng ); } |
---|
[cd623a4] | 317 | <QUOTE,STRING>"\\"{h_white}*"\n" {} // continuation (ALSO HANDLED BY CPP) |
---|
[c1c1112] | 318 | <QUOTE,STRING>"\\" { strtext->append( yytext, yyleng ); } // unknown escape character |
---|
[3848e0e] | 319 | |
---|
[b87a5ed] | 320 | /* punctuation */ |
---|
[615a096] | 321 | "@" { ASCIIOP_RETURN(); } |
---|
[b87a5ed] | 322 | "[" { ASCIIOP_RETURN(); } |
---|
| 323 | "]" { ASCIIOP_RETURN(); } |
---|
| 324 | "(" { ASCIIOP_RETURN(); } |
---|
| 325 | ")" { ASCIIOP_RETURN(); } |
---|
| 326 | "{" { ASCIIOP_RETURN(); } |
---|
| 327 | "}" { ASCIIOP_RETURN(); } |
---|
| 328 | "," { ASCIIOP_RETURN(); } // also operator |
---|
| 329 | ":" { ASCIIOP_RETURN(); } |
---|
| 330 | ";" { ASCIIOP_RETURN(); } |
---|
| 331 | "." { ASCIIOP_RETURN(); } // also operator |
---|
[3848e0e] | 332 | "..." { NAMEDOP_RETURN(ELLIPSIS); } |
---|
| 333 | |
---|
[b87a5ed] | 334 | /* alternative C99 brackets, "<:" & "<:<:" handled by preprocessor */ |
---|
[3848e0e] | 335 | "<:" { RETURN_VAL('['); } |
---|
| 336 | ":>" { RETURN_VAL(']'); } |
---|
| 337 | "<%" { RETURN_VAL('{'); } |
---|
| 338 | "%>" { RETURN_VAL('}'); } |
---|
[51b7345] | 339 | |
---|
[b87a5ed] | 340 | /* operators */ |
---|
| 341 | "!" { ASCIIOP_RETURN(); } |
---|
| 342 | "+" { ASCIIOP_RETURN(); } |
---|
| 343 | "-" { ASCIIOP_RETURN(); } |
---|
| 344 | "*" { ASCIIOP_RETURN(); } |
---|
[e5f2a67] | 345 | "\\" { ASCIIOP_RETURN(); } // CFA, exponentiation |
---|
[b87a5ed] | 346 | "/" { ASCIIOP_RETURN(); } |
---|
| 347 | "%" { ASCIIOP_RETURN(); } |
---|
| 348 | "^" { ASCIIOP_RETURN(); } |
---|
| 349 | "~" { ASCIIOP_RETURN(); } |
---|
| 350 | "&" { ASCIIOP_RETURN(); } |
---|
| 351 | "|" { ASCIIOP_RETURN(); } |
---|
| 352 | "<" { ASCIIOP_RETURN(); } |
---|
| 353 | ">" { ASCIIOP_RETURN(); } |
---|
| 354 | "=" { ASCIIOP_RETURN(); } |
---|
| 355 | "?" { ASCIIOP_RETURN(); } |
---|
[3848e0e] | 356 | |
---|
| 357 | "++" { NAMEDOP_RETURN(ICR); } |
---|
| 358 | "--" { NAMEDOP_RETURN(DECR); } |
---|
| 359 | "==" { NAMEDOP_RETURN(EQ); } |
---|
| 360 | "!=" { NAMEDOP_RETURN(NE); } |
---|
| 361 | "<<" { NAMEDOP_RETURN(LS); } |
---|
| 362 | ">>" { NAMEDOP_RETURN(RS); } |
---|
| 363 | "<=" { NAMEDOP_RETURN(LE); } |
---|
| 364 | ">=" { NAMEDOP_RETURN(GE); } |
---|
| 365 | "&&" { NAMEDOP_RETURN(ANDAND); } |
---|
| 366 | "||" { NAMEDOP_RETURN(OROR); } |
---|
| 367 | "->" { NAMEDOP_RETURN(ARROW); } |
---|
| 368 | "+=" { NAMEDOP_RETURN(PLUSassign); } |
---|
| 369 | "-=" { NAMEDOP_RETURN(MINUSassign); } |
---|
[e5f2a67] | 370 | "\\=" { NAMEDOP_RETURN(EXPassign); } // CFA, exponentiation |
---|
[3848e0e] | 371 | "*=" { NAMEDOP_RETURN(MULTassign); } |
---|
| 372 | "/=" { NAMEDOP_RETURN(DIVassign); } |
---|
| 373 | "%=" { NAMEDOP_RETURN(MODassign); } |
---|
| 374 | "&=" { NAMEDOP_RETURN(ANDassign); } |
---|
| 375 | "|=" { NAMEDOP_RETURN(ORassign); } |
---|
| 376 | "^=" { NAMEDOP_RETURN(ERassign); } |
---|
| 377 | "<<=" { NAMEDOP_RETURN(LSassign); } |
---|
| 378 | ">>=" { NAMEDOP_RETURN(RSassign); } |
---|
[51b7345] | 379 | |
---|
[08061589] | 380 | "@=" { NAMEDOP_RETURN(ATassign); } // CFA |
---|
[097e2b0] | 381 | |
---|
[b87a5ed] | 382 | /* CFA, operator identifier */ |
---|
| 383 | {op_unary}"?" { IDENTIFIER_RETURN(); } // unary |
---|
[a61fea9a] | 384 | "?"({op_unary_pre_post}|"()"|"[?]"|"{}") { IDENTIFIER_RETURN(); } |
---|
[02e5ab6] | 385 | "^?{}" { IDENTIFIER_RETURN(); } |
---|
[b87a5ed] | 386 | "?"{op_binary_over}"?" { IDENTIFIER_RETURN(); } // binary |
---|
[51b7345] | 387 | /* |
---|
[daf9671] | 388 | This rule handles ambiguous cases with operator identifiers, e.g., "int *?*?()", where the string "*?*?" can be |
---|
| 389 | lexed as "*?"/"*?" or "*"/"?*?". Since it is common practise to put a unary operator juxtaposed to an identifier, |
---|
| 390 | e.g., "*i", users will be annoyed if they cannot do this with respect to operator identifiers. Therefore, there is |
---|
| 391 | a lexical look-ahead for the second case, with backtracking to return the leading unary operator and then |
---|
| 392 | reparsing the trailing operator identifier. Otherwise a space is needed between the unary operator and operator |
---|
| 393 | identifier to disambiguate this common case. |
---|
| 394 | |
---|
| 395 | A similar issue occurs with the dereference, *?(...), and routine-call, ?()(...) identifiers. The ambiguity |
---|
| 396 | occurs when the deference operator has no parameters, *?() and *?()(...), requiring arbitrary whitespace |
---|
| 397 | look-ahead for the routine-call parameter-list to disambiguate. However, the dereference operator must have a |
---|
| 398 | parameter/argument to dereference *?(...). Hence, always interpreting the string *?() as * ?() does not preclude |
---|
| 399 | any meaningful program. |
---|
| 400 | |
---|
| 401 | The remaining cases are with the increment/decrement operators and conditional expression: |
---|
| 402 | |
---|
| 403 | i++? ...(...); |
---|
| 404 | i?++ ...(...); |
---|
| 405 | |
---|
| 406 | requiring arbitrary whitespace look-ahead for the operator parameter-list, even though that interpretation is an |
---|
| 407 | incorrect expression (juxtaposed identifiers). Therefore, it is necessary to disambiguate these cases with a |
---|
| 408 | space: |
---|
| 409 | |
---|
| 410 | i++ ? i : 0; |
---|
| 411 | i? ++i : 0; |
---|
[51b7345] | 412 | */ |
---|
[daf9671] | 413 | {op_unary}"?"({op_unary_pre_post}|"()"|"[?]"|{op_binary_over}"?") { |
---|
[b87a5ed] | 414 | // 1 or 2 character unary operator ? |
---|
| 415 | int i = yytext[1] == '?' ? 1 : 2; |
---|
| 416 | yyless( i ); // put back characters up to first '?' |
---|
| 417 | if ( i > 1 ) { |
---|
| 418 | NAMEDOP_RETURN( yytext[0] == '+' ? ICR : DECR ); |
---|
| 419 | } else { |
---|
| 420 | ASCIIOP_RETURN(); |
---|
| 421 | } // if |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | /* unknown characters */ |
---|
[a2e0687] | 425 | . { printf("unknown character(s):\"%s\" on line %d\n", yytext, yylineno); } |
---|
[51b7345] | 426 | |
---|
| 427 | %% |
---|
| 428 | |
---|
[b87a5ed] | 429 | // Local Variables: // |
---|
| 430 | // mode: c++ // |
---|
[de62360d] | 431 | // tab-width: 4 // |
---|
[b87a5ed] | 432 | // compile-command: "make install" // |
---|
| 433 | // End: // |
---|