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