词法分析新版

我们的编程语言ZZZ包含到元素如下:

基本数据类型:int,double,Point , bool(true,false)
四则运算:+  - * / () 
bool 运算: < >  >= <=  == != &&, ||, !
Point:&(取地址), $(指针指向的内容,相当于C语言的*) , ->(指针成员)
结构体:struct ,  .(点)
数组:ID[LINTEGER]
循环: for break,continue(删掉了switch case)
条件:if/else
函数: 类型+标示符+(*)+{*},return
模板:Template ,T(T也是关键字)
注释:// 直接忽略注释行
分隔符:; ,(逗号) {} () [] <> ,


保留字:int, double, Point,bool, true ,false
          if,else,for, break, continue
          Template T , return
          struct ,  
 
和上次做词法分析实验的时候相比:
删掉:const, static , include , define, switch case
修改:    取地址&
    取指针指向的值改为$
    Template<T> 改为 Template,因为只有一个泛型类能替换(T成为一个关键字)
增加:    新增关键字T(用于泛型)

lex.l代码如下:


letter                    [A-Za-z]
digit                    [0-9]
double                    {digit}+\.{digit}+(E[+-]?{digit}+)?
%%        

        
int                     {printf("It is a keyword %s\n",yytext); return INT;}
double                     {printf("It is a keyword %s\n",yytext); return DOUBLE;}
Point                     {printf("It is a keyword %s\n",yytext); return (POINT); }
bool                     {printf("It is a keyword %s\n",yytext); return  BOOL;}
if                         {printf("It is a keyword %s\n",yytext); return  (IF);}
else                    {printf("It is a keyword %s\n",yytext); return (ELSE);}
for                        {printf("It is a keyword %s\n",yytext); return (FOR);}
break                    {printf("It is a keyword %s\n",yytext); return   (BREAK);}
continue                {printf("It is a keyword %s\n",yytext); return   (CONTINUE);}
Template                {printf("It is a keyword %s\n",yytext); return   (TEMPLATE);}
T                        {printf("It is a keyword %s\n",yytext); return T;}
struct                    {printf("It is a keyword %s\n",yytext); return   (STRUCT);}
return                    {printf("It is a keyword %s\n",yytext);  return (RETURN);}
true                      {printf("It is a literl bool %d\n",yylval.type_int);  yylval.type_int = 1;    return TRUE;}
false                      {printf("It is a literl bool %d\n",yylval.type_int);  yylval.type_int = 0;    return FALSE;}
[ \t\r]                   {}
{digit}+{letter}        {    //错误的标识符
                            fprintf(stderr,"Error identify in line %d: %s\n:",lines,yytext);}
{digit}+\.{digit}+\.    {    //错误的浮点数,例如
                            char c = input();
                            fprintf(stderr,"Error digits in line %d: %s",lines,yytext);
                            while(c != '\n'){
                                printf("%c",c);
                                c = input();
                            }
                            fprintf(stderr,"\n");
                        }

"\n"                    {lines++;}
[a-zA-Z_][a-zA-Z0-9_]*  {printf("It is a ID %s\n",yytext);          return ID ;}
{double}                   {    yylval.type_double = atof(yytext);
                            printf("It is a literal double %f\n",yylval.type_double);  
                            return LDOUBLE;
                        }
{digit}+                   {    yylval.type_int = atoi(yytext);
                            printf("It is a literal integer %d\n", yylval.type_int);  
                            return LINTEGER;
                        }
"&&"                    {printf("It is a logical AND %s\n",yytext);          return (AND);}
"||"                    {printf("It is a logical OR  %s\n",yytext);          return (OR);}
"!"                        {printf("It is a logical NOT %s\n",yytext);         return NOT;}
"="                     {printf("It is a assignop %s\n",yytext);         return (ASSIGN);}
"=="                    {printf("It is a relop equalence  %s\n",yytext);    return (EQ);}
"!="                    {printf("It is a relop not equal %s\n",yytext);     return (NE);}
"<"                     {printf("It is a relop less   %s\n",yytext);         return (LT);}
"<="                    {printf("It is a relop less equal  %s\n",yytext);     return (LE);}
">"                     {printf("It is a relop greater   %s\n",yytext);         return (GT);}
">="                    {printf("It is a relop greater equal  %s\n",yytext);         return (GE);}
"("                     {printf("It is a left parenthese  %s\n",yytext);     return (LP);}
")"                     {printf("It is a right parenthese  %s\n",yytext);     return (RP);}
"{"                     {printf("It is a left brace  %s\n",yytext);         return (LC);}
"}"                     {printf("It is a right brace  %s\n",yytext);         return (RC);}
"."                     {printf("It is a dot  %s\n",yytext);             return (DOT);}
","                     {printf("It is a comma  %s\n",yytext);            return (COMMA);}
"+"                     {printf("It is a plus sign  %s\n",yytext);         return (PLUS);}
"-"                     {printf("It is a minus sign  %s\n",yytext);         return (MINUS);}
"*"                     {printf("It is a star  %s\n",yytext);         return (MUL);}
"$"                        {printf("It is a point operation %s \n",yytext); return PADDR;}
"/"                     {printf("It is a division sign  %s\n",yytext);      return (DIV);}
"->"                    {printf("It is a arrow %s\n",yytext);             return ARROW;}
"//"                    {    char c = input();                //annotation;
                            printf("annotation in line %d: \\\\",++lines);
                            while(c != '\n') {
                                printf("%c",c);
                                c = input();
                            }
                            printf("\n");
                            
                        }
"&"                        {printf("It is a get address operation %s\n",yytext);         return GETADDR;}
";"                        {printf("It is a semicolon %s\n",yytext);         return  (SEMI);}
"["                        {printf("It is a LB %s\n",yytext);         return  (LB);}
"]"                        {printf("It is a RB %s\n",yytext);         return  (RB);}
<<EOF>>                    {printf("End of file. Lexcal analyse finish! Total lines is %d\n",lines-1); return -1;}
.                       {printf("Unknown token:%s in lines %d\n",yytext,lines); yyterminate();        }

 

 

转载于:https://www.cnblogs.com/heracles123/archive/2013/05/03/3058560.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值