📄 lex.cpp
字号:
#include"lex.h"
#include"error.h"
#include"Table.h"
#include<stdlib.h>
#include<iostream.h>
///////公用量区//////////////////////////////////////////////////////////
FILE*fd,*fw;
char strout1[2000];
int c1=0;
char *guanjian[]={"and","array","begin","bool","call","case","char","constant","dim","do","else",
"end","false","for","if","input","integer","not","of","or","output","procedure","program","read",
"real","repeat","set","stop","then","to","true","until","var","while","write"};
///////////lex.cpp内部公用量//////////////////////////////////////////////
static char infile[30];
static char error[1000];
static int c3=0;
//////////////////////////////////////////////////////////////////////////
static FILE*init()
{
cout<<"\t\tPlease input the address of infile\n";
cout<<"\t\t";
cin>>infile;
FILE*filename1;
if((filename1=fopen(infile,"r"))==NULL)
{
return(NULL);
}
else
{
return(filename1);
}
}
static void startl()
{
char string[MAX/2],currentch;
int xb,Isg;
int signid;
cout<<"\n\n\n\n\n\n\t\tSIMPLE语言词法分析器\n\n";
cout<<"\t\t\t\t--计算机02(2)张斌\n";
cout<<"\t\t\t\t200245003122\n";
fd=init();
fw=fopen("token.txt","w");
system("cls");
do
{
currentch=get_ch();
if(currentch==' ')
{
continue;
}
else if(currentch=='\n')
{
fprintf(fw," \n");
}
else if(IsLetter(currentch))
{
string[0]=currentch;
xb=1;
while(IsLetter(currentch)||IsDigit(currentch))
{
currentch=get_ch();
string[xb]=currentch;
xb++;
}
string[xb-1]='\0';
retract();
if(!IsSign(currentch)&¤tch!=' '
&¤tch!='\n')//出现非法字符,识别失效
{
continue;
}
Isg=IsGJZ(string);
if(Isg==0)
{
signid=AddTab(36,string);
fprintf(fw,"<36\t");
fprintf(fw,"%d>\n",signid);
}
else
{
fprintf(fw,"<%d\t",Isg);
fprintf(fw,"->\n");
}
}
else if(IsDigit(currentch))
{
string[0]=currentch;
xb=1;
while(IsDigit(currentch))
{
currentch=get_ch();
string[xb]=currentch;
xb++;
}
string[xb-1]='\0';
retract();
signid=AddTab(37,string);
fprintf(fw,"<37\t");
fprintf(fw,"%d>\n",signid);
}
else if(currentch=='\'')
{
currentch=get_ch();
xb=0;
L1: while(IsSign(currentch)&&(currentch!='\'')
&&(currentch!='\n')&&(currentch!=' '))
{
string[xb++]=currentch;
currentch=get_ch();
}
if(!IsSign(currentch)&¤tch!='\n'
&¤tch!=' ')
{
Error(col,1);
currentch=get_ch();
goto L1;
}
else if(currentch=='\'')
{
string[xb]='\0';
signid=AddTab(38,string);
fprintf(fw,"<38\t");
fprintf(fw,"%d>\n",signid);
}
else
{
Error(col,2);
}
}
else if(currentch=='+')
{
string[0]=currentch;
string[1]='\0';
fprintf(fw,"<43\t");
fprintf(fw,"->\n");
}
else if(currentch=='-')
{
string[0]=currentch;
string[1]='\0';
fprintf(fw,"<45\t");
fprintf(fw,"->\n");
}
else if(currentch=='(')
{
string[0]=currentch;
string[1]='\0';
fprintf(fw,"<39\t");
fprintf(fw,"->\n");
}
else if(currentch==')')
{
string[0]=currentch;
string[1]='\0';
fprintf(fw,"<40\t");
fprintf(fw,"->\n");
}
else if(currentch==',')
{
string[0]=currentch;
string[1]='\0';
fprintf(fw,"<44\t");
fprintf(fw,"->\n");
}
else if(currentch==';')
{
string[0]=currentch;
string[1]='\0';
fprintf(fw,"<52\t");
fprintf(fw,"->\n");
}
else if(currentch=='=')
{
string[0]=currentch;
string[1]='\0';
fprintf(fw,"<56\t");
fprintf(fw,"->\n");
}
else if(currentch=='[')
{
string[0]=currentch;
string[1]='\0';
fprintf(fw,"<59\t");
fprintf(fw,"->\n");
}
else if(currentch==']')
{
string[0]=currentch;
string[1]='\0';
fprintf(fw,"<60\t");
fprintf(fw,"->\n");
}
else if(currentch=='*')
{
string[0]=currentch;
currentch=get_ch();
if(currentch=='/')
{
string[1]=currentch;
string[2]='\0';
}
else
{
string[1]='\0';
retract();
fprintf(fw,"<41\t");
fprintf(fw,"->\n");
}
}
else if(currentch=='.')
{
string[0]=currentch;
currentch=get_ch();
if(currentch=='.')
{
string[1]=currentch;
string[2]='\0';
fprintf(fw,"<47\t");
fprintf(fw,"->\n");
}
else
{
string[1]='\0';
retract();
fprintf(fw,"<46\t");
fprintf(fw,"->\n");
}
}
else if(currentch==':')
{
string[0]=currentch;
currentch=get_ch();
if(currentch=='=')
{
string[1]=currentch;
string[2]='\0';
fprintf(fw,"<51\t");
fprintf(fw,"->\n");
}
else
{
string[1]='\0';
retract();
fprintf(fw,"<50\t");
fprintf(fw,"->\n");
}
}
else if(currentch=='<')
{
string[0]=currentch;
currentch=get_ch();
if(currentch=='=')
{
string[1]=currentch;
string[2]='\0';
fprintf(fw,"<54\t");
fprintf(fw,"->\n");
}
else
{
if(currentch=='>')
{
string[1]=currentch;
string[2]='\0';
fprintf(fw,"<55\t");
fprintf(fw,"->\n");
}
else
{
string[1]='\0';
retract();
fprintf(fw,"<53\t");
fprintf(fw,"->\n");
}
}
}
else if(currentch=='>')
{
string[0]=currentch;
currentch=get_ch();
if(currentch=='=')
{
string[1]=currentch;
string[2]='\0';
fprintf(fw,"<58\t");
fprintf(fw,"->\n");
}
else
{
string[1]='\0';
retract();
fprintf(fw,"<57\t");
fprintf(fw,"->\n");
}
}
else if(currentch=='/')
{
string[0]=currentch;
currentch=get_ch();
if(currentch=='*')
{
string[1]=currentch;
string[2]='\0';
currentch=get_ch();
L2: while(currentch!='*'&¤tch!='\n')
{
currentch=get_ch();
}
if(currentch=='*')
{
currentch=get_ch();
if(currentch=='/')
{
retract();
retract();
continue;
}
else
{
goto L2;
}
}
if(currentch=='\n')
{
Error(col,3);
}
}
else
{
string[1]='\0';
retract();
fprintf(fw,"<48\t");
fprintf(fw,"->\n");
}
}
else
{
if(currentch!=EOF)
{
Error(col,1);
}
}
}while(currentch!=EOF);
fclose(fd);
fclose(fw);
fclose(err);
fclose(sign);
fclose(nmsign);
strout2[c2]='\0'; //源文件已经全部读入strout2中
fw=fopen("token.txt","r");//把分析的结果读入strout1中,供输出用
while(!feof(fw))
{
strout1[c1++]=fgetc(fw);
}
strout1[c1]='\0';
fclose(fw);
err=fopen("error.txt","r");//把错误信息读入error中,供输出用
while(!feof(err))
{
error[c3++]=fgetc(err);
}
error[c3]='\0';
fclose(err);
}
static void PrintWord()
{
system("cls");
cout<<"\t\t\tTHE CODE\n";
cout<<strout2<<endl;
cout<<"\t\t\tRESULT\n";
cout<<error<<endl;
cout<<strout1<<endl;
}
void main()
{
startl();
PrintWord();
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -