主题:词法分析器
dong456chang
[专家分:0] 发布于 2005-04-23 20:13:00
各位大哥:
给小弟编一个词法分析器啊
回复列表 (共11个回复)
沙发
帝均 [专家分:270] 发布于 2005-04-25 20:43:00
说明白一点``可以吗?```要不然都不知要做什么````
3 楼
niuyongjie [专家分:10] 发布于 2005-04-28 23:04:00
[em1]
首先,我就不介绍Lex的语法规则了,因为在一些书上这些是重点介绍的内容,把Lex的源程序写在下面,然后讲解。
%{
#define NUMBER 257
#define PLUS 258
#define SUB 259
#define CHEN 260
#define DIV 261
#define LKUO 262
#define YKUO 263
#include <stdio.h>
#include <stdlib.h>
extern int yylval;
%}
number [0-9]+
%%
{number} {yylval=atoi(yytext);printf("%s",yytext);return NUMBER;}
"+" {printf("%s",yytext);return PLUS;}
"-" {printf("%s",yytext);return SUB;}
"*" {printf("%s",yytext);return CHEN;}
"/" {printf("%s",yytext);return DIV;}
"(" {printf("%s",yytext);return LKUO;}
")" {printf("%s",yytext);return YKUO;}
%%
4 楼
简Θ单 [专家分:2360] 发布于 2005-05-01 04:54:00
词法分析程序也要别人代劳,自己写是很容易的事情。
如果用其他辅助工具(如lex&yacc或ParGen等)做起来
就更容易了。
5 楼
hk18 [专家分:2230] 发布于 2005-05-08 22:36:00
多加分就行 ,啊哈哈哈哈
/*cifa fenxi chengxu*/
#include <stdio.h>
#include <ctype.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#define NULL 0
FILE *fp;
char cbuffer;
char *key[8]={"if","else","for","while","do","return","break","continue"};//保留字
char *border[6]={",",";","{","}","(",")"}; //界符
char *arithmetic[4]={"+","-","*","/"}; //运算符
char *relation[6]={"<","<=","=",">",">=","<>"}; //关系
char *consts[20];
char *label[20];
int constnum=0,labelnum=0;
int search(char searchchar[],int wordtype)
{ int i=0;
switch (wordtype) {
case 1:{for (i=0;i<=7;i++) //查找保留字
{ if (strcmp(key[i],searchchar)==0)
return(i+1);
}
return(1);break;
}
case 2:{for (i=0;i<=5;i++) //查找界符
{ if (strcmp(border[i],searchchar)==0)
{ return(i+1);
}
}
return(1);break;
}
case 3:{for (i=0;i<=3;i++) //查找运算符
{ if (strcmp(arithmetic[i],searchchar)==0)
{ return(i+1);
}
}
return(1);break;
}
case 4:{for (i=0;i<=5;i++) //查找关系运算符
{ if (strcmp(relation[i],searchchar)==0)
{ return(i+1);
}
}
return(1);break;
}
case 5:{for (i=0;i<=constnum;i++)
{ if (strcmp(consts[i],searchchar)==0)
{return(i+1);
}
}
consts[i-1]=(char *)malloc(sizeof(searchchar));
strcpy(consts[i-1],searchchar);
constnum++;
return(i);
}
case 6:{for (i=0;i<=labelnum;i++)
{ if (strcmp(label[i],searchchar)==0)
{ return(i+1);
}
}
label[i-1]=(char *)malloc(sizeof(searchchar));
strcpy(label[i-1],searchchar);
labelnum++;
return(i);
}
}
}
char alphaprocess(char buffer)
{ int atype;
int i=-1;
char alphatp[20];
while ((isalpha(buffer))||(isdigit(buffer)))
{ alphatp[++i]=buffer;
buffer=fgetc(fp);
}
alphatp[i+1]='\0';
if (atype=search(alphatp,1))
printf("%s (1,%d)\n",alphatp,atype-1);
else { atype=search(alphatp,6);
printf("%s (6,%d)\n",alphatp,atype-1);
}
return(buffer);
}
char digitprocess(char buffer)
{ int i=-1;
char digittp[20];
int dtype;
while ((isdigit(buffer)))
{ digittp[++i]=buffer;
buffer=fgetc(fp);
}
digittp[i+1]='\0';
dtype=search(digittp,5);
printf("%s (5,%d)\n",digittp,dtype-1);
return(buffer);
}
char otherprocess(char buffer)
{ int i=-1;
char othertp[20];
int otype,otypetp;
othertp[0]=buffer;
othertp[1]='\0';
if (otype=search(othertp,3))
{ printf("%s (3,%d)\n",othertp,otype-1);
buffer=fgetc(fp);
goto out;
}
if (otype=search(othertp,4))
{ buffer=fgetc(fp);
othertp[1]=buffer;
othertp[2]='\0';
if (otypetp=search(othertp,4))
{ printf("%s (4,%d)\n",othertp,otypetp-1);
goto out;
}
else
othertp[1]='\0';
printf("%s (4,%d)\n",othertp,otype-1);
goto out;
}
if (buffer==':')
{ buffer=fgetc(fp);
if (buffer=='=')
printf(":= (2,2)\n");
buffer=fgetc(fp);
goto out;
}
else
{ if (otype=search(othertp,2))
{ printf("%s (2,%d)\n",othertp,otype-1);
buffer=fgetc(fp);
goto out;
}
}
if ((buffer!='\n')&&(buffer!=' '))
printf("%c error,not a word\n",buffer);
buffer=fgetc(fp);
out: return(buffer);
}
void main()
{
int i;
for (i=0;i<=20;i++)
{
label[i]="";
consts[i]="";
};
if ((fp=fopen("example.c","r"))==NULL)
printf("error");
else
{
cbuffer = fgetc(fp);
while (cbuffer!=EOF)
{
if (isalpha(cbuffer))
cbuffer=alphaprocess(cbuffer);
else if (isdigit(cbuffer))
cbuffer=digitprocess(cbuffer);
else cbuffer=otherprocess(cbuffer);
}
printf("over\n");
getchar();
}
}
自己在编一个 名为example.c就行了
6 楼
hk18 [专家分:2230] 发布于 2005-05-08 22:37:00
我今天第一次进这论坛
怎么很多朋友都在找词法分析器的代码啊?
哈哈
9 楼
hk18 [专家分:2230] 发布于 2005-05-11 22:07:00
谢也不给点分?吝啬!!!
我这几天才进论坛看看,就想多拿点分.哈哈
对这没兴趣,就不想进了!
10 楼
多学点点 [专家分:0] 发布于 2005-05-26 20:29:00
看来我的多学点英语了
我来回复