#include<iostream>
#include<stack>
#include<string>
#include<set>
#include<iomanip>
#include<vector>
#include<fstream>
using namespace std;
//变量声明及定义
enum Operator{pluss,minuss,multiply,divide,power,lparen,rparen,variable,neq,equ};
enum Flag{var,opera,temp,num,doo,ndo,whilee};
string sstack[50];
vector<string>sorcecode;
stack <string> sStack;//规约栈
char index='0';//temp的编码
const int RelTable[10][10]=//运算符优先关系表
{
{1,1,-1,-1,-1,-1,1,-1,1,2},
{1,1,-1,-1,-1,-1,1,-1,1,2},
{1,1,1,1,-1,-1,1,-1,1,2},
{1,1,1,1,-1,-1,1,-1,1,2},
{1,1,1,1,-1,-1,1,-1,1,2},
{-1,-1,-1,-1,-1,-1,0,-1,2,2},
{1,1,1,1,1,2,1,2,1,2},
{1,1,1,1,1,2,1,2,1,1},
{-1,-1,-1,-1,-1,-1,2,-1,0,-1},
{-1,-1,-1,-1,-1,-1,2,-1,1,2}
};
char exp[100];
set<string>sVar;
set<string>sNum;
int ierrorline;
set<string>sWhile;
stack<int>iaddress;
int ioutline=1000;//输出的行数计数,同时也是地址码
int iinputlinecount=0;//输入的代码行数计数
//======================函数声明
int ReturnNum(string word);
int Compare(string first,string second);
bool WhetherVT(string word);
bool WhetherVar(string word);
//======================函数声明结束
//======================函数定义
bool WhetherVT(string word)//运算符优先分析中判断是否是终结符
{
if(word[0]=='_'&&word[1]=='_')
return 0;
else
return 1;
}
bool Whethervtx(string &x)//LL1中判断是否为终结符
{
if(x=="do"||x=="ndo"||x=="{"||x=="}"||x=="while"||x=="("||x==")"||x=="<"||x=="<="||x==">"||x=="=="||x=="i")
return 1;
else if(sWhile.count(x))
return 1;
else
return 0;
}
void ErrorPlace(int &iIn,char *&strInput)//地位错误定位
{
cout<<endl<<endl<<endl;
cout<<"ERROR!"<<"Line("<<ierrorline<<") ";
for(int ier=0;ier<iIn-1;ier++)
{
cout<<strInput[ier];
}
cout<<"<error>"<<strInput[iIn-1]<<""<<endl;
}
void LexAnalysiswhile(char & charIn,string &word,int & iIn,Flag& flag,char*strInput)//LL1词法分析
{
word="";
charIn=strInput[iIn];
if(charIn=='{')
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
return ;
}
else if(charIn==';')
{
word+='$';
iIn++;
charIn=strInput[iIn];
return ;
}
else if(charIn=='}')
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
return ;
}
else if(charIn=='(')
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
return ;
}
else if(charIn==')')
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
return ;
}
else if(charIn=='<')
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
return ;
}
else if(charIn=='>')
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
return ;
}
else if(charIn=='<')
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
if(charIn=='=')
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
return ;
}
}
else if(charIn=='=')
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
if(charIn=='=')
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
return ;
}
}
else if(charIn=='o')
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
if(charIn=='p')
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
if(charIn>0)
while(isalpha(charIn)||isdigit(charIn))
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
}
return ;
}
}
else if(charIn=='w')
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
if(charIn=='h')
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
if(charIn=='i')
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
if(charIn=='l')
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
if(charIn=='e')
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
if(charIn>0)
while(isalpha(charIn)||isdigit(charIn))
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
}
flag=whilee;
return ;
}
}
}
}
}
else if(charIn=='$')
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
return ;
}
else if(charIn=='n')
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
if(charIn=='d')
{
word +=charIn;
iIn++;
charIn=strInput[iIn];
if(charIn=='o')
{
word+=charIn;
}
iIn++;
charIn=strInput[iIn];
if(charIn>0)
while(isalpha(charIn)||isdigit(charIn))
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
if(charIn<0)
break;
}
flag=ndo;
return ;
}
else
cout<<"error in lexanalysis of do"<<endl;
return ;
}
if(charIn=='d')
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
if(charIn=='o')
{
word +=charIn;
flag=doo;
iIn++;
charIn=strInput[iIn];
if(charIn>0)
while(isalpha(charIn)||isdigit(charIn))
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
if(charIn<0)
break;
}
return ;
}
else
{
goto charread;
}
return ;
}
else if(isalpha(charIn))
{
charread: while(isalpha(charIn)||isdigit(charIn))
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
if(charIn<0)
break;
}
sWhile.insert(word);
return;
}
else if(isdigit(charIn))
{
while(isdigit(charIn))
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
if(charIn<0)
break;
}
sWhile.insert(word);
flag=num;
return;
}
iIn++;
charIn=strInput[iIn];
}
void LexAnalysis(char & charIn,string &word,int & iIn,Flag& flag,char*strInput)//运算符优先词法分析
{
word="";
charIn=strInput[iIn];
if(isalpha(charIn))
{
while(isalpha(charIn)||isdigit(charIn))
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
}
sVar.insert(word);
flag=var;
return;
}
else if(charIn=='e')
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
if(charIn=='d')
{
word +=charIn;
iIn++;
charIn=strInput[iIn];
if(charIn=='o')
{
word+=charIn;
}
iIn++;
charIn=strInput[iIn];
flag=ndo;
return ;
}
else
cout<<"error in lexanalysis of do"<<endl;
return ;
}
else if(isdigit(charIn))
{
while(isdigit(charIn))
{
word+=charIn;
iIn++;
charIn=strInput[iIn];
}
sNum.insert(word);
flag=num;
return;
}
else if(charIn=='+'||charIn=='-'||charIn=='*'||charIn=='/'||charIn=='^'||charIn=='('||charIn==')'||charIn=='#'||charIn=='=')
{
word+=charIn;
flag=opera;
}
iIn++;
charIn=strInput[iIn];
}
void printline()//画横线
{
for(int iprint=1;iprint<=74;iprint++)
cout<<"-";
cout<<endl;
};
void Parsing(char *& charLine,string &word)//赋值语句的语法分析&生成中间代码
{
char inputChar;//the word of input
int iIn=0;//index of input
stack<string> sStack;
char * strInput=new char[100];//input
int ichindex=0;
do
{
strInput[ichindex]=charLine[ichindex];
ichindex++;
}while(charLine[ichindex]!=';');
strInput[ichindex]='#';
strInput[ichindex+1]=';';
int k=1;
int j=0;
sstack[1]="#";
iIn=0;
while(';'!=strInput[iIn])
{
Flag flag;
string topword;
int iCompare=0;
string q;
LexAnalysis(inputChar,word,iIn,flag,strInput);
go: if(WhetherVT(sstack[k]))
{
j=k;
}
else
{
j=k-1;
}
iCompare=Compare(sstack[j],word);
if(iCompare==1)
{
do
{
q=sstack[j];
if(WhetherVT(sstack[j-1]))
{
j=j-1;
}
else
{
j=j-2;
}
}while(-1!=Compare(sstack[j],q));
if(j+1==k)
{
if(k==2)
{
if(sVar.count(sstack[k]))
{
string sOut="__"+sstack[k];
sstack[k]=sOut;
}
else
{
ErrorPlace(iIn,strInput);
cout<<"错误提示:只能为左值赋值"<<endl;
return ;
}
}
else if(sVar.count(sstack[k])||sNum.count(sstack[k]))
{
string sOut="__"+sstack[k];