| 1 | /*                               -*- Mode: C -*- 
 | 
|---|
| 2 |  * 
 | 
|---|
| 3 |  * Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
 | 
|---|
| 4 |  *
 | 
|---|
| 5 |  * The contents of this file are covered under the licence agreement in the
 | 
|---|
| 6 |  * file "LICENCE" distributed with Cforall.
 | 
|---|
| 7 |  * 
 | 
|---|
| 8 |  * lex.ll --
 | 
|---|
| 9 |  * 
 | 
|---|
| 10 |  * Author           : Rodolfo Gabriel Esteves
 | 
|---|
| 11 |  * Created On       : Sat Dec 15 11:45:59 2001
 | 
|---|
| 12 |  * Last Modified By : Peter A. Buhr
 | 
|---|
| 13 |  * Last Modified On : Wed Jun 28 22:57:17 2017
 | 
|---|
| 14 |  * Update Count     : 253
 | 
|---|
| 15 |  */
 | 
|---|
| 16 | 
 | 
|---|
| 17 | %option stack
 | 
|---|
| 18 | %option yylineno
 | 
|---|
| 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 | 
 | 
|---|
| 35 | integer [0-9]+
 | 
|---|
| 36 | identifier [a-zA-Z_$][0-9a-zA-Z_$]*
 | 
|---|
| 37 | 
 | 
