📄 lex-wxw.cpp
字号:
#include<iostream>
#include<map>
#include<vector>
#include<fstream>
#include<string>
#include<utility>
#include<queue>
#include<conio.h>
using namespace std;
#define MAX 20 //分析表的最大容量
#define MAXBUF 255
char ch =' '; // 存放读入当前的输入字符
int lineno; //行号
struct reserve //关键字
{
char lexptr[MAXBUF];
int token;
};
struct reserve symtable[MAX];
char * str[]={"include","main","using","namespace","endl","cout","cin","char","int","float",
"double","while","for","if","then","else","do","return" };
void init() //对符号表进行初始化
{
for( int j=0; j<18; j++)
{
strcpy(symtable[j].lexptr,str[j]);
symtable[j].token=j+3;
}
}
bool search(char *temp)
{
string a=temp;
string b="";
for(int i=0; i<18;i++)
{
b=str[i];
if(a==b) return true;
}
return false;
}
void analyse(FILE *fpin,FILE *fpout) //分析程序
{
char arr[MAXBUF];
int i=0;
int j=0;
while((ch=fgetc(fpin))!=EOF) //读入字符判断,空格、字母、数字、界符
{
if(ch==' '||ch=='\t')
{
}
else if(ch=='\n') //如果是换行符,则行号加1
{
lineno++;
}
else if(isdigit(ch)) //如果是数字
{
while(isdigit(ch)) //判断和读取数字
{
arr[j]=ch;
j++;
ch=fgetc(fpin);
}
arr[j]='\0';
j=0;
fseek(fpin,-1L,SEEK_CUR); //?
fprintf(fpout,"%s\t,常量\n",arr) ;
}
else if (isalpha(ch)) //如果是字母
{
while(isalpha(ch)||isdigit(ch))
{
arr[j]=ch;
j++;
ch=fgetc(fpin);
}
fseek(fpin,-1L,SEEK_CUR);
arr[j]='\0';
j=0;
if (search(arr)) //如果是关键字
{
fprintf(fpout,"%s\t关键字\n",arr,search(arr));
}
else
fprintf(fpout,"%s\t文字串\n",arr,1); //文字串
}
else if(ch=='=')
{
ch=fgetc(fpin);
if(ch=='=')
{
fprintf(fpout,"%s\t赋值符\n","="); //如果是 =
}
else
{
fprintf(fpout,"%s\t普通符号\n",":"); //如果是 :
fseek(fpin,-1L,SEEK_CUR);
}
}
else if (ch=='>')
{
ch=fgetc(fpin);
if(ch=='=') //如果是 >=
{
fprintf(fpout,"%s\t普通符号\n",">=");
}
else
{
fprintf(fpout,"%s\t普通符号\n",">"); //如果是 >
fseek(fpin,-1L,SEEK_CUR);
}
}
else if(ch=='<')
{
ch=fgetc(fpin);
if(ch=='=')
{
fprintf(fpout,"%s\t小于等于号\n","<="); //如果是 <=
}
else
{
fprintf(fpout,"%s\t小于号号\n","<"); //如果是 <
fseek(fpin,-1L,SEEK_CUR);
}
}
else if(ch=='/')
{
ch=fgetc(fpin);
if(ch=='*')
{
ch=fgetc(fpin);
s:
while(ch!='*')
{
ch=fgetc(fpin);
}
while(ch=='*')
{
ch=fgetc(fpin);
while(ch!='/')
{
goto s; //如果是注释 /* */, 忽略,直到代码段
}
}
}
else if(ch=='/')
{
ch=fgetc(fpin);
while(ch!='\n')
{
ch=fgetc(fpin); //如果是注释 //
}
}
else
{
fprintf(fpout,"%s\t除法运算符\n","/");
fseek(fpin,-1L,SEEK_CUR);
}
}
else if(ch=='+')
{
fprintf(fpout,"%s\t加法运算符\n","+");
}
else if(ch=='-')
{
fprintf(fpout,"%s\t减法运算符\n","-",22);
}
else if(ch=='*')
{
fprintf(fpout,"%s\t乘法运算符\n","*");
}
else if(ch=='(')
{
fprintf(fpout,"%s\t左括号\n","(");
}
else if(ch==')')
{
fprintf(fpout,"%s\t右括号\n",")");
}
else if(ch=='[')
{
fprintf(fpout,"%s\t左中括号\n","[");
}
else if(ch==']')
{
fprintf(fpout,"%s\t右中括号\n","]");
}
else if(ch=='{')
{
fprintf(fpout,"%s\t左花括号\n","(");
}
else if(ch=='}')
{
fprintf(fpout,"%s\t右大括号\n","(");
}
else if(ch=='.')
{
fprintf(fpout,"%s\t成员引用符\n",".");
}
else if(ch==';')
{
fprintf(fpout,"%s\t标点符号\n",";");
}
else if(ch==',')
{
fprintf(fpout,"%s\t标点符号\n",",");
}
else if(ch=='#')
{
fprintf(fpout,"%s\t标识符\n","#");
}
else fprintf(fpout,"无法识别的字符 %c\n",ch) ;
}
}
int main()
{
cout<<"***************************词法分析器——>计科0502 王小伟 200542036*************************\n\n\n";
//cout<<"直接输入源代码请按1,从文件中读入源代码请按2 \n" ;
char filenamein[10];
char filenameout[10];
cout<<"请输入源文件名:\n";
cin>>filenamein;
cout<<"请输入保存词法分析结果的文件名:\n";
cin>>filenameout;
FILE* fpin=fopen(filenamein,"r");
FILE* fpout=fopen(filenameout,"w");
init();
analyse(fpin,fpout);
fclose(fpin);
fclose(fpout);
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -