| 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 Dec 15 11:45:59 2001 | 
|---|
| 11 | * Last Modified By : Peter A. Buhr | 
|---|
| 12 | * Last Modified On : Tue Aug 29 17:33:36 2017 | 
|---|
| 13 | * Update Count     : 268 | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | %option stack | 
|---|
| 17 | %option yylineno | 
|---|
| 18 | %option nounput | 
|---|
| 19 |  | 
|---|
| 20 | %{ | 
|---|
| 21 | #include <list> | 
|---|
| 22 | #include <string> | 
|---|
| 23 | #include <iostream> | 
|---|
| 24 | using namespace std; | 
|---|
| 25 | #include "ParserTypes.h" | 
|---|
| 26 | #include "parser.hh" | 
|---|
| 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; | 
|---|
| 33 |  | 
|---|
| 34 | // Stop warning due to incorrectly generated flex code. | 
|---|
| 35 | #pragma GCC diagnostic ignored "-Wsign-compare" | 
|---|
| 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 | %% | 
|---|
| 50 | <INITIAL,C_CODE>"/*" {                                                                  // C style comments */ | 
|---|
| 51 | #if defined(DEBUG_ALL) | defined(DEBUG_COMMENT) | 
|---|
| 52 | cerr << "\"/*\" : " << yytext << endl; | 
|---|
| 53 | #endif | 
|---|
| 54 | if ( YYSTATE == C_CODE ) code_str += yytext; | 
|---|
| 55 | else comment_str += yytext; | 
|---|
| 56 | yy_push_state(C_COMMENT); | 
|---|
| 57 | } | 
|---|
| 58 | <C_COMMENT>(.|"\n")     {                                                                       // C style comments | 
|---|
| 59 | #if defined(DEBUG_ALL) | defined(DEBUG_COMMENT) | 
|---|
| 60 | cerr << "<C_COMMENT>(.|\\n) : " << yytext << endl; | 
|---|
| 61 | #endif | 
|---|
| 62 | if ( yy_top_state() == C_CODE ) code_str += yytext; | 
|---|
| 63 | else comment_str += yytext; | 
|---|
| 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 |  | 
|---|
| 79 | <INITIAL,C_CODE>"//"[^\n]*"\n" {                                                // C++ style comments | 
|---|
| 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 | } | 
|---|
| 120 |  | 
|---|
| 121 | "%%"                    { RETURN_TOKEN( MARK ) } | 
|---|
| 122 | "%{"                    { RETURN_TOKEN( LCURL ) } | 
|---|
| 123 | <C_CODE>"%}"    { RETURN_TOKEN( RCURL ) } | 
|---|
| 124 |  | 
|---|
| 125 | ^"%union"       { RETURN_TOKEN( UNION ) } | 
|---|
| 126 | ^"%start"       { RETURN_TOKEN( START ) } | 
|---|
| 127 | ^"%token"       { RETURN_TOKEN( TOKEN ) } | 
|---|
| 128 | ^"%type"            { RETURN_TOKEN( TYPE ) } | 
|---|
| 129 | ^"%left"            { RETURN_TOKEN( LEFT ) } | 
|---|
| 130 | ^"%right"           { RETURN_TOKEN( RIGHT ) } | 
|---|
| 131 | ^"%nonassoc"    { RETURN_TOKEN( NONASSOC ) } | 
|---|
| 132 | ^"%precedence"  { RETURN_TOKEN( PRECEDENCE ) } | 
|---|
| 133 | ^"%pure_parser" { RETURN_TOKEN( PURE_PARSER ) } | 
|---|
| 134 | ^"%semantic_parser"     { RETURN_TOKEN( SEMANTIC_PARSER ) } | 
|---|
| 135 | ^"%expect"      { RETURN_TOKEN( EXPECT ) } | 
|---|
| 136 | ^"%thong"               { RETURN_TOKEN( THONG ) } | 
|---|
| 137 |  | 
|---|
| 138 | "%prec"                 { RETURN_TOKEN( PREC ) } | 
|---|
| 139 |  | 
|---|
| 140 | {integer}           { RETURN_TOKEN( INTEGER ); } | 
|---|
| 141 | [']{c_char}[']  { RETURN_TOKEN( CHARACTER ); } | 
|---|
| 142 | {identifier}    { RETURN_TOKEN( IDENTIFIER ); } | 
|---|
| 143 |  | 
|---|
| 144 | <C_CODE>["]{s_char}*["] {                                                               // hide braces "{}" in strings | 
|---|
| 145 | #if defined(DEBUG_ALL) | defined(DEBUG_C) | 
|---|
| 146 | cerr << "<C_CODE>. : " << yytext << endl; | 
|---|
| 147 | #endif | 
|---|
| 148 | code_str += yytext; | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | <C_CODE>(.|\n) {                                                                                // must be last rule of C_CODE | 
|---|
| 152 | #if defined(DEBUG_ALL) | defined(DEBUG_C) | 
|---|
| 153 | cerr << "<C_CODE>. : " << yytext << endl; | 
|---|
| 154 | #endif | 
|---|
| 155 | code_str += yytext; | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | /* unknown characters */ | 
|---|
| 159 | .                               { printf("unknown character(s):\"%s\" on line %d\n", yytext, yylineno); } | 
|---|
| 160 | %% | 
|---|
| 161 | void lexC(void) { | 
|---|
| 162 | BEGIN(C_CODE); | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | string lexYacc(void) { | 
|---|
| 166 | BEGIN(INITIAL); | 
|---|
| 167 | //cerr << "CODE: " << endl << code_str << endl; | 
|---|
| 168 | string temp( code_str ); | 
|---|
| 169 | code_str = ""; | 
|---|
| 170 | return temp; | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | // Local Variables: // | 
|---|
| 174 | // mode: c++ // | 
|---|
| 175 | // tab-width: 4 // | 
|---|
| 176 | // compile-command: "make install" // | 
|---|
| 177 | // End: // | 
|---|