|---|
| 38 | simple_escape ['"?\\]
 | 
|---|
| 39 | escape_sequence [\\]{simple_escape}
 | 
|---|
| 40 | c_char [^'\\\n]|{escape_sequence}
 | 
|---|
| 41 | s_char [^"\\\n]|{escape_sequence}
 | 
|---|
| 42 | 
 | 
|---|
| 43 | %x C_COMMENT STR C_CODE
 | 
|---|
| 44 | 
 | 
|---|
| 45 | /* ---------------------------- Token Section ---------------------------- */
 | 
|---|
| 46 | %%
 | 
|---|
| 47 | <INITIAL,C_CODE>"/*"    {                               /* C style comments */
 | 
|---|
| 48 |                         #if defined(DEBUG_ALL) | defined(DEBUG_COMMENT)
 | 
|---|
| 49 |                             cerr << "\"/*\" : " << yytext << endl;
 | 
|---|
| 50 |                         #endif
 | 
|---|
| 51 |                             if ( YYSTATE == C_CODE ) code_str += yytext;
 | 
|---|
| 52 |                             else comment_str += yytext;
 | 
|---|
| 53 |                             yy_push_state(C_COMMENT);
 | 
|---|
| 54 |                         }
 | 
|---|
| 55 | <C_COMMENT>(.|"\n")     {                               /* C style comments */
 | 
|---|
| 56 |                         #if defined(DEBUG_ALL) | defined(DEBUG_COMMENT)
 | 
|---|
| 57 |                             cerr << "<C_COMMENT>(.|\\n) : " << yytext << endl;
 | 
|---|
| 58 |                         #endif
 | 
|---|
| 59 |                             if ( yy_top_state() == C_CODE ) code_str += yytext;
 | 
|---|
| 60 |                             else comment_str += yytext;
 | 
|---|
| 61 |                         }
 | 
|---|
| 62 | <C_COMMENT>"*/"         {                               /* C style comments */
 | 
|---|
| 63 |                         #if defined(DEBUG_ALL) | defined(DEBUG_COMMENT)
 | 
|---|
| 64 |                             cerr << "<C_COMMENT>\"*/\" : " << yytext << endl;
 | 
|---|
| 65 |                         #endif
 | 
|---|
| 66 |                             if ( yy_top_state() == C_CODE ) code_str += yytext;
 | 
|---|
| 67 |                             else {
 | 
|---|
| 68 |                                 comment_str += yytext;
 | 
|---|
| 69 |                                 //cerr << "C COMMENT : " << endl << comment_str << endl;
 | 
|---|
| 70 |                                 ws_list.push_back( comment_str );
 | 
|---|
| 71 |                                 comment_str = "";
 | 
|---|
| 72 |                             }
 | 
|---|
| 73 |                             yy_pop_state();
 | 
|---|
| 74 |                         }
 | 
|---|
| 75 | <INITIAL,C_CODE>"//"[^\n]*"\n" {                        /* C++ style comments */
 | 
|---|
| 76 |                         #if defined(DEBUG_ALL) | defined(DEBUG_COMMENT)
 | 
|---|
| 77 |                             cerr << "\"//\"[^\\n]*\"\n\" : " << yytext << endl;
 | 
|---|
| 78 |                         #endif
 | 
|---|
| 79 |                             if ( YYSTATE == C_CODE ) code_str += yytext;
 | 
|---|
| 80 |                             else {
 | 
|---|
| 81 |                                 comment_str += yytext;
 | 
|---|
| 82 |                                 //cerr << "C++ COMMENT : " << endl << comment_str << endl;
 | 
|---|
| 83 |                                 ws_list.push_back( comment_str );
 | 
|---|
| 84 |                                 comment_str = "";
 | 
|---|
| 85 |                             }
 | 
|---|
| 86 |                         }
 | 
|---|
| 87 | 
 | 
|---|
| 88 | ";"                     { RETURN_TOKEN( ';' ) }
 | 
|---|
| 89 | ":"                     { RETURN_TOKEN( ':' ) }
 | 
|---|
| 90 | "|"                     { RETURN_TOKEN( '|' ) }
 | 
|---|
| 91 | ","                     { RETURN_TOKEN( ',' ) }
 | 
|---|
| 92 | "<"                     { RETURN_TOKEN( '<' ) }
 | 
|---|
| 93 | ">"                     { RETURN_TOKEN( '>' ) }
 | 
|---|
| 94 | 
 | 
|---|
| 95 | [[:space:]]+            {                               /* [ \t\n]+ */
 | 
|---|
| 96 |                             ws_list.push_back( yytext );
 | 
|---|
| 97 |                             //cerr << "WS : " << "\"" << yytext << "\"" << endl;
 | 
|---|
| 98 |                         }
 | 
|---|
| 99 | 
 | 
|---|
| 100 | <INITIAL>"{"            { RETURN_TOKEN( '{' ) }
 | 
|---|
| 101 | <INITIAL>"}"            { RETURN_TOKEN( '}' ) }
 | 
|---|
| 102 | <C_CODE>"{"             {
 | 
|---|
| 103 |                         #if defined(DEBUG_ALL) | defined(DEBUG_C)
 | 
|---|
| 104 |                             cerr << "<C_CODE>. : " << yytext << endl;
 | 
|---|
| 105 |                         #endif
 | 
|---|
| 106 |                             code_str += yytext;
 | 
|---|
| 107 |                             RETURN_TOKEN( '{' )
 | 
|---|
| 108 |                         }
 | 
|---|
| 109 | <C_CODE>"}"             {
 | 
|---|
| 110 |                         #if defined(DEBUG_ALL) | defined(DEBUG_C)
 | 
|---|
| 111 |                             cerr << "<C_CODE>. : " << yytext << endl;
 | 
|---|
| 112 |                         #endif
 | 
|---|
| 113 |                             code_str += yytext;
 | 
|---|
| 114 |                             RETURN_TOKEN( '}' )
 | 
|---|
| 115 |                         }
 | 
|---|
| 116 | 
 | 
|---|
| 117 | "%%"                    { RETURN_TOKEN( MARK ) }
 | 
|---|
| 118 | "%{"                    { RETURN_TOKEN( LCURL ) }
 | 
|---|
| 119 | <C_CODE>"%}"            { RETURN_TOKEN( RCURL ) }
 | 
|---|
| 120 | 
 | 
|---|
| 121 | ^"%union"               { RETURN_TOKEN( UNION ) }
 | 
|---|
| 122 | ^"%start"               { RETURN_TOKEN( START ) }
 | 
|---|
| 123 | ^"%token"               { RETURN_TOKEN( TOKEN ) }
 | 
|---|
| 124 | ^"%type"                { RETURN_TOKEN( TYPE ) }
 | 
|---|
| 125 | ^"%left"                { RETURN_TOKEN( LEFT ) }
 | 
|---|
| 126 | ^"%right"               { RETURN_TOKEN( RIGHT ) }
 | 
|---|
| 127 | ^"%nonassoc"            { RETURN_TOKEN( NONASSOC ) }
 | 
|---|
| 128 | ^"%pure_parser"         { RETURN_TOKEN( PURE_PARSER ) }
 | 
|---|
| 129 | ^"%semantic_parser"     { RETURN_TOKEN( SEMANTIC_PARSER ) }
 | 
|---|
| 130 | ^"%expect"              { RETURN_TOKEN( EXPECT ) }
 | 
|---|
| 131 | ^"%thong"               { RETURN_TOKEN( THONG ) }
 | 
|---|
| 132 | 
 | 
|---|
| 133 | "%prec"                 { RETURN_TOKEN( PREC ) }
 | 
|---|
| 134 | 
 | 
|---|
| 135 | {integer}               { RETURN_TOKEN( INTEGER ); }
 | 
|---|
| 136 | [']{c_char}[']          { RETURN_TOKEN( CHARACTER ); }
 | 
|---|
| 137 | {identifier}            { RETURN_TOKEN( IDENTIFIER ); }
 | 
|---|
| 138 | 
 | 
|---|
| 139 | <C_CODE>["]{s_char}*["] {                               /* hide braces "{}" in strings */
 | 
|---|
| 140 |                         #if defined(DEBUG_ALL) | defined(DEBUG_C)
 | 
|---|
| 141 |                             cerr << "<C_CODE>. : " << yytext << endl;
 | 
|---|
| 142 |                         #endif
 | 
|---|
| 143 |                             code_str += yytext;
 | 
|---|
| 144 |                         }
 | 
|---|
| 145 | 
 | 
|---|
| 146 | <C_CODE>(.|\n)          {                               /* must be last rule of C_CODE */
 | 
|---|
| 147 |                         #if defined(DEBUG_ALL) | defined(DEBUG_C)
 | 
|---|
| 148 |                             cerr << "<C_CODE>. : " << yytext << endl;
 | 
|---|
| 149 |                         #endif
 | 
|---|
| 150 |                             code_str += yytext;
 | 
|---|
| 151 |                         }
 | 
|---|
| 152 | 
 | 
|---|
| 153 | .                       { printf("UNKNOWN CHARACTER:%s\n", yytext); } /* unknown characters */
 | 
|---|
| 154 | %%
 | 
|---|
| 155 | void lexC(void) {
 | 
|---|
| 156 |     BEGIN(C_CODE);
 | 
|---|
| 157 | }
 | 
|---|
| 158 | 
 | 
|---|
| 159 | string lexYacc(void) {
 | 
|---|
| 160 |     BEGIN(INITIAL);
 | 
|---|
| 161 |     //cerr << "CODE: " << endl << code_str << endl;
 | 
|---|
| 162 |     string temp( code_str );
 | 
|---|
| 163 |     code_str = "";
 | 
|---|
| 164 |     return temp;
 | 
|---|
| 165 | }
 | 
|---|
| 166 | 
 | 
|---|
| 167 | // Local Variables: //
 | 
|---|
| 168 | // mode: c++ //
 | 
|---|
| 169 | // tab-width: 4 //
 | 
|---|
| 170 | // compile-command: "make install" //
 | 
|---|
| 171 | // End: //
 | 
|---|