// Author:
yijian
// Date:
2015/01/20
%option yylineno
// flex
mooon.l
%{
#include "mooon.tab.h"// bison编译mooon.y时生成的文件
#include "service_info.h"
//定义保留关键词
void reserved_keyword(const char* keyword)
{
yyerror("Cannot use reserved language keyword:
\"%s\"\n", keyword);
exit(1);
}
//整数大小溢出判断
void integer_overflow(const char* text)
{
yyerror("This integer is too big: \"%s\"\n",
text);
exit(1);
}
//无效的单词
void unexpected_token(const char* text)
{
yyerror("Unexpected token: \"%s\"\n", text);
exit(1);
}
%}
//下面的定义,类似于C/C++语言中的宏定义,另外请注意本区域内的注释不能顶格
intconstant
([+-]?[0-9]+)
dubconstant
([+-]?[0-9]*(\.[0-9]+)?([eE][+-]?[0-9]+)?)
identifier
([a-zA-Z_][\.a-zA-Z_0-9]*)
whitespace
([ \t\r\n]*)
sillycomm
("/*""*"*"*/")
multicomm
("/*"[^*]"/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/")
comment
("//"[^\n]*)
unixcomment
("#"[^\n]*)
symbol
([:;\,\{\}\(\)\=<>\[\]])
%%
//下面是模式,以及模式对应的动作,也请注意本区域内的注释不能顶格
{whitespace}
{ }
{sillycomm}
{ }
{multicomm}
{ }
{comment} { }
{unixcomment}
{ }
"request" { yylval.sval = strdup(yytext);
return tok_request; }
"response" { yylval.sval = strdup(yytext);
return tok_response; }
"string" { yylval.sval = strdup(yytext);
return tok_string; }
"bool" { yylval.sval =
strdup(yytext); return tok_bool;
}
"int16" { yylval.sval = strdup(yytext);
return tok_int16; }
"int32" { yylval.sval = strdup(yytext);
return tok_int32; }
"int64" { yylval.sval = strdup(yytext);
return tok_int64; }
"double" { yylval.sval = strdup(yytext);
return tok_double; }
"required" { yylval.sval = strdup(yytext);
return tok_required; }
"optional" { yylval.sval = strdup(yytext);
return tok_optional; }
{symbol} { return yytext[0]; }
"service" { reserved_keyword(yytext); }
"struct" { reserved_keyword(yytext); }
{intconstant} {
errno =
0;
yylval.iconst = strtoll(yytext, NULL, 10);
if
(ERANGE == errno)
{
integer_overflow(yytext);
}
return
tok_int_constant;
}
{dubconstant} {
yylval.dconst = atof(yytext);
return
tok_dub_constant;
}
{identifier} { yylval.sval = strdup(yytext);
return tok_identifier; }
.
{ unexpected_token(yytext); }
%%
//如果定义了选项“%option noyywrap”,则不需要下面的yywrap()函数
int yywrap()
{
return 1;
}