完成 1.词法分析 2.语法分析 3.语义分析和中间代码生成 4.代码优化 5.目标代码生成
文法:
1<程序>→<main关键字>(){<声明序列><语句序列>}
Project->main(){DeclareStatement}
2<声明序列>→<声明序列><声明语句>|<声明语句>|<空>
Declare->DeclareSe|Se|&
--------------------------
Declare->SeDeclare_|&
Declare_->SeDeclare_|&
-------------------------
3<声明语句>→int<标识符表>;
Se->intIdentifiers;
4<标识符表>→<标识符>,<标识符表>|<标识符>
Identifiers->Identifier,Identifiers|Identifier
-----------------------------------
Identifiers->IdentifierIdentifiers_
Identifiers_->,Identifiers|&
5<语句序列>→<语句序列><语句>|<语句>
Statement->StatementSentence|Sentence
------------------------------------------
Statement->SentenceStatement_
Statement_->SentenceStatement_|&
------------------------------------------
6<语句>→< if语句>|< while语句>|< for语句>|<赋值语句>
Sentence->If|While|For|Assignent
7<赋值语句>→<表达式>;
Assignment->Expression;
8<表达式>→<标识符>=<算数表达式>
Expression->Identifier=Alex
9<布尔表达式>→<算数表达式> |<算数表达式><关系运算符><布尔表达式>
Boex->Alex|AlexOperatorBoex
------------------------------
Boex->AlexBoex_
Boex_->OperatorBoex|&
---------------------------
<关系运算符>→>|<|>=|<=|==|!=
/*Operator*/
10<算数表达式>→<算数表达式>+<项>|<算数表达式>-<项>|<项>
Alex->Alex+Term|Alex-Term|Term
------------------------------------------
Alex->TermAlex_
Alex_->+TermAlex_|-TermAlex_|&
------------------------------------------
11<项>→<项>*<因子>|<项>/<因子>|<因子>
Term->Term*Factor|Term/Factor|Factor
----------------------------------------
Term->FactorTerm_
Term_->*FactorTerm_|&|/FactorTerm_
--------------------------------------
12<因子>→<标识符>|<无符号整数>|(<算数表达式>)
Factor->Identifier|Integer|(Alex)
<标识符>→<字母>|<标识符><字母>|<标识符><数字>
/*Identifier*/
<无符号整数>→<数字>|<无符号整数><数字>
/*Integer*/
13<复合语句>→{<语句序列>}
Compound->{Statement}
14< if语句>→< if关键字>(<布尔表达式>)<复合语句>//else先不实现了
If->if(Boex)Compound
15<while语句>→< while关键字>(<布尔表达式>)<复合语句>
While->while(Boex)Compound
16< for语句>→< for关键字>(<布尔表达式>;<布尔表达式>;<布尔表达式>)<复合语句>
For->for()Boex;Boex;Boex)
终结符:Boundary[6] Operator[11] Integer Indentifier key[6]
预测分析表:
代码:
#include <iostream>
#include <stdio.h>
#include <sstream>
#include <vector>
#include <queue>
#include <fstream>
#include <iostream>
using namespace std;
/*******终结符们*******/
vector<string> Indentifier;
vector<string> Integer;
string Boundary[6]= {"{","}","(",")",",",";"};
string Operator[8]= {"<",">",">=","<=","==","!=","&&","||"};
string key[6]= {"main", "if", "else", "for", "while", "int"};
queue<string> seq;
#include <stdio.h>
#include <queue>
#include <iostream>
using namespace std;
int grammerCheck();
int Project();
int Declare();
int Declare_();
int Se();
int Identifiers();
int Identifiers_();
int Statement();
int Statement_();
int Sentence();
int Assignment();
int Expression();
int Compound();
int If();
int While();
int For();
int Boex();
int Boex_();
int Alex();
int Alex_();
int Term();
int Term_();
int Factor();
void Quater();
ofstream outgrammerfile("E:\\com\\grammerCheck.txt", ios::out);
int grammerCheck()//<程序>→<main关键字>(){<声明序列><语句序列>}
{
if(seq.front()=="main")
{
seq.pop();
if(!seq.size())
return 0;
if(seq.front()=="(")
{
seq.pop();
if(!seq.size())
return 0;
if(seq.front()==")")
{
seq.pop();
if(!seq.size())
return 0;
if(seq.front()=="{")
{
cout<<"<程序> → <main>(){ <声明序列> <语句序列> }"<<endl;
outgrammerfile <<"<程序> → <main>(){ <声明序列> <语句序列> }"<<endl;
seq.pop();
if(Declare())
{
if(Statement())
{
if(!seq.size())
return 0;
if(seq.front()=="}")
{
seq.pop();
return 1;
}
}
}
}
}
}
}
return 0;
}
int Declare()//<声明序列>→<声明序列><声明语句>|<声明语句>|<空>
{
if(!seq.size())
return 0;
string ch=seq.front();
for(int i=0; i<Indentifier.size(); i++)
{
if(Indentifier[i]==ch)
{
return 1;
}
}
if(ch=="if")
return 1;
if(ch=="while")
return 1;
if(ch=="for")
return 1;
if(ch=="int")
{
printf("<声明序列> → <声明序列><声明语句> | <声明语句> | <空>\n");
outgrammerfile <<"<声明序列> → <声明序列><声明语句> | <声明语句> | <空>"<<endl;
if(Se())
{
if(Declare_())
return 1;
}
}
return 0;
}
int Declare_()//Declare临时变量
{
if(!seq.size())
return 0;
string ch=seq.front();
for(int i=0; i<Indentifier.size(); i++)
{
if(Indentifier[i]==ch)
{
return 1;
}
}
if(ch=="if")
return 1;
if(ch=="while")
return 1;
if(ch=="for")
return 1;
if(ch=="int")
{
printf("<声明序列> → <声明序列><声明语句> | <声明语句> | <空>\n");
outgrammerfile <<"<声明序列> → <声明序列><声明语句> | <声明语句> | <空>"<<endl;
if(Se())
{
if(Declare_())
return 1;
}
}
return 0;
}
int Se()//<声明语句>→int<标识符表>;
{
if(!seq.size())
return 0;
if(seq.front()=="int")
{
seq.pop();
printf("<声明语句> → int <标识符表> ; \n");
outgrammerfile <<"<声明语句> → int <标识符表> ; "<<endl;
if(Identifiers())
{
if(!seq.size())
return 0;
if(seq.front()==";")
{
seq.pop();
return 1;
}
}
}
return 0;
}
int Identifiers()//<标识符表>→<标识符>,<标识符表>|<标识符>
{
if(!seq.size())
return 0;
string ch=seq.front();
seq.pop();
for(int i=0; i<Indentifier.size(); i++)
{
if(Indentifier[i]==ch)
{
printf("<标识符表> → <标识符> , <标识符表> | <标识符>\n");
outgrammerfile <<"<标识符表> → <标识符> , <标识符表> | <标识符>"<<endl;
if(Identifiers_())
{
return 1;
}
}
}
return 0;
}
int Identifiers_()//Identifiers临时变量
{
if(!seq.size())
return 0;
string ch=seq.front();
//cout<<"当前是,seq="<<ch<<endl;
if(ch==";")
{
return 1;
}
if(ch==",")
{
seq.pop();
if(Identifiers())
{
return 1;
}
}
return 0;
}
int Statement()//<语句序列>→<语句序列><语句>|<语句>
{
if(!seq.size())
return 0;
string ch=seq.front();
//cout<<"Statement进来了吗"<<endl;
if(ch=="if")
{
printf("<语句序列> → <语句序列><语句> | <语句>\n");
outgrammerfile <<"<语句序列> → <语句序列><语句> | <语句>"<<endl;
if(Sentence())
{
if(Statement_())
{
return 1;
}
}
}
if(ch=="while")
{
printf("<语句序列> → <语句序列><语句> | <语句>\n");
outgrammerfile <<"<语句序列> → <语句序列><语句> | <语句>"<<endl;
if(Sentence())
{
if(Statement_())
{
return 1;
}
}
}
if(ch=="for")
{
printf("<语句序列> → <语句序列><语句> | <语句>\n");
outgrammerfile <<"<语句序列> → <语句序列><语句> | <语句>"<<endl;
if(Sentence())
{
if(Statement_())
{
return 1;
}
}
}
for(int i=0; i<Indentifier.size(); i++)
{
if(Indentifier[i]==ch)
{
printf("<语句序列> → <语句序列><语句> | <语句>\n");
outgrammerfile <<"<语句序列> → <语句序列><语句> | <语句>"<<endl;
if(Sentence())
{
if(Statement_())
{
return 1;
}
}
}
}
return 0;
}
int Statement_()//Statement临时变量
{
if(!seq.size())
return 0;
string ch=seq.front();
if(ch=="}")
{
return 1;
}
if(ch=="if")
{
if(Sentence())
{
if(Statement_())
{
return 1;
}
}
}
if(ch=="while")
{
if(Sentence())
{
if(Statement_())
{
return 1;
}
}
}
if(ch=="for")
{
if(Sentence())
{
if(Statement_())
{
return 1;
}
}
}
for(int i=0; i<Indentifier.size(); i++)
{
if(Indentifier[i]==ch)
{
if(Sentence())
{
if(Statement_())
{
return 1;
}
}
}
}
return 0;
}
int Sentence()//<语句>→< if语句>|< while语句>|< for语句>|<赋值语句>
{
if(!seq.size())
return 0;
string ch=seq.front();
for(int i=0; i<Indentifier.size(); i++)
{
if(Indentifier[i]==ch)
{
printf("<语句> → <赋值语句>\n");
outgrammerfile <<"<语句> → <赋值语句>"<<endl;
if(Assignment())
{
return 1;
}
}
}
if(ch=="if")
{
printf("<语句> → < if 语句>\n");
outgrammerfile <<"<语句> → < if 语句>"<<endl;
if(If())
{
return 1;
}
}
if(ch=="while")
{
printf("<语句> → < while 语句>\n");
outgrammerfile <<"<语句> → < while 语句>"<<endl;
if(While())
{
return 1;
}
}
if(ch=="for")
{
printf("<语句> → < for 语句>\n");
outgrammerfile <<"<语句> → < for 语句>"<<endl;
if(For())
{
return 1;
}
}
return 0;
}
int Assignment()//<赋值语句>→<表达式>;
{
if(!seq.size())
return 0;
string ch=seq.front();
for(int i=0; i<Indentifier.size(); i++)
{
if(Indentifier[i]==ch)
{
printf("<赋值语句> → <表达式> ; \n");
outgrammerfile <<"<赋值语句> → <表达式> ; "<<endl;
if(Expression())
{
if(!seq.size())
return 0;
if(seq.front()==";")
{
seq.pop();
return 1;
}
}
}
}
return 0;
}
int Expression()//<表达式>→<标识符>=<算数表达式>
{
if(!seq.size())
return 0;
string ch=seq.front();
seq.pop();
for(int i=0; i<Indentifier.size(); i++)
{
if(Indentifier[i]==ch)
{
printf("<表达式> → <标识符> = <算数表达式> \n");
outgrammerfile <<"<表达式> → <标识符> = <算数表达式>"<<endl;
if(!seq.size())
return 0;
if(seq.front()=="=")
{
seq.pop();
//算数表达式
if(Alex())
{
return 1;
}
}
}
}
return 0;
}
int Compound()//<复合语句>→{<语句序列>}
{
if(!seq.size())
return 0;
string ch=seq.front();
seq.pop();
if(ch=="{")
{
if(Statement())
{
if(!seq.size())
return 0;
if(seq.front()=="}")
{
seq.pop();
return 1;
}
}
}
return 0;
}
int If()//< if语句>→< if关键字>(<布尔表达式>)<复合语句>
{
if(!seq.size())
return 0;
string ch=seq.front();
seq.pop();
if(ch=="if")
{
if(!seq.size())
return 0;
if(seq.front()=="(")
{
seq.pop();
if(Boex())
{
if(!seq.size())
return 0;
if(seq.front()==")")
{
seq.pop();
if(Compound())
{
return 1;
}
}
}
}
}
return 0;
}
int While()//<while语句>→< while关键字>(<布尔表达式>)<复合语句>
{
if(!seq.size())
return 0;
string ch=seq.front();
seq.pop();
if(ch=="while")
{
if(!seq.size())
return 0;
if(seq.front()=="(")
{
seq.pop();
if(Boex())
{
if(!seq.size())
return 0;
if(seq.front()==")")
{
seq.pop();
if(Compound())
{
return 1;
}
}
}
}
}
return 0;
}
int For()//< for语句>→< for关键字>(<布尔表达式>;<布尔表达式>;<布尔表达式>)<复合语句>
{
if(!seq.size())
return 0;
string ch=seq.front();
seq.pop();
if(ch=="for")
{
if(!seq.size())
return 0;
if(seq.front()=="(")
{
seq.pop();
if(Boex())
{
if(!seq.size())
return 0;
if(seq.front()==";")
{
seq.pop();
if(Boex())
{
if(!seq.size())
return 0;
if(seq.front()==";")
{
seq.pop();
if(Boex())
{
if(!seq.size())
return 0;
if(seq.front()==")")
{
seq.pop();
if(Compound())
{
return 1;
}
}
}
}
}
}
}
}
}
return 0;
}
int Boex()//<布尔表达式>→<算数表达式> |<算数表达式><关系运算符><布尔表达式>
{
//是否确认seq里有值
if(!seq.size())
return 0;
string ch=seq.front();
for(int i=0; i<Indentifier.size(); i++)
{
if(Indentifier[i]==ch)
{
printf("<布尔表达式>→<算数表达式> |<算数表达式><关系运算符><算数表达式>\n");
outgrammerfile <<"<布尔表达式>→<算数表达式> |<算数表达式><关系运算符><算数表达式>"<<endl;
if(Alex())
{
if(Boex_())
{
return 1;
}
}
}
}
for(int i=0; i<Integer.size(); i++)
{
if(Integer[i]==ch)
{
printf("<布尔表达式>→<算数表达式> |<算数表达式><关系运算符><算数表达式>\n");
outgrammerfile <<"<布尔表达式>→<算数表达式> |<算数表达式><关系运算符><算数表达式>"<<endl;
if(Alex())
{
if(Boex_())
{
return 1;
}
}
}
}
return 0;
}
int Boex_()//Boex临时变量
{
if(!seq.size())
return 0;
string ch=seq.front();
if(ch==";"||ch==")")
{
return 1;
}
for(int j=0; j<sizeof(Operator) / sizeof(Operator[0]); j++)
{
if(Operator[j]==ch)
{
seq.pop();
if(Boex())
{
return 1;
}
}
}
return 0;
}
int Alex()//<算数表达式>→<算数表达式>+<项>|<算数表达式>-<项>|<项>
{
if(!seq.size())
return 0;
string ch=seq.front();
for(int i=0; i<Indentifier.size(); i++)
{
if(Indentifier[i]==ch)
{
printf("<算数表达式> → <算数表达式> + <项>|<算数表达式> - <项>|<项>\n");
outgrammerfile <<"<算数表达式> → <算数表达式> + <项>|<算数表达式> - <项>|<项>"<<endl;
if(Term())
{
if(Alex_())
{
return 1;
}
}
}
}
for(int i=0; i<Integer.size(); i++)
{
if(Integer[i]==ch)
{
printf("<算数表达式> → <算数表达式> + <项> | <算数表达式> - <项> | <项>\n");
outgrammerfile <<"<算数表达式> → <算数表达式> + <项> | <算数表达式> - <项> | <项>"<<endl;
if(Term())
{
if(Alex_())
{
return 1;
}
}
}
}
return 0;
}
int Alex_()//Alex临时变量
{
if(!seq.size())
return 0;
string ch=seq.front();
if(ch==";"||ch==")")
{
return 1;
}
for(int j=0; j<sizeof(Operator) / sizeof(Operator[0]); j++)
{
if(Operator[j]==ch)
{
return 1;
}
}
if(ch=="+"||ch=="-")
{
seq.pop();
if(Term())
{
if(Alex_())
{
return 1;
}
}
}
return 0;
}
int Term()//<项>→<项>*<因子>|<项>/<因子>|<因子>
{
if(!seq.size())
return 0;
string ch=seq.front();
for(int i=0; i<Indentifier.size(); i++)
{
if(Indentifier[i]==ch)
{
printf("<项> → <项>*<因子> | <项>/<因子> | <因子>\n");
outgrammerfile <<"<项> → <项>*<因子> | <项>/<因子> | <因子>"<<endl;
if(Factor())
{
if(Term_())
{
return 1;
}
}
}
}
for(int i=0; i<Integer.size(); i++)
{
if(Integer[i]==ch)
{
printf("<项> → <项>*<因子> | <项>/<因子> | <因子>\n");
outgrammerfile <<"<项> → <项>*<因子> | <项>/<因子> | <因子>"<<endl;
if(Factor())
{
if(Term_())
{
return 1;
}
}
}
}
return 0;
}
int Term_()//Term临时变量
{
if(!seq.size())
return 0;
string ch=seq.front();
if(ch==";"||ch=="+"||ch=="-"||ch==")")
{
return 1;
}
for(int j=0; j<sizeof(Operator) / sizeof(Operator[0]); j++)
{
if(Operator[j]==ch)
{
return 1;
}
}
if(ch=="*"||ch=="/")
{
seq.pop();
if(Factor())
{
if(Term_())
{
return 1;
}
}
}
return 0;
}
int Factor()//<因子>→<标识符>|<无符号整数>|(<算数表达式>)
{
if(!seq.size())
return 0;
string ch=seq.front();
seq.pop();
for(int i=0; i<Indentifier.size(); i++)
{
if(Indentifier[i]==ch)//标识符
{
cout<<"<因子> → <标识符> "<<ch<<endl;
outgrammerfile <<"<因子> → <标识符> "<<endl;
return 1;
}
}
for(int i=0; i<Integer.size(); i++)
{
if(Integer[i]==ch)//整数
{
cout<<"<因子> → <无符号整数> "<<ch<<endl;
outgrammerfile <<"<因子> → <无符号整数> "<<endl;
return 1;
}
}
if(ch=="(")
{
if(Alex())
{
if(!seq.size())
return 0;
if(seq.front()==")")
{
seq.pop();
return 1;
}
}
}
return 0;
}
ofstream outfile("E:\\com\\wordCheck.txt", ios::out);//词法分析结果写入文件
void word(string a)//单个字符判断
{
if(a[0]>='0'&&a[0]<='9')
{
printf("\t");
cout<<"( 整数, "<<a<<" )"<<endl;
outfile <<"( 整数, "<<a<<" )"<<endl;
Integer.push_back(a);
}
else
{
int flag=0,j;
for(j=0; j<6; j++)
{
if(a==key[j])
{
flag=1;
printf("\t");
cout<<"( 关键字, "<<a<<" )"<<endl;
outfile <<"( 关键字, "<<a<<" )"<<endl;
}
}
if(flag==0)
{
printf("\t");
cout<<"( 自定义标识符, "<<a<<" )"<<endl;
outfile <<"( 自定义标识符, "<<a<<" )"<<endl;
Indentifier.push_back(a);
}
}
}
string& trim(string &str, string::size_type pos = 0)//去空格和tab
{
static const string delim = " \t"; //删除空格或者tab字符
pos = str.find_first_of(delim, pos);
if (pos == string::npos)
return str;
return trim(str.erase(pos, 1));
}
vector<string> vc;//存储从词法分析中得到的单词们
void Read()//从文件中读取
{
ifstream myfile("E:\\hello.txt");
ofstream outfile("E:\\out.txt", ios::app);
string temp;
if (!myfile.is_open())
{
cout << "未成功打开文件" << endl;
}
while(getline(myfile,temp))
{
outfile << temp;
outfile << endl;
}
myfile.close();
outfile.close();
}
void wordCheck()//词法分析
{
string str,s;
stringstream ss;
printf("选择输入模式:1.文件读取 2.键盘输入\n");
int nn;
scanf("%d",&nn);
if(nn==1)
{
ifstream myfile("E:\\com\\5.txt");
string temp;
if (!myfile.is_open())
{
cout << "未成功打开文件" << endl;
}
while(getline(myfile,temp))
{
if(temp=="#")//逐行读取代码,以#为截至
break;
s+=temp;
}
ss<<s;
str="";
myfile.close();
}
else
{
printf("请输入待分析的代码:(请以#结尾)\n");
while(getline(cin,str))
{
if(str=="#")
break;
s+=str;
}
ss<<s;
str="";
}
while(ss>>str)
{
//seq.push(str);
string a="";
int len=str.length();
int i;
for(i=0; i<len; i++)
{
if(str[i]=='('||str[i]==')'||str[i]=='{'||str[i]=='}'||str[i]==','||str[i]==';')
{
if(a.length())
{
word(a);
seq.push(a);
vc.push_back(a);
}
a="";//清空
printf("\t");
cout<<"( 界符, "<<str[i]<<" )"<<endl;
outfile <<"( 界符, "<<str[i]<<" )"<<endl;
string aa="";
aa+=str[i];
seq.push(aa);
vc.push_back(aa);
}
else if(str[i]=='='||str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/'||str[i]=='>'||str[i]=='<'||str[i]=='!'||str[i]=='&'||str[i]=='|')
{
if(a.length())
{
word(a);
seq.push(a);
vc.push_back(a);
}
a="";//清空
if(str[i+1]=='=')
{
printf("\t");
cout<<"( 运算符, "<<str[i]<<'='<<" )"<<endl;
outfile <<"( 运算符, "<<str[i]<<'='<<" )"<<endl;
seq.push("==");
vc.push_back("==");
i++;
}
else if(str[i+1]=='&')
{
printf("\t");
cout<<"( 运算符, "<<str[i]<<'&'<<" )"<<endl;
outfile <<"( 运算符, "<<str[i]<<'&'<<" )"<<endl;
seq.push("&&");
vc.push_back("&&");
i++;
}
else if(str[i+1]=='|')
{
printf("\t");
cout<<"( 运算符, "<<str[i]<<'|'<<" )"<<endl;
outfile <<"( 运算符, "<<str[i]<<'|'<<" )"<<endl;
seq.push("||");
vc.push_back("||");
i++;
}
else
{
printf("\t");
cout<<"( 运算符, "<<str[i]<<" )"<<endl;
outfile <<"( 运算符, "<<str[i]<<" )"<<endl;
string aa="";
aa+=str[i];
seq.push(aa);
vc.push_back(aa);
}
}
else
{
a=a+str[i];
}
}
if(a.length())
{
word(a);
seq.push(a);
vc.push_back(a);
}
a="";//清空
}
outfile.close();
}
int cnt=100;
ofstream out3file("E:\\com\\translate.txt", ios::out);
void translate()//拉链回填
{
vector<string> v;
string str,s;
stringstream ss;
for(int i=0; i<vc.size(); i++)
{
if(vc[i]=="(")
{
for(int j=i+1; j<vc.size(); j++)
{
if(vc[j]==")")
{
i=j;
break;
}
str=str+vc[j]+" ";
}
}
}
str+=" ed";
ss<<str;
int yes=1,no=100;
while(ss>>s)
{
if(s=="||"||s=="ed")
{
if(s=="||")
no+=2;
else
no=0;
int len=v.size();
for(int i=0; i<len-3; i+=3)
{
printf("\t");
out3file<<cnt<<"(j"<<v[i+1]<<","<<v[i]<<","<<v[i+2]<<","<<cnt+2<<")"<<endl;
printf("%d(j%s,%s,%s,%d)\n",cnt,v[i+1].c_str(),v[i].c_str(),v[i+2].c_str(),cnt+2);
cnt++;
printf("\t");
printf("%d(j,_,_,%d)\n",cnt,no);
out3file<<cnt<<"(j,_,_,"<<no<<")"<<endl;
no=cnt++;
}
printf("\t");
printf("%d(j%s,%s,%s,%d)\n",cnt,v[len-2].c_str(),v[len-3].c_str(),v[len-1].c_str(),yes);
out3file<<cnt<<"(j"<<v[len-2]<<","<<v[len-3]<<","<<v[len-1]<<","<<yes<<")"<<endl;
yes=cnt++;
printf("\t");
printf("%d(j,_,_,%d)\n",cnt,no);
out3file<<cnt<<"(j,_,_,"<<no<<")"<<endl;
cnt++;
v.clear();
}
else if(s=="&&")
no+=2;
else
v.push_back(s);
}
Quater();
}
struct node//DAG中的每个节点信息
{
int l=-1,r=-1;//左右连接
string id;
vector<string> val;//附加标记
} p[101];
int num;//DAG有几个节点
int nn;
string retain[101];//存储要保留的变量名
bool use1(int t,string ch)//判断当前节点在DAG图中是否出现过
{
for(int i=0; i<p[t].val.size(); i++)
{
if(p[t].val[i]==ch)
return true;
}
return false;
}
int add_node(string ch)//增加DAG图中的节点并返回序号
{
for(int i=num-1;i>=0;i--)
{
if(p[i].id==ch||use1(i,ch))
{
p[i].val.push_back(ch);
return i;
}
}
p[num].id=ch;
return num++;
}
void add_op(string op,int l,int r,string ch)//DAG图添加操作符新节点
{
for(int i=num-1;i>=0;i--)
{
if(p[i].id==op&&p[i].l==l&&p[i].r==r)
{
p[i].val.push_back(ch);
return ;
}
}
p[num].id=op;
p[num].l=l;
p[num].r=r;
p[num].val.push_back(ch);
num++;
}
string fun(node t) //在DAG图的节点中找出保留的变量名
{
for(int i=0; i<t.val.size(); i++)
{
for(int j=0; j<nn; j++)
{
if(t.val[i]==retain[j])
return retain[j];
}
}
return t.val[0];
}
int flag2[1001];//标记优化后的剩余的表达式
void dfs(int t) //通过左右支连接递归查找图中符合条件的信息
{
if(p[t].l!=-1||p[t].r!=-1)
{
flag2[t]=1;
dfs(p[t].l);
dfs(p[t].r);
}
}
struct node2//符合输出的表达式存在这里
{
vector<string> ans;//存储各个表达式的string字符
} da[101];
ofstream out4file("E:\\com\\DAG.txt", ios::out);
void DAG()//DAG优化
{
string str;
cout<<"优化前表达式为:"<<endl;
out4file<<"优化前表达式为:"<<endl;
for(int i=0; i<vc.size(); i++)
{
if(vc[i]=="=")
{
cout<<vc[i-1]<<vc[i]<<vc[i+1]<<vc[i+2]<<vc[i+3]<<endl;
out4file<<vc[i-1]<<vc[i]<<vc[i+1]<<vc[i+2]<<vc[i+3]<<endl;
int l=add_node(vc[i+1]);
int r=add_node(vc[i+3]);
add_op(vc[i+2],l,r,vc[i-1]);
if(i+4<vc.size())
i=i+4;
}
}
cout<<endl<<"优化后表达式为:"<<endl;
out4file<<endl<<"优化后表达式为:"<<endl;
for(int i=0; i<num; i++)
{
if(p[i].l!=-1&&p[i].r!=-1)
{
da[i].ans.push_back(fun(p[i]));
//ans[i][0]=fun(p[i]);
//ans[i][1]="=";
da[i].ans.push_back("=");
int l=p[i].l;
int r=p[i].r;
//ans[i][2]=p[l].val.size()>0?fun(p[l]):p[l].id;
if(p[l].val.size()>0)
da[i].ans.push_back(fun(p[l]));
else
da[i].ans.push_back(p[l].id);
//ans[i][3]=p[i].id;
da[i].ans.push_back(p[i].id);
//ans[i][4]=p[r].val.size()>0?fun(p[r]):p[r].id;
if(p[r].val.size()>0)
da[i].ans.push_back(fun(p[r]));
else
da[i].ans.push_back(p[r].id);
//ans[i][5]='\0';
}
}
for(int j=0; j<nn; j++)
{
for(int i=num-1; i>=0; i--)
{
if(da[i].ans[0]==retain[j])
{
dfs(i);
break;
}
}
}
for(int i=0; i<num; i++)
{
if(flag2[i])
{
//printf("\t");
for(int j=0; j<da[i].ans.size(); j++)
{
cout<<da[i].ans[j];
out4file<<da[i].ans[j];
}
cout<<endl;
out4file<<endl;
}
}
}
void Quater()//在拉链回填后面的四元式
{
int temp;
temp=0;
for(int i=0; i<vc.size(); i++)
{
if(vc[i]=="=")
{
if(vc[i+2]==";")
{
printf("\t");
cout<<cnt++<<"("<<vc[i]<<","<<vc[i+1]<<",_,"<<vc[i-1]<<")"<<endl;
out3file<<cnt++<<"("<<vc[i]<<","<<vc[i+1]<<",_,"<<vc[i-1]<<")"<<endl;
i=i+2;
}
else
{
printf("\t");
cout<<cnt++<<"("<<vc[i+2]<<","<<vc[i+1]<<","<<vc[i+3]<<",T"<<temp++<<")"<<endl;
out3file<<cnt++<<"("<<vc[i+2]<<","<<vc[i+1]<<","<<vc[i+3]<<",T"<<temp++<<")"<<endl;
printf("\t");
cout<<cnt++<<"("<<vc[i]<<",T"<<temp-1<<",_,"<<vc[i-1]<<")"<<endl;
out3file<<cnt++<<"("<<vc[i]<<",T"<<temp-1<<",_,"<<vc[i-1]<<")"<<endl;
i+=4;
}
}
}
}
void Quaternion()//前的四元式
{
int temp;
temp=0;
for(int i=0; i<vc.size(); i++)
{
if(vc[i]=="=")
{
if(vc[i+2]==";")
{
printf("\t");
cout<<"("<<vc[i]<<","<<vc[i+1]<<",_,"<<vc[i-1]<<")"<<endl;
out3file<<"("<<vc[i]<<","<<vc[i+1]<<",_,"<<vc[i-1]<<")"<<endl;
i=i+2;
}
else
{
printf("\t");
cout<<"("<<vc[i+2]<<","<<vc[i+1]<<","<<vc[i+3]<<",T"<<temp++<<")"<<endl;
out3file<<"("<<vc[i+2]<<","<<vc[i+1]<<","<<vc[i+3]<<",T"<<temp++<<")"<<endl;
printf("\t");
cout<<"("<<vc[i]<<",T"<<temp-1<<",_,"<<vc[i-1]<<")"<<endl;
out3file<<"("<<vc[i]<<",T"<<temp-1<<",_,"<<vc[i-1]<<")"<<endl;
i+=4;
}
}
}
}
string R[101];//寄存器中的值
int m;//寄存器个数
int top;//寄存器现占用情况
int get(string ch)//寄存器中是否存在变量
{
for(int i = 0; i < m; i++)
{
if(ch == R[i])
return i;
}
return -1;
}
int use(int x, string ch)//从当前式子往后查找 是否使用过
{
for(int i = x; i < num; i++)
{
if(flag2[i])
{
if(ch==da[i].ans[2]||ch==da[i].ans[4])
return i;
}
}
return num;//返回第几个使用的式子
}
int find(int x)//找到可用的寄存器
{
if(top < m)//还有可用的寄存器
return top++;
int t = -1;
int ans = -1;
for(int i = 0; i < m; i++)//遍历找到最后使用的那个寄存器
{
int k = use(x, R[i]);
if(k > ans)
{
ans = k;
t = i;
}
}
return t;//返回寄存器编号
}
ofstream out5file("E:\\com\\generator.txt", ios::out);
void Generator()//目标代码生成
{
for(int i=0; i<num; i++)
{
if(flag2[i])
{
int x=get(da[i].ans[2]);
if(x==-1)
{
x=find(i);
if(R[x]!=""&&use(i,R[x])<num)
{
cout<<"ST R"<<x<<", "<<R[x]<<endl;
out5file<<"ST R"<<x<<", "<<R[x]<<endl;
R[x]="";
}
cout<<"LD R"<<x<<", "<<da[i].ans[2]<<endl;
out5file<<"LD R"<<x<<", "<<da[i].ans[2]<<endl;
}
string op=da[i].ans[3];
if(op=="+")
printf("ADD ");
else if(op=="-")
printf("SUB ");
else if(op=="*")
printf("MUL ");
else
printf("DIV ");
int xx=get(da[i].ans[4]);
if(xx!=-1)
{
cout<<"R"<<x<<", R"<<xx<<endl;
out5file<<"R"<<x<<", R"<<xx<<endl;
}
else
{
cout<<"R"<<x<<", "<<da[i].ans[4]<<endl;
out5file<<"R"<<x<<", "<<da[i].ans[4]<<endl;
}
R[x]=da[i].ans[0];
}
}
}
int main()//主界面
{
printf("\n1.词法分析 2.语法分析 3.语义分析和中间代码生成 4.代码优化 5.目标代码生成\n");
printf("\n**********************词法分析*********************************************\n");
//Read();
wordCheck();
printf("\n**********************语法分析*********************************************\n");
int z=grammerCheck();
outgrammerfile.close();
if(z==1)
cout<<"Success!符合文法"<<endl;
else
cout<<"error!不符合文法"<<endl;
printf("\n**********************语义分析及中间代码生成*****************************************\n");
cout<<"赋值语句生成的四元式为:"<<endl;
out3file<<"赋值语句生成的四元式为:"<<endl;
Quaternion();
cout<<endl;
printf("翻译布尔表达式(拉链-回填)\n");
out3file<<"翻译布尔表达式(拉链-回填"<<endl;
translate();
printf("\n**********************DAG优化******************************************************\n");
printf("请输入想要保留的变量的个数: ");
cin>>nn;
printf("请输入保留的变量名: ");
for(int i=0; i<nn; i++)
{
cin>>retain[i];
}
DAG();
printf("\n**********************简单的代码生成**********************************************\n");
printf("请输入可用寄存器个数: ");
scanf("%d",&m);
Generator();
return 0;
}
可以实现:
main()
{
int a,b,c;
int z;
c=10;
a=(a+b)*c;
z=a/b;
}
#
main()
{
int a;
if(a>10)
{
a=2;
}
}
#
main()
{
int a;
while(a>2)
{
a=a+1;
}
}
#
main()
{
int i,a,b;
for(i=0;i<a;i+=1)
{
b=i;
}
}
#
main()
{
int a,b,c,d,e,f,z;
if(a < b || c < d && e < f)
{
a=1+2;
b=2+3;
c=11+12;
c=e+f;
c=c+12;
e=c+d;
d=e+12;
a=c+d;
b=a+c;
}
}
#