#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX 150 /*分析表的最大容量*/
#define MAXBUF 255
#define EOS '\0'
#define format1 "%-20s\t%d\n"
int k=1;/*(临时存放)当前符号表长度*/
char ch=' ';/*存放读入当前的输入字符*/
int line=1;/*程序行数*/
struct entry{
char lexptr[MAXBUF];
int token;
};/*关键字表中的结构*/
struct entry symtable[MAX];/*符号表*/
char * str[] = {"DIM","IF","DO","STOP","END"};
/*初始化符号表*/
Initial()
{ int j;
for(j=0; j<5; j++)
{
strcpy(symtable[j+1].lexptr,str[j]);
symtable[j+1].token=j+1;
k++;
}
}
/*
getbc()检查是否为空白,调用getchar直到进入非空
concat()将ch连接到strtoken
*/
int reserve(char *temp)/*查找保留字表,若是返回编码,否则返回0*/
{ int j;
for(j=1; j<k+1; j++)
{
if(!strcmp(symtable[j].lexptr,temp))
{
return symtable[j].token;
}
}
return(0);
}
/*retract()指示器返回1个字符*/
int insertid(char *temp)/*将strtoken中的标志符插入符号表,返回符号表指针*/
{
strcpy(symtable[++k].lexptr,temp);
symtable[k].token=6;
}
int insertconst(char *temp)/*将strtoken中的常数(整数)插入常数表,返回常数表指针*/
{
strcpy(symtable[++k].lexptr,temp);
symtable[k].token=7;
}
/*
bool isletter(char ch)
{
return ((ch>='a'&&ch<='z')||(ch>'A'&&ch<'Z'));
}
bool IsDigit(char ch)
{
return (ch>='0'&&ch<='9');
}*/
void main()
{
FILE *in,*out;
char strtoken[255];/*定义缓冲区*/
int i=0;
char infile[20],outfile[20];
/*infile.txt是输入程序,结尾加 @ 易于判断*/
printf("please input the in filename(18)");
scanf("%s",infile);
printf("please input the out filename(18)");
scanf("%s",outfile);
if((in=fopen(infile,"r"))==NULL)
{
printf("can not open file\n");
exit(0);
}
if((out=fopen(outfile,"w"))==NULL)
{
printf("cannout open outfile\n");
exit(1);/*非正常终止程序*/
}
Initial();
while((ch=fgetc(in))!=EOF)/*文件尾加@作判断用*/
{
if(ch==' '||ch=='\t')/*去空格*/
{
;
}
else if(ch=='\n')
{
line++;/*记录行号,方便查找出错语句*/
}
else if(isalpha(ch))/*判断标志符*/
{i=0;
while(isalnum(ch))
{
strtoken[i++]=ch;
ch=fgetc(in);
}
strtoken[i++]=EOS;
if (reserve(strtoken))
{
fprintf(out,format1,strtoken,reserve(strtoken));
}
else {
insertid(strtoken);
fprintf(out,format1,strtoken,6);
}
fseek(in,-1,1);
}
else if(isdigit(ch))/*判断数字*/
{
i=0;
while(isdigit(ch))
{
strtoken[i++]=ch;
ch=fgetc(in);
}
strtoken[i++]=EOS;
if (reserve(strtoken))
{
fprintf(out,format1,strtoken,reserve(strtoken));
}
else {
insertconst(strtoken);
fprintf(out,format1,strtoken,7);
}
fseek(in,-1,1);
}
else if(ch=='*')
{
ch=fgetc(in);
if(ch=='*')
fprintf(out,format1,"**",11);
else
{
fprintf(out,format1,"*",10);
fseek(in,-1,1);
}
}
else if(ch=='=')fprintf(out,format1,"=",8);
else if(ch=='+')fprintf(out,format1,"+",9);
else if(ch==',')fprintf(out,format1,",",12);
else if(ch=='(')fprintf(out,format1,"(",13);
else if(ch==')')fprintf(out,format1,")",14);
else
fprintf(out,"line:%d --> %c\n",line,ch);
}
fclose(in);
fclose(out);
exit(0);
}
bianyi_c.rar_visual c_词法分析_词法分析器
版权申诉
60 浏览量
2022-09-21
03:37:37
上传
评论
收藏 2KB RAR 举报
周楷雯
- 粉丝: 80
- 资源: 1万+
最新资源
- 本科毕业设计基于C# wpf人脸识别的考勤系统的设计与实现源码.zip
- 基于Ruoyi+uniapp实现学生考勤系统 学生考勤源码+项目说明.zip
- feae6bc968ca68a099455d8b8a8dea35
- 基于Pytorch训练CIRAR10上分类算法.zip
- Pytorch-pytorch深度学习教程之Tensorboard.zip
- 基于C++和Python开发yolov8-face作为人脸检测器dlib作为人脸识别器的人脸考勤系统源码+项目说明.zip
- Pytorch-pytorch深度学习教程之变分自动编码器.zip
- Pytorch-pytorch深度学习教程之神经风格迁移.zip
- Pytorch-pytorch深度学习教程之深度残差网络.zip
- Pytorch-pytorch深度学习教程之循环神经网络.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