📄 word.cpp
字号:
#include<iostream>
#include<vector>
#include<stdio.h>
using namespace std;
//单词编码表
struct symbolrecord{
char symbol[10];
int id;
};
//符号表
struct entrytype{
char idname[10];
int address;
char type[10];
};
//常数表
struct consttype{
char idname[10];
int address;
char type[10];
};
//token字
struct tokentype{
int id;
int entry;
};
vector <symbolrecord> symbol;
vector <entrytype> entype;
vector <tokentype> token;
vector <consttype> cntype;
int ident1=0,ident2=0,counti=1,token_num=0;
void isword(char c,FILE *pf,int n);
void isnumber(char c,FILE *pf,int n);
int iscode(char c,FILE *pf,int n);
int word(char *source)
{
FILE *pt,*pf,*pot,*poe,*poc;//单词编码表,分析文件,token,符号文件指针,const表
symbolrecord node;
int flag0=0,i;
int n;
char c,c1;
pt=fopen("table.txt","r");
fscanf(pt,"%d",& n);
while(fscanf(pt,"%s%d",& node.symbol, &node.id)==2)//输入单词编码表
{
symbol.push_back(node);
}
pf=fopen(source,"r");
if(pt == NULL)printf("文件打开错误!");
NN: while(flag0==0&&(c=fgetc(pf))!=EOF)
{
if(c=='/')//去掉注释(/**/)
{
if((c1=fgetc(pf))=='*')
{
MM: while((c1=fgetc(pf))!='*'&&c1!=EOF)
;
if(c1=='*')
{
if(fgetc(pf)=='/')
goto NN;
else
goto MM;
}
else
{
ungetc(c1,pf);
goto NN;
}
}
else
ungetc(c1,pf);
}
if(!isspace(c))//判断不是空格
{
if(isalpha(c))
{
isword(c,pf,n);//若是标示符调用
}
else if(c>='0'&&c<='9')
{
isnumber(c,pf,n);//若是数字调用
}
else
{
flag0=iscode(c,pf,n);//若是关键字,算符,界符调用
}
}
else if(c=='\n')
counti++;//计数行数
}
pot=fopen("token.txt","w");
poe=fopen("id.txt","w");
poc=fopen("const.txt","w");
//printf("符号表为:\n");
for(i=0;i<entype.size();i++)//输出符号表
{
fprintf(poe,"%d\t%s\t%s\n",entype[i].address,entype[i].idname,entype[i].type);
//printf("%d\t%s\t%s\n",entype[i].address,entype[i].idname,entype[i].type);
}
//printf("常数表为:\n");
for(i=0;i<cntype.size();i++)//输出符号表
{
fprintf(poc,"%d\t%s\t%s\n",cntype[i].address,cntype[i].idname);
//printf("%d\t%s\t%s\n",cntype[i].address,cntype[i].idname,cntype[i].type);
}
//printf("token字为:\n");
for(i=0;i<token.size();i++)//输出token字
{
fprintf(pot,"%d\t%d\n",token[i].id,token[i].entry);
//printf("%d\t%d\n",token[i].id,token[i].entry);
}
fclose(pot);
fclose(poe);
fclose(poc);
fclose(pf);
fclose(pt);
return 0;
}
void isword(char c,FILE *pf,int n)
{
char ch[10];
ch[0]=c;
int i=1;
int flag=0,flag1;
tokentype node1;
while(isalnum((c=fgetc(pf))))//若是字符或数字继续读
{
ch[i]=c;
i++;
}
ungetc(c,pf);
ch[i]='\0';
for(i=0;i<(n-1)&&flag==0;i++)
{
if(!strcmp(ch,symbol[i].symbol))//搜索是否为关键字
{
node1.entry=-1;
node1.id=symbol[i].id;
token.push_back(node1);
flag=1;
}
}
if(flag==0)//不是关键字搜索符号表
{
flag1=0;
for(i=0;i<entype.size()&&flag1==0;i++)
{
if(!strcmp(ch,entype[i].idname))
{
flag1=1;
}
}
if(flag1==0)
{
entrytype node2;
node2.address=ident1;
strcpy(node2.idname,ch);
strcpy(node2.type,"\0");
entype.push_back(node2);
node1.entry=ident1;
ident1++;
}
else
node1.entry=entype[i-1].address;
node1.id=symbol[n].id;
token.push_back(node1);
token_num++;
}
}
void isnumber(char c,FILE *pf,int n)
{
char ch[10];
ch[0]=c;
int i=1;
int flag=0,flag1;
tokentype node1;
while((c=fgetc(pf))>='0'&&c<='9')//是数字继续输入
{
ch[i]=c;
i++;
}
ungetc(c,pf);
ch[i]='\0';
flag1=0;
for(i=0;i<cntype.size()&&flag1==0;i++)
{
if(!strcmp(ch,cntype[i].idname))
{
flag1=1;
}
}
if(flag1==0)
{
consttype node2;
node2.address=ident2;
strcpy(node2.idname,ch);
strcpy(node2.type,"int");
cntype.push_back(node2);
node1.entry=ident2;
ident2++;
}
else
node1.entry=entype[i-1].address;
node1.id=symbol[n-1].id;
token.push_back(node1);
token_num++;
}
int iscode(char c,FILE *pf,int n)
{
char ch[10];
ch[0]=c;
int i=1;
int flag=0;
tokentype node1;
if(c=='>'||c=='<'||c=='='||c=='!')//是这些符号判断是否下一个为等号
{
if((c=fgetc(pf))=='=')
{
ch[1]=c;
ch[2]='\0';
}
else
{
ungetc(c,pf);
ch[1]='\0';
}
}
else
ch[1]='\0';
for(i=n;i<symbol.size()&&flag==0;i++)
{
if(!strcmp(ch,symbol[i].symbol))
{
node1.entry=-1;
node1.id=symbol[i].id;
token.push_back(node1);
token_num++;
flag=1;
}
}
if(flag==1)
{
return 0;
}
else
{
printf("ERROR: WRONG CODE!!! in the line %d\n",counti);//遇到非法字符输出行数
return 1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -