%%
/////////////////////////////////////////////////////////////////////////////
// rules section
// place your Lex rules here
"else" {printf("ELSE:%s%c ",yytext,' ');return ELSE;}
"if" {printf("IF:%s%c ",yytext,' ');return IF;}
"int" {printf("INT:%s%c ",yytext,' ');return INT;}
"return" {printf("RETURN:%s%c ",yytext,' ');return RETURN;}
"void" {printf("VOID:%s%c ",yytext,' ');return VOID;}
"while" {printf("WHILE:%s%c ",yytext,' ');return WHILE;}
"<" {printf("LT:%s%c ",yytext,' ');return LT;}
">" {printf("GT:%s%c ",yytext,' ');return GT;}
"<=" {printf("LE:%s%c ",yytext,' ');return LE;}
">=" {printf("GE:%s%c ",yytext,' ');return GE;}
"==" {printf("EQ:%s%c ",yytext,' ');return EQ;}
"!=" {printf("NE:%s%c ",yytext,' ');return NE;}
"{" {printf("{:%s%c ",yytext,' '); return '{';}
"}" {printf("}:%s%c ",yytext,' '); return '}';}
"(" {printf("(:%s%c ",yytext,' ');return '(';}
")" {printf("):%s%c ",yytext,' ');return ')';}
"[" {printf("[:%s%c ",yytext,' ');return '[';}
"]" {printf("]:%s%c ",yytext,' ');return ']';}
"+" {printf("+:%s%c ",yytext,' ');return '+';}
"-" {printf("-:%s%c ",yytext,' ');return '-';}
"*" {printf("*:%s%c ",yytext,' ');return '*';}
"/" {printf("/:%s%c ",yytext,' ');return '/';}
"%" {printf("%:%s%c ",yytext,' ');return '%';}
";" {printf(";:%s%c ",yytext,' ');return ';';}
"=" {printf("=:%s%c ",yytext,' ');return '=';}
"," {printf(",:%s%c ",yytext,' ');return ',';}
{id} {printf("id:%s%c ",yytext,' ');return ID;}
{number} {printf("number:%s%c ",yytext,' '); return NUMBER;}
"/*" {
char c;
int done = FALSE;
ECHO;
do
{ while((c=input())!='*')
putchar(c);
putchar(c);
while((c=input())=='*')
putchar(c);
putchar(c);
if(c=='/') done =TRUE;
} while(!done);
printf("\n: There are the conment!");
return COMMENT;
}
%%
程序可以正确的运行,供有需要的同学使用
1