📄 cifafenxi.cpp
字号:
#include <stdio.h>
#include <io.h>
#include <ctype.h>
#include <malloc.h>//源代码有错
#include <stdlib.h>
#include <string.h>
//===========================================================
#define NULL 0
FILE *fp; FILE *infile;
char cbuffer;
char *key[21]={"and","begin","const","div","do","else","end",
"function","if","integer","not","or","procedure",
"program","read","real","then","type","var","while",
"write"};//1
char *border[8]={",",";",":",".","(",")","[","]"};//2
char *arithmetic[4]={"+","-","*","/"};//3
char *relation[10]={"=","<",">","<>","<=",">=",":=","{","}","#"};//4
char *consts[20];//5
char *label[20];//6
int constnum=0,labelnum=0;
//===========================================================
int search(char searchchar[],int wordtype)//查找保留字
{
int i=0;
switch (wordtype) {
case 1:{for (i=0;i<=20;i++)
{
if (strcmp(key[i],searchchar)==0)
return(i);
};break;
}//原代码有错
case 2:{for (i=0;i<=7;i++)//查找界符
{
if (strcmp(border[i],searchchar)==0)
return(i+23);
};break;
}
case 3:{for (i=0;i<=3;i++)//查找运算符
{
if (strcmp(arithmetic[i],searchchar)==0)
return(i+34);
};break;
}
case 4:{for (i=0;i<=9;i++)//查找关系运算符
{
if (strcmp(relation[i],searchchar)==0)
return(i+38);
};break;
}
case 5:{for (i=0;i<=constnum;i++)
{
if (strcmp(consts[i],searchchar)==0)
return(22);
}
consts[i-1]=(char *)malloc(sizeof(searchchar));
strcpy(consts[i-1],searchchar);
constnum++;
}//数字
case 6:{for (i=0;i<=labelnum;i++)
{
if (strcmp(label[i],searchchar)==0)
return(21);
}
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 , %d\n",alphatp,atype);
else
{
atype=search(alphatp,6);
printf("%s , %d\n",alphatp,atype);
}
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 , %d\n",digittp,22);
fprintf(infile, "%s , %d\n",digittp,22);//不规范调试
return(buffer);
}
//==========================================
char otherprocess(char buffer)//重写完成
{
char othertp[20];
if (isspace(buffer)!=0)
{
buffer=fgetc(fp);
goto out;
}//此段代码用于消除空格,原代码没有。
int otype,otypetp;
othertp[0]=buffer;
othertp[1]='\0';
if (buffer=='+'||buffer=='-'||buffer=='*'||buffer=='/')
{
otype=search(othertp,3);
printf("%s , %d\n",othertp,otype);
fprintf(infile,"%s , %d\n",othertp,otype);
buffer=fgetc(fp);
goto out;
};
if (buffer=='='||buffer=='<'||buffer=='>'||buffer=='{'||buffer=='}')
{
otype=search(othertp,4);
printf("%s , %d\n",othertp,otype);
fprintf(infile,"%s , %d\n",othertp,otype);
buffer=fgetc(fp);
goto out;
};
if(buffer==':')
{
buffer=fgetc(fp);
if (buffer=='='){
printf(":= , 44\n");
fprintf(infile,":= , 44\n");
buffer=fgetc(fp);}
else
{
printf(": , 25\n");
fprintf(infile,": , 25\n");
}
goto out;
};
if (buffer==','||buffer==';'||buffer=='.'||buffer=='('||buffer==')'||buffer=='['||buffer==']')
{
otype=search(othertp,2);
printf("%s , %d\n",othertp,otype);
fprintf(infile,"%s , %d\n",othertp,otype);
buffer=fgetc(fp);
goto out;
};
if ((buffer!='\n')&&(buffer!=' '))
{
printf("%c error,not a word\n",buffer);
fprintf(infile,"%c error,not a word\n",buffer);
}
buffer=fgetc(fp);
out: return(buffer);
}
//=============================================
void main()
{
int i;
printf(" The Result :\n");
for (i=0;i<=20;i++)
{
label[i]="";
consts[i]="";//原代码会引起指针错误
}
infile=fopen("result.txt","w");
if ((fp=fopen("pas.txt","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");
fprintf(infile, " OVER !\n");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -