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