lvjuan5.cpp
来自「词法分析工作过程中建立符号表、常量表。 并以文本文件形式输出(按字典顺序输出)」· C++ 代码 · 共 266 行
CPP
266 行
#include<iostream.h>
#include<string>
#include<stdio.h>
FILE *out;
char * KeyWord[45]={"auto","break","case","char","const","continue",
"default","do","double","else","enum","extern",
"float","for","goto","if","int","long","return",
"short","signed","sizeof","static","struct","switch",
"typedef","unsigned","void","while","catch","class",
"const_cast","delete","friend","inline","new","operator",
"private","protected","public","template","this",
"throw","try","virtual"};
int i=0,j=0,k=0,t=0;//搜索指示器
char ch;//存放最新读入的原程序字符
char strToken[20];//存放构成单词符号的字符串
char * chr_form[100];//字符表
char * int_form[100];//常数表
char form[1000];
int q=0;
int temp;
void GetChar()//将下一个字符读入ch中,搜索指示器前移一字符位
{
ch=form[k];
k++;
}
void GetBC()//检查ch中的字符是否为空白,若是则调用Getchar直至ch中进入
//一个非空白字符
{
while(ch==' ')
{
//k--;
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中的字符是否为数字
{
//k--;
if(((ch)<='9')&&( (ch)>='0'))
return (1);
else
return (0);
}
int Reserve()//对strToken中的字符串查找保留字表,若它是一个保留字
//则返回它的编码,否则返回-1值
{
for(int q=0;q<45;q++)
{
if (strcmp(KeyWord[q],strToken)==0)
return q;
if(q==44)
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();
//cout<<"此处没有错"<<endl;
if(IsLetter())
{
while(IsLetter()||IsDigit())
{
Concat();
GetChar();
}
//cout<<"此处没有错"<<endl;
Retract();
code=Reserve();
switch(code)
{
case -1:
printf("(字符串,form) ");
fputs(form,out);break;
default:
printf("(关键字,strToken) ");
fputs(form,out);
}
}
else
{
if(IsDigit())
{
while(IsDigit()||ch=='.')
{
Concat();
GetChar();
}
Retract();
printf("(常量,form) ");
fputs(form,out);
}
else
{
switch (ch)
{
case '=':
case '/':
case '%':
case '*':
case '-':
GetChar();
if(ch=='-')
{
printf("(运算符,$-) ");
fputs("--",out);
}
else
{
Retract();
printf("(,$-) ");
fputs("-",out);
}
case '+':
GetChar();
if(ch=='+')
{
printf("(,$++) ");
fputs("++",out);
}
else
{
Retract();
printf("(,$+) ");
fputs("+",out);
}
case '|':
GetChar();
if(ch=='|')
{
printf("(,$||) ");
fputs("||",out);break;
}
else
{
Retract();
printf("(非法字符,$|) ");
fputs("|",out);
}
case '&':
GetChar();
if(ch=='&')
{
printf("(,$&) ");
fputs("&",out);break;
}
case '(':
case ';':
case ')':
case ':':
case'[':
case']':
case '"':
case'\'':
case '{':
case ',':
case '}':cout<<"界符, "<<ch<<endl;break;
case '<':
GetChar();
if(ch=='<')
{
printf("(运算符,$<) ");
fputs("<",out);
}
else
{
printf("(界符,$<<) ");
fputs("<<",out);break;
}
case '>':
GetChar();
if(ch=='>')
{
printf("(运算符,$>) ");
fputs(">",out);break;
}
else
{
Retract();
printf("(界符,$>>) ");
fputs(">>",out);
}
}
}
}
while(k<q)
{
// strcpy(strToken," ");
//strcpy(strToken,"\0");
for(int p=0;p<50;p++)
strToken[p]='\0';
i=0;
analyze();
}
}
void main()
{
char outfile[50];
cout<<"请输入一段程序,以#号结束:"<<endl;
form[0]=cin.get();
for( q=1;form[q-1]!='#';q++)
{
form[q]=cin.get();
if(form[q]=='#')
{
cout<<"你输入的程序段为\t";
cout.write(form,q);
break;
}
}
puts("Input the outfile:");
scanf("%s",outfile);
if((out=fopen(outfile,"form+"))==NULL)
{
printf("cannot open outfile!\n");
exit(0);
}
cout<<endl;
analyze();
fclose(out);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?