[6b0b624] | 1 | /* |
---|
[7d4f6ed] | 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. |
---|
[fda8168] | 6 | * |
---|
[7d4f6ed] | 7 | * lex.ll -- |
---|
[fda8168] | 8 | * |
---|
[fc1ef62] | 9 | * Author : Peter A. Buhr |
---|
[fda8168] | 10 | * Created On : Sat Dec 15 11:45:59 2001 |
---|
| 11 | * Last Modified By : Peter A. Buhr |
---|
[bd946e4] | 12 | * Last Modified On : Thu May 31 08:49:58 2018 |
---|
| 13 | * Update Count : 274 |
---|
[fda8168] | 14 | */ |
---|
| 15 | |
---|
| 16 | %option stack |
---|
| 17 | %option yylineno |
---|
[fc1ef62] | 18 | %option nounput |
---|
[fda8168] | 19 | |
---|
| 20 | %{ |
---|
| 21 | #include <list> |
---|
| 22 | #include <string> |
---|
| 23 | #include <iostream> |
---|
[7d4f6ed] | 24 | using namespace std; |
---|
[8c97ee7] | 25 | #include "ParserTypes.h" |
---|
[7d4f6ed] | 26 | #include "parser.hh" |
---|
[fda8168] | 27 | |
---|
| 28 | #define RETURN_TOKEN( kind ) yylval.tokenp = new Token( yytext, ws_list, kind ); return kind; |
---|
| 29 | |
---|
| 30 | list<string> ws_list; |
---|
| 31 | string comment_str; |
---|
| 32 | string code_str; |
---|
[fc1ef62] | 33 | |
---|
| 34 | // Stop warning due to incorrectly generated flex code. |
---|
| 35 | #pragma GCC diagnostic ignored "-Wsign-compare" |
---|
[fda8168] | 36 | %} |
---|
| 37 | |
---|
| 38 | integer [0-9]+ |
---|
| 39 | identifier [a-zA-Z_$][0-9a-zA-Z_$]* |
---|
| 40 | |
---|
| 41 | simple_escape ['"?\\] |
---|
| 42 | escape_sequence [\\]{simple_escape} |
---|
| 43 | c_char [^'\\\n]|{escape_sequence} |
---|
| 44 | s_char [^"\\\n]|{escape_sequence} |
---|
| 45 | |
---|
| 46 | %x C_COMMENT STR C_CODE |
---|
| 47 | |
---|
| 48 | /* ---------------------------- Token Section ---------------------------- */ |
---|
| 49 | %% |
---|
[fc1ef62] | 50 | <INITIAL,C_CODE>"/*" { // C style comments */ |
---|
| 51 | #if defined(DEBUG_ALL) | defined(DEBUG_COMMENT) |
---|
[81bb114] | 52 | cerr << "\"/*\" : " << yytext << endl; |
---|
[fc1ef62] | 53 | #endif |
---|
[81bb114] | 54 | if ( YYSTATE == C_CODE ) code_str += yytext; |
---|
| 55 | else comment_str += yytext; |
---|
| 56 | yy_push_state(C_COMMENT); |
---|
[fc1ef62] | 57 | } |
---|
| 58 | <C_COMMENT>(.|"\n") { // C style comments |
---|
| 59 | #if defined(DEBUG_ALL) | defined(DEBUG_COMMENT) |
---|
[81bb114] | 60 | cerr << "<C_COMMENT>(.|\\n) : " << yytext << endl; |
---|
[fc1ef62] | 61 | #endif |
---|
[81bb114] | 62 | if ( yy_top_state() == C_CODE ) code_str += yytext; |
---|
| 63 | else comment_str += yytext; |
---|
[fc1ef62] | 64 | } |
---|
| 65 | <C_COMMENT>"*/" { // C style comments |
---|
| 66 | #if defined(DEBUG_ALL) | defined(DEBUG_COMMENT) |
---|
| 67 | cerr << "<C_COMMENT>\"*/\" : " << yytext << endl; |
---|
| 68 | #endif |
---|
| 69 | if ( yy_top_state() == C_CODE ) code_str += yytext; |
---|
| 70 | else { |
---|
| 71 | comment_str += yytext; |
---|
| 72 | //cerr << "C COMMENT : " << endl << comment_str << endl; |
---|
| 73 | ws_list.push_back( comment_str ); |
---|
| 74 | comment_str = ""; |
---|
| 75 | } |
---|
| 76 | yy_pop_state(); |
---|
| 77 | } |
---|
| 78 | |
---|
[bd946e4] | 79 | <INITIAL,C_CODE>"//"[^\n]* { // C++ style comments |
---|
[fc1ef62] | 80 | #if defined(DEBUG_ALL) | defined(DEBUG_COMMENT) |
---|
| 81 | cerr << "\"//\"[^\\n]*\"\n\" : " << yytext << endl; |
---|
| 82 | #endif |
---|
| 83 | if ( YYSTATE == C_CODE ) code_str += yytext; |
---|
| 84 | else { |
---|
| 85 | comment_str += yytext; |
---|
| 86 | //cerr << "C++ COMMENT : " << endl << comment_str << endl; |
---|
| 87 | ws_list.push_back( comment_str ); |
---|
| 88 | comment_str = ""; |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | ";" { RETURN_TOKEN( ';' ) } |
---|
| 93 | ":" { RETURN_TOKEN( ':' ) } |
---|
| 94 | "|" { RETURN_TOKEN( '|' ) } |
---|
| 95 | "," { RETURN_TOKEN( ',' ) } |
---|
| 96 | "<" { RETURN_TOKEN( '<' ) } |
---|
| 97 | ">" { RETURN_TOKEN( '>' ) } |
---|
| 98 | |
---|
| 99 | [[:space:]]+ { // [ \t\n]+ |
---|
| 100 | ws_list.push_back( yytext ); |
---|
| 101 | //cerr << "WS : " << "\"" << yytext << "\"" << endl; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | <INITIAL>"{" { RETURN_TOKEN( '{' ) } |
---|
| 105 | <INITIAL>"}" { RETURN_TOKEN( '}' ) } |
---|
| 106 | <C_CODE>"{" { |
---|
| 107 | #if defined(DEBUG_ALL) | defined(DEBUG_C) |
---|
| 108 | cerr << "<C_CODE>. : " << yytext << endl; |
---|
| 109 | #endif |
---|
| 110 | code_str += yytext; |
---|
| 111 | RETURN_TOKEN( '{' ) |
---|
| 112 | } |
---|
| 113 | <C_CODE>"}" { |
---|
| 114 | #if defined(DEBUG_ALL) | defined(DEBUG_C) |
---|
| 115 | cerr << "<C_CODE>. : " << yytext << endl; |
---|
| 116 | #endif |
---|
| 117 | code_str += yytext; |
---|
| 118 | RETURN_TOKEN( '}' ) |
---|
| 119 | } |
---|
[fda8168] | 120 | |
---|
| 121 | "%%" { RETURN_TOKEN( MARK ) } |
---|
| 122 | "%{" { RETURN_TOKEN( LCURL ) } |
---|
[fc1ef62] | 123 | <C_CODE>"%}" { RETURN_TOKEN( RCURL ) } |
---|
| 124 | |
---|
[81bb114] | 125 | ^"%define"[^\n]*"\n" { RETURN_TOKEN( DEFINE ) } |
---|
| 126 | ^"%expect" { RETURN_TOKEN( EXPECT ) } |
---|
| 127 | ^"%left" { RETURN_TOKEN( LEFT ) } |
---|
| 128 | ^"%locations" { RETURN_TOKEN( LOCATIONS ) } |
---|
| 129 | ^"%nonassoc" { RETURN_TOKEN( NONASSOC ) } |
---|
| 130 | ^"%precedence" { RETURN_TOKEN( PRECEDENCE ) } |
---|
[fc1ef62] | 131 | ^"%pure_parser" { RETURN_TOKEN( PURE_PARSER ) } |
---|
[81bb114] | 132 | ^"%right" { RETURN_TOKEN( RIGHT ) } |
---|
[fc1ef62] | 133 | ^"%semantic_parser" { RETURN_TOKEN( SEMANTIC_PARSER ) } |
---|
[81bb114] | 134 | ^"%start" { RETURN_TOKEN( START ) } |
---|
| 135 | ^"%thong" { RETURN_TOKEN( THONG ) } |
---|
| 136 | ^"%token" { RETURN_TOKEN( TOKEN ) } |
---|
| 137 | ^"%type" { RETURN_TOKEN( TYPE ) } |
---|
| 138 | ^"%union" { RETURN_TOKEN( UNION ) } |
---|
[fda8168] | 139 | |
---|
[81bb114] | 140 | "%prec" { RETURN_TOKEN( PREC ) } |
---|
[fda8168] | 141 | |
---|
[81bb114] | 142 | {integer} { RETURN_TOKEN( INTEGER ); } |
---|
| 143 | [']{c_char}['] { RETURN_TOKEN( CHARACTER ); } |
---|
| 144 | {identifier} { RETURN_TOKEN( IDENTIFIER ); } |
---|
[fc1ef62] | 145 | |
---|
| 146 | <C_CODE>["]{s_char}*["] { // hide braces "{}" in strings |
---|
| 147 | #if defined(DEBUG_ALL) | defined(DEBUG_C) |
---|
| 148 | cerr << "<C_CODE>. : " << yytext << endl; |
---|
| 149 | #endif |
---|
| 150 | code_str += yytext; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | <C_CODE>(.|\n) { // must be last rule of C_CODE |
---|
| 154 | #if defined(DEBUG_ALL) | defined(DEBUG_C) |
---|
| 155 | cerr << "<C_CODE>. : " << yytext << endl; |
---|
| 156 | #endif |
---|
| 157 | code_str += yytext; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | /* unknown characters */ |
---|
| 161 | . { printf("unknown character(s):\"%s\" on line %d\n", yytext, yylineno); } |
---|
[fda8168] | 162 | %% |
---|
| 163 | void lexC(void) { |
---|
[81bb114] | 164 | BEGIN(C_CODE); |
---|
[fda8168] | 165 | } |
---|
| 166 | |
---|
| 167 | string lexYacc(void) { |
---|
[81bb114] | 168 | BEGIN(INITIAL); |
---|
| 169 | //cerr << "CODE: " << endl << code_str << endl; |
---|
| 170 | string temp( code_str ); |
---|
| 171 | code_str = ""; |
---|
| 172 | return temp; |
---|
[fda8168] | 173 | } |
---|
| 174 | |
---|
[7d4f6ed] | 175 | // Local Variables: // |
---|
| 176 | // mode: c++ // |
---|
| 177 | // tab-width: 4 // |
---|
| 178 | // compile-command: "make install" // |
---|
| 179 | // End: // |
---|