📄 number1.l
字号:
%{
#include <string.h>
#include <stdlib.h>
#define LEN sizeof(struct ident)
struct ident
{
char *str;
int num;
struct ident *next;
};
struct ident *Keyword=NULL;
struct ident *Identifier=NULL;
FILE *fp;
char *Integer[100];
char *Float[100];
int IntNum=0, FloatNum=0,OperNum=0,SymNum=0; //这些用来计数
void InsertKey(char *newone)
{
struct ident *p,*q;
struct ident *news;
if(strcmp(newone,Keyword->str)==0)
{
Keyword->num++;
return;
}
p=Keyword;
q=Keyword;
p=p->next;
while(p!=NULL)
{
if( strcmp(newone,p->str)==0)
break;
p=p->next;
q=q->next;
}
if(p==NULL)
{
news=(struct ident *)malloc(LEN);
news->str=(char *)malloc(sizeof(newone)+1);
strcpy(news->str,newone);
news->num=1;
q->next=news;
news->next=NULL;
}
else
{
p->num++;
}
}
void InsertId(char *newone)
{
struct ident *p,*q;
struct ident *news;
if(strcmp(newone,Identifier->str)==0)
{
Identifier->num++;
return;
}
p=Identifier;
q=Identifier;
p=p->next;
while(p!=NULL)
{
if( strcmp(newone,p->str)==0)
break;
p=p->next;
q=q->next;
}
if(p==NULL)
{
news=(struct ident *)malloc(LEN);
news->str=(char *)malloc(sizeof(newone)+1);
strcpy(news->str,newone);
news->num=1;
q->next=news;
news->next=NULL;
}
else
{
p->num++;
}
}
void AddToKey(char *newone)
{
if(Keyword==NULL)
{
Keyword=(struct ident *)malloc(LEN);
Keyword->str=(char *)malloc(sizeof(newone)+1);
strcpy(Keyword->str,newone);
Keyword->num=1;
Keyword->next=NULL;
}
else
{
InsertKey(newone);
}
}
void AddToId(char *newone)
{
if(Identifier==NULL)
{
Identifier=(struct ident *)malloc(LEN);
Identifier->str=(char *)malloc(sizeof(newone)+1);
strcpy(Identifier->str,newone);
Identifier->num=1;
Identifier->next=NULL;
}
else
{
InsertId(newone);
}
}
void print()
{
int i,j;
struct ident *s,*p;
p=Keyword;
fprintf(fp,"关键字如下:\n");
while(p!=NULL)
{
fprintf(fp,"%20s %3d time(s)\n",p->str,p->num);
p=p->next;
}
fprintf(fp,"识别的变量如下:\n");
s=Identifier;
while(s!=NULL)
{
fprintf(fp,"%20s %3d time(s)\n",s->str,s->num);
s=s->next;
}
fprintf(fp,"整数总数: %d\n",IntNum);
for(i=0;i<IntNum;i++)
fprintf(fp,"%s\n",Integer[i]);
fprintf(fp,"\n");
fprintf(fp,"浮点数总数: %d\n",FloatNum);
for(j=0;j<FloatNum;j++)
fprintf(fp,"%s\n",Float[j]);
fprintf(fp,"\n");
fprintf(fp,"运算符号:(+-*/=())共有 %d time(s)\n",OperNum);
fprintf(fp,"标点符号:(,;)共有 %d time(s)\n",SymNum);
}
%}
INTEGER [0-9]
ID [a-zA-Z][A-Za-z0-9]*
%%
{INTEGER}+ {Integer[IntNum]=(char *)malloc(sizeof(yytext)+1);strcpy(Integer[IntNum],yytext); IntNum++;}//当出现整数时
{INTEGER}+"."{INTEGER}* {Float[FloatNum]=(char *)malloc(sizeof(yytext)+1);strcpy(Float[FloatNum],yytext); FloatNum++;} //当出现浮点数时
if|then|begin|end|procedure|while|do|read|write|call|const|var|odd { AddToKey(yytext); }
{ID} { AddToId(yytext); } //当出现其他字符串时
"+"|"-"|"*"|"/"|"="|"("|")" { OperNum++; } //当出现这些运算浮时
";"|"," { SymNum++; } //当出现; 和 ,时
"{"[^}\n]*"}" { } //将这些符号略去,不做统计
[ \t\n]+ {} //空格和换行浮不做统计
. {
fprintf( fp,"Unrecognized: %s\n", yytext );
}
%%
int yywrap()
{
return 1;
}
main( argc, argv )
int argc;
char **argv;
{
++argv; --argc;
if( argc > 0)
{
yyin = fopen( argv[0], "r" );
fp=fopen("output.txt","w");
yylex();
fprintf(fp,"\n 下面是词法分析结果 : \n");
print();
fclose(fp);
printf("词法分析结束,请打开output.txt查看结果 .\n");
}
else
{
printf("对不起,没有找到源文件。请重试!! \n");
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -