?? new.cpp
字號:
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#include <string.h>
#include <fstream.h>
#include <ctype.h>
char* ComWords[65]=
{"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",
"alpha", "digit", "'char'", "(", ")",
"*", "*/", "+", ",", "-",
".", "..", "/", "/*", ":",
":=", ";", "<", "<=", "<>",
"=", ">", ">=", "[", "]",
0 };
char* variable[30]; //變量表,序列號代表值
int si=0; //變量序列編號:變量二元式的后一項
char* err[30]; //出錯序列表
int ern[100]; //出錯行號
int ei=0; //出錯序列表編號
int ViewT1(char* tp) //檢查字符串是否在保留字表
{
for (int t=0;(ComWords[t]!=NULL);t++)
{
if (!strcmp(tp,ComWords[t]))
return t+1;
}
return 0;
}
int ViewT2(char* tp) //檢查字符串是否在變量表
{
for (int t=0;(variable[t]!=NULL);t++)
{
if (!strcmp(tp,variable[t]))
{
return t+1;
}
}
return 0;
}
void Type(int co,int vi) //輸出二元式,co 代碼序號; vi 后綴數字 (0 表示保留字);
{
if(vi==0)
{
cout<<"(";
if(co<10)
cout<<" ";
cout<<co<<", -) ";
}
else
{
cout<<"("<<co<<",";
if(vi<10)
cout<<" ";
cout<<vi<<") ";
}
}
void Charge(char* tp,int& con,int ln) //判斷函數,分析送入的字串
{
int cha=0; //字符串常量標志,1為常量開始
char string[20];
char temp[2]; //用于接受一個字符的臨時串指針
temp[1]=NULL;
int i=0;
int sin=1; //注釋行
while(tp[i]!=NULL)
{
//如果當前字符是整數
if(isdigit(tp[i])!=0&&sin==1)
{
while(isdigit(tp[i])!=0) //讀入整個數字常量
{
temp[0]=tp[i];
strcat(string,temp);
i++;
}
Type(37,si+1);
variable[si]=new char [sizeof (string)];
strcpy(variable[si],string);
si++;
con++;
if ((con)%5==0)
cout<<endl;
}
for (int j=0;string[j]!=NULL;j++) //釋放string
string[j]=NULL;
//如果當前字符是字母
if(isalpha(tp[i])!=0&&sin==1)
{
while( (isalpha(tp[i])!=0)||(isdigit(tp[i])!=0) ) //讀入整個字串,碰到“符號”結束
{
temp[0]=tp[i];
strcat(string,temp);
i++;
}
if(ViewT1(string)) //如果是保留字
{
Type(ViewT1(string),0);
con++;
if ((con)%5==0)
cout<<endl;
}
else //是變量
{
int bs=ViewT2(string); //如果已經存在于變量表,返回變量序號
if(bs)
Type(36,bs);
else //是新變量,插入變量表
{
Type(36,si+1);
variable[si]=new char [sizeof (string)];
strcpy(variable[si],string);
si++;
}
con++;
if ((con)%5==0)
cout<<endl;
}
}
for (j=0;string[j]!=NULL;j++) //釋放string
string[j]=NULL;
while(tp[i]==32&&sin==1) //處理空格
i++;
//如果當前字符是“符號”
if( (tp[i]!=NULL)&&(tp[i]!=32)&&(isalpha(tp[i])==0)&&(isdigit(tp[i])==0) )
{
while( (tp[i]!=NULL)&&(tp[i]!=32)&&(isalpha(tp[i])==0)&&(isdigit(tp[i])==0) )
{
if(tp[i]=='\'') //是字符串常量,跳出處理
{
cha=1;
break;
}
temp[0]=tp[i];
strcat(string,temp);
i++;
}
if(ViewT1(string)) //如果是保留字
{
if( (ViewT1(string)!=49)&&(ViewT1(string)!=42) )
{
Type(ViewT1(string),0);
con++;
if ((con)%5==0)
cout<<endl;
}
else
{
sin*=(-1);
}
}
else if(!cha) //無定義符號,報錯
{
cout<<"#"<<ei+1;
err[ei]=new char [sizeof ("Sign define error ! in line ")];
strcpy(err[ei],"Sign define error ! in line ");
ern[ei]=ln;
ei++;
}
if(cha==1) //處理字符串常量
{
for (j=0;string[j]!=NULL;j++) //釋放string
string[j]=NULL;
strcat(string,"'");
i++;
while(tp[i]!=NULL)
{
if(tp[i]=='\'') //等到結尾的“ ’”出現
{
strcat(string,"'");
i++;
cha=0;
break;
}
temp[0]=tp[i];
strcat(string,temp);
i++;
}
if(cha!=0) //如果字符串常量沒有后標志符“ ’”,報錯
{
cout<<"#"<<ei+1;
err[ei]=new char [sizeof ("String const define error! in line ")];
strcpy(err[ei],"String const define error! in line ");
ern[ei]=ln;
ei++;
}
Type(38,si+1); //插入變量表
variable[si]=new char [sizeof (string)];
strcpy(variable[si],string);
si++;
con++;
if ((con)%5==0)
cout<<endl;
}
}
if(sin!=1) //處理注釋行
i++;
for (j=0;string[j]!=NULL;j++) //釋放string
string[j]=NULL;
}
if(sin!=1) //注釋行出錯
{
cout<<"#"<<ei+1;
err[ei]=new char [sizeof ("Remark line error! in line ")];
strcpy(err[ei],"Remark line error! in line ");
ern[ei]=ln;
ei++;
}
}
void test()
{
cout<<" please input text name: ";
char* path;
path=new char [20];
cin>>path;
int con=0; //輸出二元式的個數
cout<<" Open files: "<<path;
cout<<endl<<" input command words:"<<endl;
char inp[20];
ifstream input1(path); //用input1接收文本作為輸出設備,顯示原文
for (int i=0;;i++)
{
input1.getline(inp,128);
if(input1.fail())
break;
cout<<inp<<endl;
}
cout<<endl;
getch();
cout<<path;
cout<<endl<<" output the command word's ID:"<<endl<<endl;
ifstream input2(path); //用input2接收文本作為輸出設備,處理字符串
for (i=0;;i++)
{
input2.getline(inp,128);
if(input2.fail())
break;
Charge(inp,con,i+1);
}
cout<<endl<<endl;
for (int j=0;err[j]!=NULL;j++) //顯示出錯信息
cout<<"#"<<j+1<<" "<<err[j]<<ern[j]<<endl;
getch();
for (j=0;variable[j]!=NULL;j++) //清空variable
delete []variable[j];
si=0;
for (j=0;err[j]!=NULL;j++) //清空err
delete []err[j];
ei=0;
}
void main ()
{
cout<<"***************************************************************"<<endl
<<"** **"<<endl
<<"** 'SIMPLE' language analysis 1.0 2002.5.20 **"<<endl
<<"** **"<<endl
<<"** CS99(2) QiuGuangHua No.37 **"<<endl
<<"** **"<<endl
<<"***************************************************************"<<endl
<<endl;
S:
cout<<endl<<endl<<" Please choose the test type:"<<endl
<<" t. test"<<endl
<<" q. quit"<<endl;
char cho;
cin>>cho;
switch(cho)
{
case 't': test();goto S;
case 'q': break;
default: goto S;
}
cout<<endl<<" See you next time!"<<endl<<endl;
getch();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -