#include <fstream>
#include <stack>
#include <map>
#include <string>
using namespace std;
#define zhengshu 1 //int
#define IF 2 //if
#define ELSE 3 //else
#define shishu 4 //float
#define PRINT 5 //print
#define ID 6 //identify
#define CONSTANT 7 //constant
#define op_fuzhi 8 //=
#define op_add 9 //+
#define op_mul 10 //*
#define op_2star 11 //**
#define div_fenhao 12 //;
#define syl_ls 13 //(
#define syl_rs 14 //)
#define syl_lb 15 //{
#define syl_rb 16 //}
#define sbl_lm 17 //[
#define sbl_rm 18 //]
#define op_sub 19 //-
#define op_div 20 // /
#define div_douhao 21 //,
#define rop_yu 22 //&&
#define op_or 23 //||
#define rop_fei 24 //!
#define rop_equal 25 //==
#define rop_dayu 26 //>
#define rop_xiaoyu 27 //<
#define rop_buxiaoyu 28 //>=
#define rop_budayu 29 //<=
#define rop_uneql 30 //!=
#define TEMP 31
#define NULL 0
#define JMP 32
#define GOTO 33 //goto標識
/*****************************重要數據結構的聲明開始*************************/
struct delos
{
int code,value;
}*result; //結果
//變量表
struct analyse
{
int state;
char sign;
};
struct list
{
int value;
list *next;
};
//條件語句的LR(1)分析表,110表示接受,999表示出錯
int table[38][20]={
/*0*/,
/*1*/,
/*2*/,
/*3*/,
/*4*/,
/*5*/,
/*6*/,
/*7*/,
/*8*/,
/*9*/,
/*10*/,
/*11*/,
/*12*/,
/*13*/,
/*14*/,
/*15*/,
/*16*/,
/*17*/,
/*18*/,
/*19*/,
/*20*/,
/*21*/,
/*22*/,
/*23*/,
/*24*/,
/*25*/,
/*26*/,
/*27*/,
/*28*/,
/*29*/,
/*30*/,
/*31*/,
/*32*/,
/*33*/,
/*34*/,
/*35*/,
/*36*/,
/*37*/
};
/*****************************重要數據結構的聲明結束*************************/
/*********************************全局變量聲明開始**************************/
int place=1;
int nextpos=1;
stack<list *> stknext;
stack<list *> stktrue;
stack<list *> stkfalse;
stack<int> stkpos;
stack<delos> stktemp;//常量,變量,臨時變量
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<resultlen;i++)
{ result[i].code=p3[i].code;
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<nowvar;i++)
var[i]=p1[i];
delete[] p1;
}
void renewconst()
{
float *p2=myconst;
int i;
constlen+=10;
myconst=new float[constlen];
for(i=0;i<nowconst;i++)
myconst[i]=p2[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]!='.'&&d<i)
d++;
for(j=d-1;j>=0;j--)
{ a=a+(c[j]-48)*b;
b=b*10;
}
b=10;
for(j=d+1;j<i;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 !"<<endl;
return;
}
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 :"<<strtoken<<endl;
}
}
else if(ch=='|')
{ ch=myfile.get();
if(ch=='|')
insertresult(op_or,0);
else
{
myfile.seekg(-1,ios::cur);
myfile.get(strtoken,10);
cout<<"ERROR :"<<strtoken<<endl;
}
}
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 :"<<strtoken<<endl;
myfile.seekg(1,ios::cur);
}
}
}
myfile.close();
cout<<"詞法分析成功啦!!"<<endl;
}
/**********************************詞法分析函數結束***********************/
妳的串號我已經記下,采納後我會幫妳制作