00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 %{
00023 #include <string>
00024 #include <limits>
00025 #include <cstdlib>
00026 #include "driver.hh"
00027 #include "parser.hh"
00028 #include "lexer.hh"
00029
00030 #define yyterminate() return token::END
00031
00032 #define YY_USER_ACTION yylloc->columns (YYLeng());
00033
00034 #ifdef YY_DECL
00035 #undef YY_DECL
00036 #endif
00037 #define YY_DECL \
00038 yy::parser::token_type \
00039 yy::lexer::yylex(yy::parser::semantic_type* yylval, \
00040 yy::parser::location_type* yylloc)
00041 %}
00042
00043 %option noyywrap nounput batch debug c++ yyclass="lexer"
00044
00045 %x QS
00046
00047 ws [ \t\f]
00048 nl \r\n|\n|\r
00049 word [a-zA-Z_][a-zA-Z_0-9]*
00050
00051 %%
00052
00053
00054
00055
00056
00057 %{
00058 yylloc->step();
00059 typedef yy::parser::token token;
00060 std::string strbuf;
00061 %}
00062
00063 {ws}+ yylloc->step();
00064 {nl} yylloc->lines(1); yylloc->step();
00065
00066 [*/,{};=+-] return yy::parser::token_type (YYText()[0]);
00067
00068 [0-9]+ {
00069 errno = 0;
00070 long n = strtol(YYText(), NULL, 10);
00071 if (n < 0 || n > std::numeric_limits<int>::max() || errno == ERANGE) {
00072 theDriver.error(*yylloc, "integer is out of range");
00073 }
00074 yylval->i = n;
00075 return token::INT;
00076 }
00077
00078 \"\*\" return yy::parser::token_type ('*');
00079
00080 {word} yylval->s = new std::string(YYText()); return token::WORD;
00081
00082 \" strbuf=""; BEGIN(QS);
00083 <QS>[^"\r\n]+ strbuf += YYText();
00084 <QS>{nl} strbuf += "\n"; yylloc->lines(1);
00085 <QS>\" {
00086 yylval->s = new std::string(strbuf);
00087 BEGIN(INITIAL);
00088 return token::QSTR;
00089 }
00090
00091 . theDriver.error(*yylloc, "invalid character");
00092
00093
00094
00095
00096
00097
00098 %%
00099
00100 namespace yy {
00101
00102 lexer::lexer(driver &d, std::istream* in, std::ostream* out)
00103 : yyFlexLexer(in, out), theDriver(d) {
00104 }
00105
00106 lexer::~lexer() {
00107 }
00108
00109 void lexer::LexerError(const char* msg) {
00110 theDriver.error(msg);
00111 exit(YY_EXIT_FAILURE);
00112 }
00113
00114 }