source: src/Parser/lex.ll @ 44f44617

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 44f44617 was a67b60e, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

rename files and adjust includes

  • Property mode set to 100644
File size: 16.2 KB
Line 
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 : Wed Jun 28 21:03:45 2017
13 * Update Count     : 529
14 */
15
16%option yylineno
17%option nounput
18
19%{
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.
23
24//**************************** Includes and Defines ****************************
25
26#include <string>
27#include <cstdio>                                                                               // FILENAME_MAX
28
29#include "ParseNode.h"
30#include "TypedefTable.h"
31
32char *yyfilename;
33std::string *strtext;                                                                   // accumulate parts of character and string constant value
34
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 )
37#define RETURN_CHAR(x)          yylval.tok.str = nullptr; RETURN_LOCN( x )
38#define RETURN_STR(x)           yylval.tok.str = strtext; RETURN_LOCN( x )
39
40#define WHITE_RETURN(x)         // do nothing
41#define NEWLINE_RETURN()        WHITE_RETURN( '\n' )
42#define ASCIIOP_RETURN()        RETURN_CHAR( (int)yytext[0] ) // single character operator
43#define NAMEDOP_RETURN(x)       RETURN_CHAR( x )                        // multichar operator, with a name
44#define NUMERIC_RETURN(x)       rm_underscore(); RETURN_VAL( x ) // numeric constant
45#define KEYWORD_RETURN(x)       RETURN_CHAR( x )                        // keyword
46#define IDENTIFIER_RETURN()     RETURN_VAL( typedefTable.isKind( yytext ) )
47#define ATTRIBUTE_RETURN()      RETURN_VAL( ATTR_IDENTIFIER )
48
49void rm_underscore() {
50        // Remove underscores in numeric constant by copying the non-underscore characters to the front of the string.
51        yyleng = 0;
52        for ( int i = 0; yytext[i] != '\0'; i += 1 ) {
53                if ( yytext[i] != '_' ) {
54                        yytext[yyleng] = yytext[i];
55                        yyleng += 1;
56                } // if
57        } // for
58        yytext[yyleng] = '\0';
59}
60
61%}
62
63octal [0-7]
64nonzero [1-9]
65decimal [0-9]
66hex [0-9a-fA-F]
67universal_char "\\"((u"_"?{hex_quad})|(U"_"?{hex_quad}{2}))
68
69                                // identifier, GCC: $ in identifier
70identifier ([a-zA-Z_$]|{universal_char})([0-9a-zA-Z_$]|{universal_char})*
71
72                                // attribute identifier, GCC: $ in identifier
73attr_identifier "@"{identifier}
74
75                                // numeric constants, CFA: '_' in constant
76hex_quad {hex}("_"?{hex}){3}
77integer_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]?)))
78
79octal_digits ({octal})|({octal}({octal}|"_")*{octal})
80octal_prefix "0""_"?
81octal_constant (("0")|({octal_prefix}{octal_digits})){integer_suffix}?
82
83nonzero_digits ({nonzero})|({nonzero}({decimal}|"_")*{decimal})
84decimal_constant {nonzero_digits}{integer_suffix}?
85
86hex_digits ({hex})|({hex}({hex}|"_")*{hex})
87hex_prefix "0"[xX]"_"?
88hex_constant {hex_prefix}{hex_digits}{integer_suffix}?
89
90decimal_digits ({decimal})|({decimal}({decimal}|"_")*{decimal})
91real_decimal {decimal_digits}"."{exponent}?{floating_suffix}?
92real_fraction "."{decimal_digits}{exponent}?{floating_suffix}?
93real_constant {decimal_digits}{real_fraction}
94exponent "_"?[eE]"_"?[+-]?{decimal_digits}
95                                // GCC: D (double) and iI (imaginary) suffixes, and DL (long double)
96floating_suffix "_"?([fFdDlL][iI]?|[iI][lLfFdD]?|"DL")
97floating_constant (({real_constant}{exponent}?)|({decimal_digits}{exponent})){floating_suffix}?
98
99binary_exponent "_"?[pP]"_"?[+-]?{decimal_digits}
100hex_fractional_constant ({hex_digits}?"."{hex_digits})|({hex_digits}".")
101hex_floating_constant {hex_prefix}(({hex_fractional_constant}{binary_exponent})|({hex_digits}{binary_exponent})){floating_suffix}?
102
103                                // character escape sequence, GCC: \e => esc character
104simple_escape "\\"[abefnrtv'"?\\]
105                                // ' stop highlighting
106octal_escape "\\"{octal}("_"?{octal}){0,2}
107hex_escape "\\""x""_"?{hex_digits}
108escape_seq {simple_escape}|{octal_escape}|{hex_escape}|{universal_char}
109cwide_prefix "L"|"U"|"u"
110swide_prefix {cwide_prefix}|"u8"
111
112                                // display/white-space characters
113h_tab [\011]
114form_feed [\014]
115v_tab [\013]
116c_return [\015]
117h_white [ ]|{h_tab}
118
119                                // overloadable operators
120op_unary_only "~"|"!"
121op_unary_binary "+"|"-"|"*"
122op_unary_pre_post "++"|"--"
123op_unary {op_unary_only}|{op_unary_binary}|{op_unary_pre_post}
124
125op_binary_only "/"|"%"|"^"|"&"|"|"|"<"|">"|"="|"=="|"!="|"<<"|">>"|"<="|">="|"+="|"-="|"*="|"/="|"%="|"&="|"|="|"^="|"<<="|">>="
126op_binary_over {op_unary_binary}|{op_binary_only}
127                                // op_binary_not_over "?"|"->"|"."|"&&"|"||"|"@="
128                                // operator {op_unary_pre_post}|{op_binary_over}|{op_binary_not_over}
129
130%x COMMENT
131%x BKQUOTE
132%x QUOTE
133%x STRING
134
135%%
136                                   /* line directives */
137^{h_white}*"#"{h_white}*[0-9]+{h_white}*["][^"\n]+["].*"\n" {
138        /* " stop highlighting */
139        static char filename[FILENAME_MAX];                                     // temporarily store current source-file name
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, '"' );
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;
154                yyfilename = filename;
155        } // if
156}
157
158                                /* ignore preprocessor directives (for now) */
159^{h_white}*"#"[^\n]*"\n" ;
160
161                                /* ignore C style comments (ALSO HANDLED BY CPP) */
162"/*"                    { BEGIN COMMENT; }
163<COMMENT>.|\n   ;
164<COMMENT>"*/"   { BEGIN 0; }
165
166                                /* ignore C++ style comments (ALSO HANDLED BY CPP) */
167"//"[^\n]*"\n"  ;
168
169                                /* ignore whitespace */
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(); }
173
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
179asm                             { KEYWORD_RETURN(ASM); }
180__asm                   { KEYWORD_RETURN(ASM); }                                // GCC
181__asm__                 { KEYWORD_RETURN(ASM); }                                // GCC
182_At                             { KEYWORD_RETURN(AT); }                                 // CFA
183_Atomic                 { KEYWORD_RETURN(ATOMIC); }                             // C11
184__attribute             { KEYWORD_RETURN(ATTRIBUTE); }                  // GCC
185__attribute__   { KEYWORD_RETURN(ATTRIBUTE); }                  // GCC
186auto                    { KEYWORD_RETURN(AUTO); }
187_Bool                   { KEYWORD_RETURN(BOOL); }                               // C99
188break                   { KEYWORD_RETURN(BREAK); }
189case                    { KEYWORD_RETURN(CASE); }
190catch                   { KEYWORD_RETURN(CATCH); }                              // CFA
191catchResume             { KEYWORD_RETURN(CATCHRESUME); }                // CFA
192char                    { KEYWORD_RETURN(CHAR); }
193choose                  { KEYWORD_RETURN(CHOOSE); }                             // CFA
194_Complex                { KEYWORD_RETURN(COMPLEX); }                    // C99
195__complex               { KEYWORD_RETURN(COMPLEX); }                    // GCC
196__complex__             { KEYWORD_RETURN(COMPLEX); }                    // GCC
197const                   { KEYWORD_RETURN(CONST); }
198__const                 { KEYWORD_RETURN(CONST); }                              // GCC
199__const__               { KEYWORD_RETURN(CONST); }                              // GCC
200continue                { KEYWORD_RETURN(CONTINUE); }
201coroutine               { KEYWORD_RETURN(COROUTINE); }                  // CFA
202default                 { KEYWORD_RETURN(DEFAULT); }
203disable                 { KEYWORD_RETURN(DISABLE); }                    // CFA
204do                              { KEYWORD_RETURN(DO); }
205double                  { KEYWORD_RETURN(DOUBLE); }
206dtype                   { KEYWORD_RETURN(DTYPE); }                              // CFA
207else                    { KEYWORD_RETURN(ELSE); }
208enable                  { KEYWORD_RETURN(ENABLE); }                             // CFA
209enum                    { KEYWORD_RETURN(ENUM); }
210__extension__   { KEYWORD_RETURN(EXTENSION); }                  // GCC
211extern                  { KEYWORD_RETURN(EXTERN); }
212fallthrough             { KEYWORD_RETURN(FALLTHRU); }                   // CFA
213fallthru                { KEYWORD_RETURN(FALLTHRU); }                   // CFA
214finally                 { KEYWORD_RETURN(FINALLY); }                    // CFA
215float                   { KEYWORD_RETURN(FLOAT); }
216__float128              { KEYWORD_RETURN(FLOAT); }                              // GCC
217for                             { KEYWORD_RETURN(FOR); }
218forall                  { KEYWORD_RETURN(FORALL); }                             // CFA
219fortran                 { KEYWORD_RETURN(FORTRAN); }
220ftype                   { KEYWORD_RETURN(FTYPE); }                              // CFA
221_Generic                { KEYWORD_RETURN(GENERIC); }                    // C11
222goto                    { KEYWORD_RETURN(GOTO); }
223if                              { KEYWORD_RETURN(IF); }
224_Imaginary              { KEYWORD_RETURN(IMAGINARY); }                  // C99
225__imag                  { KEYWORD_RETURN(IMAGINARY); }                  // GCC
226__imag__                { KEYWORD_RETURN(IMAGINARY); }                  // GCC
227inline                  { KEYWORD_RETURN(INLINE); }                             // C99
228__inline                { KEYWORD_RETURN(INLINE); }                             // GCC
229__inline__              { KEYWORD_RETURN(INLINE); }                             // GCC
230int                             { KEYWORD_RETURN(INT); }
231__int128                { KEYWORD_RETURN(INT); }                                // GCC
232__label__               { KEYWORD_RETURN(LABEL); }                              // GCC
233long                    { KEYWORD_RETURN(LONG); }
234lvalue                  { KEYWORD_RETURN(LVALUE); }                             // CFA
235monitor                 { KEYWORD_RETURN(MONITOR); }                    // CFA
236mutex                   { KEYWORD_RETURN(MUTEX); }                              // CFA
237_Noreturn               { KEYWORD_RETURN(NORETURN); }                   // C11
238__builtin_offsetof { KEYWORD_RETURN(OFFSETOF); }                // GCC
239one_t                   { NUMERIC_RETURN(ONE_T); }                              // CFA
240otype                   { KEYWORD_RETURN(OTYPE); }                              // CFA
241register                { KEYWORD_RETURN(REGISTER); }
242restrict                { KEYWORD_RETURN(RESTRICT); }                   // C99
243__restrict              { KEYWORD_RETURN(RESTRICT); }                   // GCC
244__restrict__    { KEYWORD_RETURN(RESTRICT); }                   // GCC
245return                  { KEYWORD_RETURN(RETURN); }
246short                   { KEYWORD_RETURN(SHORT); }
247signed                  { KEYWORD_RETURN(SIGNED); }
248__signed                { KEYWORD_RETURN(SIGNED); }                             // GCC
249__signed__              { KEYWORD_RETURN(SIGNED); }                             // GCC
250sizeof                  { KEYWORD_RETURN(SIZEOF); }
251static                  { KEYWORD_RETURN(STATIC); }
252_Static_assert  { KEYWORD_RETURN(STATICASSERT); }               // C11
253struct                  { KEYWORD_RETURN(STRUCT); }
254switch                  { KEYWORD_RETURN(SWITCH); }
255thread                  { KEYWORD_RETURN(THREAD); }                             // C11
256_Thread_local   { KEYWORD_RETURN(THREADLOCAL); }                // C11
257throw                   { KEYWORD_RETURN(THROW); }                              // CFA
258throwResume             { KEYWORD_RETURN(THROWRESUME); }                // CFA
259trait                   { KEYWORD_RETURN(TRAIT); }                              // CFA
260try                             { KEYWORD_RETURN(TRY); }                                // CFA
261ttype                   { KEYWORD_RETURN(TTYPE); }                              // CFA
262typedef                 { KEYWORD_RETURN(TYPEDEF); }
263typeof                  { KEYWORD_RETURN(TYPEOF); }                             // GCC
264__typeof                { KEYWORD_RETURN(TYPEOF); }                             // GCC
265__typeof__              { KEYWORD_RETURN(TYPEOF); }                             // GCC
266union                   { KEYWORD_RETURN(UNION); }
267unsigned                { KEYWORD_RETURN(UNSIGNED); }
268__builtin_va_list { KEYWORD_RETURN(VALIST); }                   // GCC
269void                    { KEYWORD_RETURN(VOID); }
270volatile                { KEYWORD_RETURN(VOLATILE); }
271__volatile              { KEYWORD_RETURN(VOLATILE); }                   // GCC
272__volatile__    { KEYWORD_RETURN(VOLATILE); }                   // GCC
273while                   { KEYWORD_RETURN(WHILE); }
274zero_t                  { NUMERIC_RETURN(ZERO_T); }                             // CFA
275
276                                /* identifier */
277{identifier}    { IDENTIFIER_RETURN(); }
278{attr_identifier} { ATTRIBUTE_RETURN(); }
279"`"                             { BEGIN BKQUOTE; }
280<BKQUOTE>{identifier} { IDENTIFIER_RETURN(); }
281<BKQUOTE>"`"    { BEGIN 0; }
282
283                                /* numeric constants */
284"0"                             { NUMERIC_RETURN(ZERO); }                               // CFA
285"1"                             { NUMERIC_RETURN(ONE); }                                // CFA
286{decimal_constant} { NUMERIC_RETURN(INTEGERconstant); }
287{octal_constant} { NUMERIC_RETURN(INTEGERconstant); }
288{hex_constant}  { NUMERIC_RETURN(INTEGERconstant); }
289{real_decimal}  { NUMERIC_RETURN(REALDECIMALconstant); } // must appear before floating_constant
290{real_fraction} { NUMERIC_RETURN(REALFRACTIONconstant); } // must appear before floating_constant
291{floating_constant}     { NUMERIC_RETURN(FLOATINGconstant); }
292{hex_floating_constant} { NUMERIC_RETURN(FLOATINGconstant); }
293
294                                /* character constant, allows empty value */
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); }
298                                /* ' stop highlighting */
299
300                                /* string constant */
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); }
304                                /* " stop highlighting */
305
306                                /* common character/string constant */
307<QUOTE,STRING>{escape_seq} { rm_underscore(); strtext->append( yytext, yyleng ); }
308<QUOTE,STRING>"\\"{h_white}*"\n" {}                                             // continuation (ALSO HANDLED BY CPP)
309<QUOTE,STRING>"\\" { strtext->append( yytext, yyleng ); } // unknown escape character
310
311                                /* punctuation */
312"@"                             { ASCIIOP_RETURN(); }
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
323"..."                   { NAMEDOP_RETURN(ELLIPSIS); }
324
325                                /* alternative C99 brackets, "<:" & "<:<:" handled by preprocessor */
326"<:"                    { RETURN_VAL('['); }
327":>"                    { RETURN_VAL(']'); }
328"<%"                    { RETURN_VAL('{'); }
329"%>"                    { RETURN_VAL('}'); }
330
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(); }
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); }
368
369"@="                    { NAMEDOP_RETURN(ATassign); }                   // CFA
370
371                                /* CFA, operator identifier */
372{op_unary}"?"   { IDENTIFIER_RETURN(); }                                // unary
373"?"({op_unary_pre_post}|"()"|"[?]"|"{}") { IDENTIFIER_RETURN(); }
374"^?{}" { IDENTIFIER_RETURN(); }
375"?"{op_binary_over}"?"  { IDENTIFIER_RETURN(); }                // binary
376        /*
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;
401        */
402{op_unary}"?"({op_unary_pre_post}|"()"|"[?]"|{op_binary_over}"?") {
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 */
414.                       { printf("unknown character(s):\"%s\" on line %d\n", yytext, yylineno); }
415
416%%
417
418// Local Variables: //
419// mode: c++ //
420// tab-width: 4 //
421// compile-command: "make install" //
422// End: //
Note: See TracBrowser for help on using the repository browser.