主题:哪位帅哥帮帮小弟呀,做个词法分析程序
zhdxy
[专家分:0] 发布于 2005-06-13 10:12:00
题目要求很简单:
1 输入为字符串,输出单词串,既由(单词,类别)所组成的二员组序列
2 有一定的错误检查能力,如能发现2a这类不能单词的字符串。
小弟在此跪谢了。
回复列表 (共4个回复)
沙发
zhdxy [专家分:0] 发布于 2005-06-13 10:24:00
对了,说明一下,要C语言版的
板凳
lyid [专家分:0] 发布于 2005-07-10 08:43:00
望能对你有些帮助!
词法分析:
#include <iostream.h>
#include <stdio.h>
#include <string.h>
FILE *f;
void noblank(char &ch)
{
while (ch==' '||ch=='\n')
ch=fgetc(f);
}
//====================================================================
void identifier(char name[],char &ch)
{
int i;
for (i=0;i<10;i++)
name[i]='\0';
i=0;
name[i]=ch;i++;
ch=fgetc(f);
while (('0'<=ch&&ch<='9')||('a'<=ch&&ch<='z')||('A'<=ch&&ch<='Z'))
{
name[i]=ch;
i++;
ch=fgetc(f);
}
}
//====================================================================
int intconst(char &ch)
{
int num=0;
while ('0'<=ch&&ch<='9')
{
num=num*10+(ch-'0');
ch=fgetc(f);
}
return num;
}
//====================================================================
void main()
{
char ch,name[20];
char st1[10]={"#include"};
char st2[10]={"main()"};
char st3[10]={"{"};
char st4[10]={"int"};
char st5[10]={"}"};
char st6[20]={"cout<<"};
int num;
f=fopen("prog.txt","r");
if (f==NULL)
cout<<"no such file."<<endl;
ch=fgetc(f);
while (!feof(f))
{
noblank(ch);
if ((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
{
identifier(name,ch);
if (!strcmp(name,st1)) printf("${\n");
else if (!strcmp(name,st2)) printf("$}\n");
else if (!strcmp(name,st3)) printf("$int\n");
else if (!strcmp(name,st4)) printf("$cout<<\n");
else if (!strcmp(name,st5)) printf("$#include\n");
else printf("$id,%s\n",name);
}
else if (ch>='0'&&ch<='9')
{
num=intconst(ch);
printf("$intc,%d\n",num);
}
else switch(ch)
{
case'(':cout<<"lparen"<<endl;
ch=fgetc(f);break;
case')':cout<<"rparen"<<endl;
ch=fgetc(f);break;
case'+':cout<<"plus"<<endl;
ch=fgetc(f);break;
case'*':cout<<"mult"<<endl;
ch=fgetc(f);break;
case';':cout<<"semi"<<endl;
ch=fgetc(f);break;
case'=':cout<<"give value"<<endl;
ch=fgetc(f);break;
case'.':cout<<"point"<<endl;
ch=fgetc(f);break;
default:break;
}
}fclose(f);
}
3 楼
zhgliet [专家分:140] 发布于 2005-12-20 21:41:00
我的
#define BEGIN 1
#define END 2
#define IF 3
#define THEN 4
#define ELSE 5
#define ID 6
#define INT 7
#define LT 8
#define LE 9
#define EQ 10
#define NE 11
#define GT 12
#define GE 13
#define TOKEN_SIZE 64
#define TAB_SIZE 5
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
char TOKEN[TOKEN_SIZE];
extern int lookup(char *);
extern void out(int ,char*);
extern void report_error();
char ch;
//信息表
typedef struct{
int ad;
char id[6];
}info_ele;
info_ele info_tab[TAB_SIZE]={{1,"begin"},{2,"end"},{3,"if"},{4,"then"},{5,"else"}};
//扫描器函数
void scaner(FILE *fp)
{
int i,c_id;
ch=fgetc(fp);
if(isalpha(ch)){
TOKEN[0]=ch;
i=1;
ch=fgetc(fp);
while(isalnum(ch))
{TOKEN[i]=ch;
i++;
ch=fgetc(fp);
}
fseek(fp,-1,1);
TOKEN[i]='\0';
c_id=lookup(TOKEN);
if (c_id==0) out(ID,TOKEN);
else out(c_id,TOKEN);
}
else if(isdigit(ch)){
TOKEN[0]=ch;
i=1;
ch=fgetc(fp);
while(isdigit(ch))
{TOKEN[i]=ch;
i++;
ch=fgetc(fp);
}
fseek(fp,-1,1);
TOKEN[i]='\0';
out(INT,TOKEN);
}
else
if(ch==' ');//遇见空格继续
else switch(ch)
{
case '=':out(EQ,"=");
break;
case '>':ch=fgetc(fp);
if(ch=='=')out(GE,">=");
else{ fseek(fp,-1,1);
out(GT,">");
}
break;
case'<':ch=fgetc(fp);
if(ch=='=') out(LE,"<=");
else if(ch=='>')out(NE,"<>");
else{
fseek(fp,-1,1);
out(LT,"<");
}
break;
default: report_error();
break;
}
}
int lookup(char p[])
{
int i=0;
for(i;i<TAB_SIZE;i++)
{
if(!strcmp(info_tab[i].id,p))return (info_tab[i].ad);
}
return 0;
}
void out(int a,char *p)
{
switch(a){
case BEGIN: printf("(BEGIN,%s)",p);break;
case END : printf("(END,%s)",p); break;
case IF: printf("(IF,%s)",p);break;
case THEN: printf("(THEN,%s)",p);break;
case ELSE: printf("(ELSE,%s)",p);break;
case ID: printf("(ID,%s)",p);break;
case INT:printf("(INT,%s)",p);break;
case LT:printf("(LT,%s)",p);break;
case LE:printf("(LE,%s)",p);break;
case EQ:printf("(EQ,%s)",p);break;
case NE:printf("(NE,%s)",p);break;
case GT:printf("(GT,%s)",p);break;
case GE:printf("(GE,%s)",p);break;
default: break;
}
}
void report_error()
{
printf("\n有错误!\n");
exit(0);
}
---------------------------------
#include<stdio.h>
#include<stdlib.h>
extern void scaner(FILE *);
void main()
{
extern char ch;
char fname[20];
FILE * fp;
printf("Enter file's name:");
scanf("%s",&fname);
if((fp=fopen(fname,"r"))==NULL)
{
printf("\nfile open error!\n");
exit(0);
}
do{scaner(fp);}while(ch!=EOF);
}
4 楼
baby624779 [专家分:0] 发布于 2006-11-13 22:30:00
哇,好尽业``!![em11]
我来回复