📄 lex.cpp
字号:
if(add_sub==0)
power=power*10+(c-48);
else
power=1/(power*10+(c-48));
num=1;
}//5.2
else if(num==1&&(c=='+'||c=='-'))//+或者-当前是运算符
{//5.3
Operator(c,infile,table_operator,table_delimiter);
temp=temp*pow(10.0,double(power));
Getnum(table_num,temp);return 1;
}//5.3
else //ERROR
{
ERROR[error]=colume;
error++;
return 0;
}
}//4.1
break;
case 3:
temp=temp*pow(10.0,double(power));
Getnum(table_num,temp);
if(c==10||c==13)
colume++;//行数加1
return 1;
case 4://遇到运算符
temp=temp*pow(10.0,double(power));
Getnum(table_num,temp);
//call OPERATOR
Operator(c,infile,table_operator,table_delimiter);
return 1;
}//3
if(!infile.get(c))
break;
}//2
return 1;
}//1
//提取运算符
bool Operator(char c,ifstream &infile,table &table_operator,table &table_delimiter)
{
char b;
string stemp;
switch(c)
{
case '+':stemp+=c;
infile.get(b);
if(b=='+'||b=='=')//是否构成双运算符
stemp+=b;
else infile.seekg(-1,ios::cur);//文件指针回退一位
Getword(table_operator,stemp);
return 1;
case '-':
stemp+=c;
infile.get(b);
if(b=='-'||b=='=')
stemp+=b;
else infile.seekg(-1,ios::cur);//文件指针回退一位
Getword(table_operator,stemp);
return 1;
case '*':
case '/':
case '!':
case '=':
case '>':
case '<':stemp+=c;
infile.get(b);
if(b=='=')
stemp+=b;
else infile.seekg(-1,ios::cur);//文件指针回退一位
Getword(table_operator,stemp);
return 1;
case '&':
case '|':stemp+=c;
infile.get(b);
if(b=='&')
stemp+=b;
else infile.seekg(-1,ios::cur);
Getword(table_operator,stemp);
return 1;
case '%':stemp+=c;
Getword(table_operator,stemp);
return 1;
default:ERROR[error]=colume;
error++;
return 0;
}
}
bool Delimiter(char c,table &table_delimiter )//提取界符
{
string stemp;
stemp+=c;
Getword(table_delimiter,stemp);
return 1;
}
void Getword(table &table_sub,string stemp)//提取单词
{
//如果table已经满了,重新分配
if(table_sub.length>=table_sub.size)
{
string *p=new string [table_sub.size+20];
for(int former=0;former<table_sub.size;former++)
p[former]=table_sub.pst[former];
table_sub.pst=p;
table_sub.size+=20;
}
//提取单词
//cout<<stemp<<endl;
table_sub.pst[table_sub.length]=stemp;
table_sub.length++;//table长度自曾1
}
void Getnum(tablenum &table_sub,double temp)//提取数字
{
//
if(table_sub.length>=table_sub.size)//如果空间已满,重新分配
{
double *p=new double [table_sub.size+20];
for(int former=0;former<table_sub.size;former++)
p[former]=table_sub.pst[former];
table_sub.pst=p;
table_sub.size+=20;
}
//提取单词
table_sub.pst[table_sub.length]=temp;
table_sub.length++;
}
int classfify(char c)//字符处理的读入字符分离
{
int ret=0;
switch(c)
{
case ':':
case '{':
case '}':
case ',':
case '[':
case ']':
case ';':
case '(':
case ')':
case '.':ret=1;break;//界符
case '+':
case '-':
case '*':
case '/':
case '%':
case '!':
case '&':
case '|':
case '=':
case '>':
case '<':ret=2;break;//运算符
case ' ':
case 13:
case 10:ret=3;break;//回车或者空格
default://ERROR
break;
}
return ret;
}
int classify_num(char c)//数字处理的读入字符分类
{
int b=0;
switch(c)
{
case ')':
case ':':
case ';':
case ',':
case ']':b=1;break;
case '+':
case '-':
case 'E':
case 'e':
case '.':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':b=2;break;
case ' ':
case 13:
case 10:b=3;break;
case '*':
case '/':
case '%':
case '!':
case '&':
case '|':
case '=':
case '>':
case '<':b=4;break;
default ://error
break;
}
return b;
}
//主菜单
void Manager(table &table_key,table &table_ID,table &table_operator,
table &table_delimiter,table &table_const,tablenum &table_num)
{
char select1='Y';int select=0;
while(1)
{
system("cls");
Outtenspace();cout<<"****************************************"<<endl;
Outtenspace();cout<<"* Table system *"<<endl;
Outtenspace();cout<<"* <1>Identify Table *"<<endl;
Outtenspace();cout<<"* <2>Const Table *"<<endl;
Outtenspace();cout<<"* <3>Keywords Table *"<<endl;
Outtenspace();cout<<"* <4>Operator Table *"<<endl;
Outtenspace();cout<<"* <5>Delimiter Table *"<<endl;
Outtenspace();cout<<"* <6>Error Table *"<<endl;
Outtenspace();cout<<"* <0>Exit *"<<endl;
Outtenspace();cout<<"****************************************"<<endl;
Outtenspace();cout<<"* 您的选项: ";
cin>>select;
switch(select)
{
case 0:exit(1);
case 3:ShowKeywords(table_key,select1);break;//显示关键字
case 1:ShowID(table_ID,select1);break;//显示标识符
case 2:ShowConst(table_const,table_num,select1);break;//显示常量
case 4:ShowOperator(table_operator,select1);break;//显示运算符
case 5:ShowDelimiter(table_delimiter,select1);break;//显示界符
case 6:ShowError(select1);break;//显示错误信息
default:exit(1);//退出
}
if(select1!='Y')//如果select1='Y',继续,否则退出
break;
}
}
void ShowKeywords(table &table_key,char &select1)//显示关键字
{
int i=0;
cout<<endl;
Outtenspace();
cout<<"classify: 3"<<endl;
for(i;i<table_key.length;i++)
{
if(i%3==0)
{
cout<<endl;
Outtenspace();
}
cout.setf(ios::left);
cout.width(20);
cout<<table_key.pst[i];
}
cout<<endl;cout<<endl;
Outtenspace();
cout<<"if continue please input 'Y',else input 'N'"<<endl;
Outtenspace();
cin>>select1;
}
void ShowID(table &table_ID,char &select1)//显示标识符
{
int i=0;
Outtenspace();
cout<<"classify: 1"<<endl;
for(i;i<table_ID.length;i++)
{
if(i%3==0)
{
cout<<endl;
Outtenspace();
}
cout.setf(ios::left);
cout.width(20);
cout<<table_ID.pst[i];
}
cout<<endl;cout<<endl;
Outtenspace();
cout<<"if continue please input 'Y',else input 'N'"<<endl;
Outtenspace();
cin>>select1;
}
void ShowConst(table &table_const,tablenum &table_num,char &select1)//显示常量
{
int i=0;
Outtenspace();
cout<<"classify: 2"<<endl;
for(i;i<table_const.length;i++)//字符常量
{
if(i%3==0)
{
cout<<endl;
Outtenspace();
}
cout.setf(ios::left);
cout.width(20);
cout<<table_const.pst[i];
}
for(i;i<table_num.length;i++)//数字常量
{
if(i%3==0)
{
cout<<endl;
Outtenspace();
}
cout.setf(ios::left);
cout.width(20);
cout<<table_num.pst[i];
}
cout<<endl;cout<<endl;
Outtenspace();
cout<<"if continue please input 'Y',else input 'N'"<<endl;
Outtenspace();
cin>>select1;
}
void ShowOperator(table &table_operator,char &select1) //显示运算符
{
int i=0;
Outtenspace();
cout<<"classify: 4"<<endl;
for(i;i<table_operator.length;i++)
{
if(i%3==0)
{
cout<<endl;
Outtenspace();
}
cout.setf(ios::left);
cout.width(20);
cout<<table_operator.pst[i];
}
cout<<endl;cout<<endl;
Outtenspace();
cout<<"if continue please input 'Y',else input 'N'"<<endl;
Outtenspace();
cin>>select1;
}
void ShowDelimiter(table &table_delimiter,char &select1) //显示界符
{
int i=0;
Outtenspace();
cout<<"classify: 5"<<endl;
for(i;i<table_delimiter.length;i++)
{
if(i%3==0)
{
cout<<endl;
Outtenspace();
}
cout.setf(ios::left);
cout.width(20);
cout<<table_delimiter.pst[i];
}
cout<<endl;cout<<endl;
Outtenspace();
cout<<"if continue please input 'Y',else input 'N'"<<endl;
Outtenspace();
cin>>select1;
}
void ShowError(char &select1) //显示错误信息
{
int i=0;
for(i;i<error;i++)
{
if(i%5==0)
{
cout<<endl;
Outtenspace();//输出20个空格
}
cout.setf(ios::left);//设置做对齐
cout.width(10);//宽度10
cout<<ERROR[i]+1;//错误所在行
}
cout<<endl;cout<<endl;
Outtenspace();
cout<<"if continue please input 'Y',else input 'N'"<<endl;
Outtenspace();
cin>>select1;
}
void Outtenspace()//输出20个空格
{
for(int i=0;i<20;i++)
cout<<" ";
}
void Outputfile(table & table_sub,char *p,int a)//写标识符表或者关键字表或者字符常量表文件
{
ofstream outfile(p,ios::out);
if(!outfile){cout<<"error";exit(1);}
int i=0;
outfile<<"classify "<<" content"<<endl;
for(i;i<table_sub.length;i++)
{
outfile<<a<<" ";
outfile<<table_sub.pst[i]<<endl;
}
outfile.close();
}
void Outputfile(tablenum &table_num,char *p,int a)//写数字数文件表
{
ofstream outfile(p,ios::out);
if(!outfile){cout<<"error";exit(1);}
int i=0;
outfile<<"classify "<<" content"<<endl;
for(i;i<table_num.length;i++)
{
outfile<<a<<" ";
outfile<<table_num.pst[i]<<endl;
}
outfile.close();
}
void Outputfile(char *P)//写错误信息表文件
{
ofstream outfile(P,ios::out);
if(!outfile){cout<<"error";exit(1);}
int i=0;
for(i;i<error;i++)
{
outfile<<"ERROR: COLUME: "<<ERROR[i]+1<<endl;
}
outfile.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -