📄 asd.cpp
字号:
//---------------------------简单词法分析器-----------------------------
#include<iostream>
#include<stdio.h>
using namespace std;
#define MAX 50
typedef struct tnode{
char ch[20]; //用字符的数组实现字符串的输入
int length; //当前字符串的长度
int id; //编码值
struct tnode *next; //用链表存储编码信息
}tnode;
char text[MAX]; //作为缓冲,存储处理后文本文件
//文本预处理---------------------------------------------------------------------
void pretreatment()
{FILE *fp,*fop;
char filename[15],file[15],ch;
int i=0;
cout<<endl;
cout<<"--------文本预处理--------"<<endl;
cout<<"请输入文本文件的文件名:";
cin>>filename;
if((fp=fopen(filename,"r"))==NULL){
cout<<"不能打开该文件"<<endl;
exit(0);}
ch=fgetc(fp);
cout<<"请输入存储处理后文本文件的文件名:";
cin>>file;
if((fop=fopen(file,"w+"))==NULL){
cout<<"不能打开该文件"<<endl;
exit(0);}
while(ch!=EOF)
{if((ch!=' ')&&(ch!='\n')) //当当前字符不为空格和换行符时读入(处理空格和回车)
{fputc(ch,fop);
text[i]=ch; //将文本内容写入数组(空格和回车除外)
i++;
}
ch=fgetc(fp);
}
fclose(fp);
fclose(fop);
cout<<"处理后文本文件输出=》"<<endl;
cout<<"请输入存储编码文件的文件名:";
cin>>filename;
if((fp=fopen(filename,"r"))==NULL){
cout<<"不能打开该文件"<<endl;
exit(0);}
cout<<"文本信息输出=》"<<endl;
ch=fgetc(fp);
while(ch!=EOF)
{putchar(ch);
ch=fgetc(fp);
}
fclose(fp);
cout<<endl;
cout<<"--------处理完毕--------"<<endl;
cout<<endl;
}
//建表,字符串扫描与匹配------------------------------------------------------
//新建编码表-------------------------------------------
void scanANDmatch()
{char *r,*s,*t,*c,*w;
struct tnode *p,*q,*l;
int i,n,leng=0;
cout<<endl;
cout<<"--------建表&扫描&匹配--------"<<endl;
cout<<"-----编码信息构造-----"<<endl;
cout<<"请输入编码表节点个数:";
cin>>n;
p=(struct tnode *)malloc(sizeof(struct tnode));
p->next=NULL;
q=p;
l=p;
cout<<"请输入各节点单词及其编码值:"<<endl;
for(i=1;i<=n;i++)
{cout<<"请输入第"<<i<<"个单词内容:";
cin>>p->ch;
c=p->ch;
while(*c!=NULL) //计算当前字符串的长度(一个曾经的思维过程,此处无用但保留)
{leng++;
c++;
}
p->length=leng; //将当前字符串长存入结构体
leng=0;
cout<<"请输入其编码值:";
cin>>p->id;
//cout<<p->ch<<endl;
p=(struct tnode *)malloc(sizeof(struct tnode));
p->next=q->next;q->next=p;
q=p;
} //生成不带头节点的链表
cout<<"-----构造编码表成功-----"<<endl<<endl;
//字符串扫描与匹配-----------------------------------------
cout<<"Token串输出为:"<<endl;
p=l; //恢复链表起始地址指针
s=text; //定义指针b指向数组起始地址
t=p->ch; //定义指针c指向当前单词的起始地址
r=s; //存储数组首地址指针,便于恢复
to: while(*s!=NULL) //注意:这里不要写为while(*s!=' '),否则将进入死循环
{if((*s>='a' && *s<='z')&&(*(s+1)<='a' || *(s+1)>='z')) //判定是否为变量
{p=l;
while(p->id!=1) //设定变量编码值均为1
{p=p->next;}
cout<<"("<<*s<<","<<p->id<<")"<<endl;
p=l; //恢复指针p
s=s+1; //数组指针前移一位
}
if(*s>='0' && *s<='9') //判定是否为常量
{p=l;
while(p->id!=2) //设定常量编码值均为2
{p=p->next;}
cout<<"("<<*s<<","<<p->id<<")"<<endl;
p=l;
s=s+1;
}
w=s; //存储当前数组指针地址
while(*s!=NULL){ //关键字及界符输出
while(*s==*t){
if((*s==':')&&(*(s+1)=='=')) //为判定是否为":="做超前搜索
{//p=l;
//t=p->ch;
while(*(t+1)!='=')
{p=p->next;
t=p->ch;
}
if((*t==':')&&(*(t+1)=='=')){
cout<<"("<<p->ch<<","<<p->id<<")"<<endl;
p=l;
t=p->ch;
s=s+2; //数组指针前移两位
goto to;
}
}
s++;
t++;
if(*t==NULL){ //匹配,输出关键字或界符与其的编码值
cout<<"("<<p->ch<<","<<p->id<<")"<<endl;
p=l;
t=p->ch;
goto to; //while语句过多,只能采用goto了
}
}
if(*s!=*t) {s=w; p=p->next; t=p->ch;} //未匹配上,回到上次s的位置,指针p前移,继续扫描
}
}
cout<<endl;
cout<<"--------操作完毕--------"<<endl<<endl;
}
void main()
{char w;
int k=1;
cout<<endl;
cout<<" **********************"<<endl;
cout<<" *简单词法分析器*"<<endl;
cout<<" **********************"<<endl;
cout<<" ------计0306,曾庚卓,20032969"<<endl<<endl;
cout<<"---------------目录---------------"<<endl<<endl;
cout<<" 1-----------文本预处理"<<endl;
cout<<" 2-----------建表&扫描&匹配"<<endl;
cout<<" 3-----------退出"<<endl<<endl;
do
{
cout<<"请按键选择:";
cin>>w;
switch(w)
{
case '1':pretreatment();break;
case '2':scanANDmatch();break;
}}
while(w!='3');
cout<<"---------------结束任务!---------------"<<endl<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -