词法分析器源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#include
#include
#include
2,999,999,999,999,999,999,999,999,999,11,13,999,999,999,999,27,999,999},
/*20*/{999,12,999,999,999,999,999,999,999,999,999,11,13,999,999,999,999,28,999,999},
/*21*/{999,12,999,999,999,999,999,999,999,999,999,11,13,999,999,999,999,29,999,999},
/*22*/{999,999,71,71,999,71,71,999,71,71,71,999,999,71,999,999,999,999,999,999},
/*23*/{999,999,30,999,999,999,999,999,999,20,21,999,999,999,999,999,999,999,999,999},
/*24*/{3,999,999,999,999,999,999,999,999,999,999,999,4,999,999,31,999,999,999,999},
/*25*/{999,12,999,999,999,999,999,9,999,999,999,11,13,999,999,999,32,10,999,999},
/*26*/{999,12,999,999,999,999,999,9,999,999,999,11,13,999,999,999,33,10,999,999},
/*27*/{999,999,68,999,999,68,68,999,999,20,21,999,999,999,999,999,999,999,999,999},
/*28*/{999,999,69,69,999,69,69,999,69,69,21,999,999,69,999,999,999,999,999,999},
/*29*/{999,999,70,70,999,70,70,999,70,70,70,999,999,70,999,999,999,999,999,999},
/*30*/{999,999,72,72,999,72,72,999,72,72,72,999,999,72,999,999,999,999,999,999},
/*31*/{999,999,999,75,999,999,999,999,999,999,999,999,999,63,999,999,999,999,999,34},
/*32*/{999,999,65,999,999,65,65,999,999,999,999,999,999,999,999,999,999,999,999,999},
/*33*/{999,999,66,999,999,16,66,999,999,999,999,999,999,999,999,999,999,999,999,999},
/*34*/{999,999,999,35,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999,999},
/*35*/{74,999,999,999,999,999,999,999,999,999,999,999,74,74,999,999,999,999,36,999},
/*36*/{3,999,999,999,999,999,999,999,999,999,999,999,4,999,999,37,999,999,999,999},
/*37*/{999,999,999,62,999,999,999,999,999,999,999,999,999,62,999,999,999,999,999,999}
};
/*****************************重要数据结构的声明结束*************************/
/*********************************全局变量声明开始**************************/
int place=1;
int nextpos=1;
stack stknext;
stack stktrue;
stack stkfalse;
stack
stack
delos temp;
delos gen[50][4];//生成的三地址
string str[31]={"","int","if","else","float","print","标识符","常数",
"=","+","*","**",";","(",")",
"{","}","[","]","-","/",",","&&","||","!",
"==",">","<",">=","<=","!="};
//变量
string *var;
int varlen=0,nowvar=1;
//常量
float *myconst;
int constlen=0,nowconst=1;
int resultlen=0,nowresult=0;
/*********************************全局变量声明结束**************************/
void renewresult()
{
delos *p3=result;
int i;
resultlen+=10;
result=new delos[resultlen];
for(i=0;i
result[i].value=p3[i].value;
}
delete[] p3;
}
void renewvar()
{
string *p1=var;
int i;
varlen+=10;
var=new string[varlen];
for(i=0;i
delete[] p1;
}
void renewconst()
{
float *p2=myconst;
int i;
constlen+=10;
myconst=new float[constlen];
for(i=0;i
delete[]
p2;
}
bool isletter(char c) //判别是否字母
{
if(c>64&&c<91||c>96&&c<123)
return true;
return false;
}
bool isdigital(char c) //判别是否数字
{ if(c>47&&c<58)
return true;
return false;
}
int reserve(char c[],int i)
{
string s(c,0,i);
for(int j=1;j<7;j++)
if(s==str[j])
return j;
return 0;
}
void insertresult(int code,int value)
{ if(nowresult>resultlen)
renewresult();
result[nowresult].code=code;
result[nowresult++].value=value;
}
void insertid(char c[],int i)
{ string s(c,0,i);
insertresult(ID,nowvar);
if(nowvar>varlen)
renewvar();
var[nowvar++]=s;
}
//插入常数,为浮点型
void insertconst(char c[],int i)
{ int d=0,j;
float a=0,b=1;
while(c[d]!='.'&&dd++;
for(j=d-1;j>=0;j--)
{ a=a+(c[j]-48)*b;
b=b*10;
}
b=10;
for(j=d+1;j{a=a+(c[j]-48)/b;
b=b*10;
}
insertresult(CONSTANT,nowconst);
if(nowconst>constlen)
renewconst();
myconst[nowconst++]=a;
}
/**********************************词法分析函数开始***********************/
void wordanalyse()
{
char strtoken[10];
int i=0,code;
char ch;
ifstream myfile;
myfile.open("sourcefile.txt");
if(!myfile)
{ cout<<"Can not open input file !"<
}
while(!myfile.eof())
{ i=0;
for(ch=myfile.get();ch==' '||ch==13||ch==10;ch=myfile.get())
;
if(isletter(ch))
{while(isletter(ch)||isdigital(ch))
{strtoken[i++]=ch;
ch=myfile.get();
}
myfile.seekg(-1,ios::cur);
code=reserve(strtoken,i);
if(code==0)
insertid(strtoken,i);
else
{insertresult(code,0);
}
}
else if(isdigital(ch))
{while(isdigital(ch)||ch=='.')
{strtoken[i++]=ch;
ch=myfile.get();
}
myfile.seekg(-1,ios::cur);
insertconst(strtoken,i);
}
else if(ch=='=')
{ ch=myfile.get();
if(ch=='=')
insertresult(rop_equal,0);
else
{insertresult(op_fuzhi,0);
myfile.seekg(-1,ios::cur);
}
}
else if(ch=='+')
{insertresult(op_add,0);
}
else if(ch=='*')
{ ch=myfile.get();
if(ch=='*')
insertresult(op_2star,0);
else
{insertresult(op_mul,0);
myfile.seekg(-1,ios::cur);
}
}
else if(ch==';')
{ insertresult(div_fenhao,0);
}
else if(ch=='(')
{insertresult(syl_ls,0);
}
else if(ch==')')
{insertresult(syl_rs,0);
}
else if(ch=='{')
{ insertresult(syl_lb,0);
}
else if(ch=='}')
{ insertresult(syl_rb,0);
}
else if(ch=='[')
{ insertresult(sbl_lm,0);
}
else if(ch==']')
{ insertresult(sbl_rm,0);
}
else if(ch=='-')
{ insertresult(op_sub,0);
}
else if(ch=='/')
{ insertresult(op_div,0);
}
else if(ch==',')
{ insertresult(div_douhao,0);
}
else if(ch=='&')
{ ch=myfile.get();
if(ch=='&')
insertresult(rop_yu,0);
else
{
myfile.seekg(-1,ios::cur);
myfile.get(strtoken,10);
cout<<"ERROR :"<
}
else if(ch=='|')
{ ch=myfile.get();
if(ch=='|')
insertresult(op_or,0);
else
{
myfile.seekg(-1,ios::cur);
myfile.get(strtoken,10);
cout<<"ERROR :"<
}
else if(ch=='!')
{ ch=myfile.get();
if(ch=='=')
insertresult(rop_uneql,0);
else
{insertresult(rop_fei,0);
myfile.seekg(-1,ios::cur);
}
}
else if(ch=='>')
{ ch=myfile.get();
if(ch=='=')
insertresult(rop_buxiaoyu,0);
else
{insertresult(rop_dayu,0);
myfile.seekg(-1,ios::cur);
}
}
else if(ch=='<')
{ ch=myfile.get();
if(ch=='=')
insertresult(rop_budayu,0);
else
{insertresult(rop_xiaoyu,0);
myfile.seekg(-1,ios::cur);
}
}
else
{if(ch!=-1)
{myfile.seekg(-1,ios::cur);
myfile.get(strtoken,10);
cout<<"ERROR :"<
}
}
}
myfile.close();
cout<<"词法分析成功啦!!"<
/**********************************词法分析函数结束***********************/