📄 word.cpp
字号:
#include<iostream>
#include<string>
#include<vector>
#include<fstream>
using namespace std;
class CIV //存储的输出结构
{
public:
int code; //种别码
int index; //变量或常量在变量表或常量表中的位置索引,其他则为-1;
string value; //单词值
public:
CIV(int c,int in,string&v)
{
code=c;
value=v;
index=in;
}
CIV& operator=(const CIV &s)
{
code=s.code;
value=s.value;
index=s.index;
return *this;
}
CIV(const CIV&s)
{
code=s.code;
value=s.value;
index=s.index;
}
};
class word
{
string* keywords;
private:
vector<string> symbols; //符号表
vector<CIV> out; //输出
vector<string> consts; //常量表
string source; //读入的源字符串
int len; //长度
int current; //当前位置
char ch; //当前字符
string token;
public:
word(string&s,int l,string* key_list)
{
source=s;
len=l;
current=0;
ch=' ';
keywords=key_list;
}
bool GetChar()
{
if(current>=len)
{
ch=0;
return false;
}
ch=source[current++];
return true;
}
bool GetBC()
{
while(ch==' ')
{
if(!GetChar())
return false;
}
return true;
}
void Concat()
{
token+=ch;
}
int Reserve()
{
for(int i=0;i<8;i++)
if(token==keywords[i])
return 1;
return 2;
}
bool Retract()
{
if(current==0)
{
ch=0;
return false;
}
else
{
current--;
ch=0;
}
}
int InsertId()
{
symbols.push_back(token);
return symbols.size()-1;
}
bool IsLetter(char c)
{
if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
return true;
return false;
}
bool IsDigit(char c)
{
if(c>='0'&&c<='9')
return true;
return false;
}
bool parse();
void print();
};
bool word::parse()
{
int code,index;
string value;
while(GetChar())
{
if(!GetBC()) continue;
if(IsLetter(ch))
{
while(IsLetter(ch)||IsDigit(ch))
{
Concat();
if(!GetChar()) break;
}
Retract();
code=Reserve();
if(code==2)
{
index=InsertId();
value=token;
token.erase();
}
else
{
index=-1;
value=token;
token.erase();
}
CIV cv(code,index,value);
out.push_back(cv);
}
else if(IsDigit(ch))
{
while(IsDigit(ch))
{
Concat();
if(!GetChar()) break;
}
if(IsLetter(ch))
{
while(IsLetter(ch))
{
Concat();
if(!GetChar()) break;
}
cout<<"单词:"<<token<<" error!"<<endl;
Retract();
token.erase();
continue;
}
Retract();
consts.push_back(token);
index=consts.size()-1;
CIV cv(3,index,token);
out.push_back(cv);
token.erase();
}
else if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
{
char ss[2]={'\0'};
ss[0]=ch;
CIV cv(4,-1,string(ss));
out.push_back(cv);
}else if(ch=='>'||ch=='<'||ch=='='||ch=='!')
{
char ss[3]={'\0'};
ss[0]=ch;
GetChar();
if(ch=='=')
ss[1]='=';
else
Retract();
string s(ss);
CIV cv(4,-1,s);
out.push_back(cv);
}
else if(ch==','||ch==';'||ch=='{'||ch=='}'||ch=='('||ch==')')
{
char ss[2]={'\0'};
ss[0]=ch;
string s(ss);
CIV cv(5,-1,s);
out.push_back(cv);
}
}
return true;
}
void word::print()
{
vector<CIV>::iterator it;
for(it=out.begin();it!=out.end();it++)
cout<<" ("<<(*it).code<<",\""<<(*it).value<<"\")"<<endl;
}
int main()
{
string x[8]={"if","int","for","while","do","return","break","continue"};
word* pw=NULL;
FILE*fp;
string filename;
cout<<"请输入源文件名(绝对路径):";
cin>>filename;
string source;
fp=fopen(filename.c_str(),"r");
if(fp==NULL)
{
cout<<"不能打开文件 "<<filename<<endl;
exit(0);
}
char buff[1024];
while(!feof(fp))
{
fgets(buff,1024,fp);
string ss(buff);
source+=ss;
}
cout<<"源文件 "<<filename<<"内容为:"<<endl;
cout<<source<<endl;
pw=new word(source,source.size(),x);
pw->parse();
cout<<"输出结果为:"<<endl;
pw->print();
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -