📄 tz.cpp
字号:
#include<iostream.h>
#include<fstream.h>
#include<string.h>
char * KeyWord[39]={"auto","break","case","char","const",
"default","do","double","else","enum","extern","try",
"float","for","goto","if","int","long","return",
"short","signed","sizeof","static","switch",
"unsigned","void","while","catch","class",
"const_cast","delete","friend","new","operator",
"private","protected","public","template","this"};
int i=0,j=0,k=0,t=0,a=0;//搜索指示器
char ch;//存放最新读入的原程序字符
char strToken[15];//存放构成单词符号的字符串
char * chr_form[100];//字符表
char * int_form[100];//常数表
char form[1000],*form1[100];
int q=0;
int temp;
fstream f1;
void GetChar()//将下一个字符读入ch中,搜索指示器前移一字符位
{
ch=form[k];
k++;
}
void GetBC()//检查ch中的字符是否为空白,若是则调用Getchar直至ch中进入一个非空白字符
{
while(ch==' ')
{
GetChar();
}
}
void Concat()//将ch中的字符连接到strToken之后,
{
strToken[i]=ch;
i++;
}
bool IsLetter()//判断ch中的字符是否为字符
{
if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))
return(1);
else
return(0);
}
bool IsDigit()//判断ch中的字符是否为数字
{
if(((ch)<='9')&&((ch)>='0'))
return (1);
else
return (0);
}
int Reserve()//对strToken中的字符串查找保留字表,若它是一个保留字
//则返回它的编码,否则返回-1值
{
for(int q=0;q<39;q++)
{
if (strcmp(KeyWord[q],strToken)==0)
return q;
if(q==38)
return -1;
}
}
void Retract()//将搜索指示器回调一个字符位置,将ch置为空白字符
{
k--;
ch=NULL;
}
char*InsertId()//将strToken中的标识符插入符号表,返回符号表的指针
{
chr_form[j]=strToken;
j++;
return chr_form[0];
}
char *InsertConst()//将strToken中的常数插入常数表,返回常数表指针
{
int_form[t]=strToken;
t++;
return int_form[0];
}
int code;
//////////////////////////////////////////////////////////////////////
void analyze()
{
GetChar();
GetBC();
if(IsLetter())
{
while(IsLetter()||IsDigit())
{
Concat();
GetChar();
}
Retract();
code=Reserve();
switch(code)
{
case -1:cout<<"<字符串ID,指向"<<strToken<<"的符号指针>"<<endl;
f1.write("<ID,",4);
f1.write(strToken,strlen(strToken));
f1.write(">\n",3);
break;
default:cout<<"<关键字,"<<strToken<<">"<<endl;
f1.write("<关键字,",8);
f1.write(strToken,strlen(strToken));
f1.write(">\n",3);
}
}
else
{
if(IsDigit())
{
while(IsDigit()||ch=='.')
{
Concat();
GetChar();
}
Retract();
cout<<"<常数,"<<strToken<<">"<<endl;
f1.write("<常数,",6);
f1.write(strToken,strlen(strToken));
f1.write(">\n",3);
}
else
{
switch (ch)
{
case '=':
case '/':
case '%':
case '*':cout<<"<运算符,"<<ch<<">"<<endl;
f1.write("<运算符,",8);
f1.put(ch);
f1.write(">\n",3);break;
case '-':
GetChar();
if(ch=='-')
{
cout<<"<运算符,"<<"-->"<<endl;
f1.write("<运算符,-->\n",13);break;
}
else
{
Retract();
cout<<"<运算符,"<<"->"<<endl;
f1.write("<运算符,->\n",12);
break;
}
case '+':
GetChar();
if(ch=='+')
{
cout<<"<运算符,"<<"++>"<<endl;
f1.write("<运算符,++>\n",13);break;
}
else
{
Retract();
cout<<"<运算符,"<<"+>"<<endl;
f1.write("<运算符,+>\n",12);break;
}
case '|':
GetChar();
if(ch=='|')
{
cout<<"<运算符,"<<"||>"<<endl;
f1.write("<运算符,||>\n",13);break;
}
else
{
Retract();
cout<<"<非法符号,"<<"|>"<<endl;
f1.write("<运算符,|>\n",12);break;
}
case '&':
GetChar();
if(ch=='&')
{
cout<<"<运算符,"<<"&&>"<<endl;
f1.write("<运算符,&&>\n",13);break;
}
case '(':
case ';':
case ')':
case ':':
case'[':
case']':
case '"':
case'\'':
case '{':
case ',':
case '}':cout<<"<界符,"<<ch<<">"<<endl;
f1.write("<界符,",6);
f1.put(ch);
f1.write(">\n",3);break;
case '<':
GetChar();
if(ch=='<')
{
cout<<"<运算符,"<<"<<>"<<endl;
f1.write("<运算符,<<>\n",13);break;
}
else
{
cout<<"<界符,"<<"<>"<<endl;
f1.write("<运算符,<>\n",12);break;
}
case '>':
GetChar();
if(ch=='>')
{
cout<<"<运算符,"<<">>>"<<endl;
f1.write("<运算符,>>>\n",13);break;
}
else
{
Retract();
cout<<"<界符,"<<">>"<<endl;
f1.write("<运算符,>>\n",12);break;
}
}
}
}
while(k<q)
{
for(int p=0;p<50;p++)
strToken[p]='\0';
i=0;
analyze();
}
}
void main()
{
fstream file;
file.open("tz.txt",ios::in);
if(!file)
{
cout<<"can't open tz.txt.\n";
}
f1.open("result.txt",ios::out);
if(!f1)
{
cout<<"can't open result.txt.\n";
}
file.get(ch);
form[0]=ch;
for( q=1;form[q-1]!='#';q++)
{
file.get(ch);
form[q]=ch;
if(form[q]=='#')
{
cout<<"你文件里的程序段为的程序段为\t";
file.write(form,q);
for(q=0;q<strlen(form);q++)
cout<<form[q];
break;
}
}
file.close();
cout<<endl;
analyze();
f1.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -