1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| %{ #include <iostream> #include <cstdlib> using namespace std; enum {T_N_TOKEN,T_FUNC, T_INT, T_PACKAGE, T_LCB, T_RCB, T_LPAREN, T_RPAREN, T_ID, T_WHITESPACE, T_WHITESPACE_N, T_AND, T_ASSIGN, T_BOOLTYPE, T_BREAK, T_COMMA, T_CHARCONSTANT, T_COMMENT, T_CONTINUE, T_DIV, T_DOT, T_ELSE, T_EQ, T_EXTERN, T_FALSE, T_FOR, T_GEQ, T_GT, T_IF, T_INTCONSTANT, T_INTTYPE, T_LEFTSHIFT, T_LEQ, T_LSB, T_LT, T_MINUS, T_MOD, T_MULT, T_NEQ, T_NOT, T_NULL, T_OR, T_PLUS, T_RIGHTSHIFT, T_RSB, T_SEMICOLON, T_STRINGCONSTANT, T_STRINGTYPE, T_TRUE, T_VAR, T_VOID, T_WHILE, T_RETURN };
string & covert_newline(string & s){ string tmp = ""; for(size_t i = 0; i < s.size(); i++) if(s[i] == '\n') tmp += "\\n"; else tmp += s[i]; s = tmp; return s; }
%}
%%
func { return T_FUNC; } return { return T_RETURN; } while { return T_WHILE; } void { return T_VOID; } var { return T_VAR; } string { return T_STRINGTYPE; } true { return T_TRUE; } null { return T_NULL; } int { return T_INTTYPE; } if { return T_IF; } extern { return T_EXTERN; } int { return T_INT; } for { return T_FOR; } package { return T_PACKAGE; } break { return T_BREAK; } continue { return T_CONTINUE; } , { return T_COMMA; } else { return T_ELSE; } false { return T_FALSE; } == { return T_EQ; } >= { return T_GEQ; } > { return T_GT; } \<\< { return T_LEFTSHIFT; } >> { return T_RIGHTSHIFT; } \<= { return T_LEQ; } \[ { return T_LSB; } \] { return T_RSB; } \< { return T_LT; } \- { return T_MINUS; } \+ { return T_PLUS; } \% { return T_MOD; } \* { return T_MULT; } != { return T_NEQ; } ! { return T_NOT; } \|\| { return T_OR; } ; { return T_SEMICOLON; } \"([^\n"\\]|\\(a|b|t|n|v|f|r|\\|\'|\"))*\" { return T_STRINGCONSTANT; }
([0-9]+(\.[0-9]+)?)|(0[xX][0-9A-Fa-f]+) { return T_INTCONSTANT; } \'([^\n'\\]|\\(a|b|t|n|v|f|r|\\|\'|\"))\' { return T_CHARCONSTANT; } "//".*"\n" { return T_COMMENT; } \{ { return T_LCB; } \} { return T_RCB; } \( { return T_LPAREN; } \) { return T_RPAREN; } [a-zA-Z\_][a-zA-Z\_0-9]* { return T_ID; } \n+[\t\r\a\v\b ]* { return T_WHITESPACE_N; } [\t\r\a\v\b ]+ { return T_WHITESPACE; } && { return T_AND; } = { return T_ASSIGN; } \/ { return T_DIV; } "." { return T_DOT; } . { cerr << "Error: unexpected character in input" << endl; return -1; } %%
int main () { int token; string lexeme; while ((token = yylex())) { if (token > 0) { lexeme.assign(yytext); switch(token) { case T_FUNC: cout << "T_FUNC " << lexeme << endl; break; case T_INT: cout << "T_INT " << lexeme << endl; break; case T_PACKAGE: cout << "T_PACKAGE " << lexeme << endl; break; case T_LCB: cout << "T_LCB " << lexeme << endl; break; case T_RCB: cout << "T_RCB " << lexeme << endl; break; case T_LPAREN: cout << "T_LPAREN " << lexeme << endl; break; case T_RPAREN: cout << "T_RPAREN " << lexeme << endl; break; case T_ID: cout << "T_ID " << lexeme << endl; break; case T_WHITESPACE: cout << "T_WHITESPACE " << lexeme << endl; break; case T_WHITESPACE_N: cout << "T_WHITESPACE "; for( size_t i = 0; i < lexeme.size(); i++) if( lexeme[i] == '\n' ) cout << "\\n"; else cout << lexeme[i]; cout << endl; break;
case T_AND: cout << "T_AND " << lexeme << endl; break; case T_ASSIGN: cout << "T_ASSIGN " << lexeme << endl; break; case T_BOOLTYPE: cout << "T_BOOLTYPE " << lexeme << endl; break; case T_BREAK: cout << "T_BREAK " << lexeme << endl; break; case T_COMMA: cout << "T_COMMA " << lexeme << endl; break; case T_CHARCONSTANT: cout << "T_CHARCONSTANT " << lexeme << endl; break; case T_COMMENT: cout << "T_COMMENT " << covert_newline(lexeme) << endl; break; case T_CONTINUE: cout << "T_CONTINUE " << lexeme << endl; break; case T_DIV: cout << "T_DIV " << lexeme << endl; break; case T_DOT: cout << "T_DOT " << lexeme << endl; break; case T_ELSE: cout << "T_ELSE " << lexeme << endl; break; case T_EQ: cout << "T_EQ " << lexeme << endl; break; case T_EXTERN: cout << "T_EXTERN " << lexeme << endl; break; case T_FALSE: cout << "T_FALSE " << lexeme << endl; break; case T_FOR: cout << "T_FOR " << lexeme << endl; break; case T_GEQ: cout << "T_GEQ " << lexeme << endl; break; case T_GT: cout << "T_GT " << lexeme << endl; break; case T_IF: cout << "T_IF " << lexeme << endl; break; case T_INTCONSTANT: cout << "T_INTCONSTANT " << lexeme << endl; break; case T_INTTYPE: cout << "T_INTTYPE " << lexeme << endl; break; case T_LEFTSHIFT: cout << "T_LEFTSHIFT " << lexeme << endl; break; case T_LEQ: cout << "T_LEQ " << lexeme << endl; break; case T_LSB: cout << "T_LSB " << lexeme << endl; break; case T_LT: cout << "T_LT " << lexeme << endl; break; case T_MINUS: cout << "T_MINUS " << lexeme << endl; break; case T_MOD: cout << "T_MOD " << lexeme << endl; break; case T_MULT: cout << "T_MULT " << lexeme << endl; break; case T_NEQ: cout << "T_NEQ " << lexeme << endl; break; case T_NOT: cout << "T_NOT " << lexeme << endl; break; case T_NULL: cout << "T_NULL " << lexeme << endl; break; case T_OR: cout << "T_OR " << lexeme << endl; break; case T_PLUS: cout << "T_PLUS " << lexeme << endl; break; case T_RIGHTSHIFT: cout << "T_RIGHTSHIFT " << lexeme << endl; break; case T_RSB: cout << "T_RSB " << lexeme << endl; break; case T_SEMICOLON: cout << "T_SEMICOLON " << lexeme << endl; break; case T_STRINGCONSTANT: cout << "T_STRINGCONSTANT " << lexeme << endl; break; case T_STRINGTYPE: cout << "T_STRINGTYPE " << lexeme << endl; break; case T_TRUE: cout << "T_TRUE " << lexeme << endl; break; case T_VAR: cout << "T_VAR " << lexeme << endl; break; case T_VOID: cout << "T_VOID " << lexeme << endl; break; case T_WHILE: cout << "T_WHILE " << lexeme << endl; break; case T_RETURN: cout << "T_RETURN " << lexeme << endl; break; default: exit(EXIT_FAILURE); } } else { if (token < 0) { exit(EXIT_FAILURE); } } } exit(EXIT_SUCCESS); }
|