📄 compile3.cpp
字号:
table_var* temp2=tableVar[index];
while(temp2!=NULL)
{
if(temp2->name=="constant")
break;
else
temp2=temp2->next;
}
if(temp2==NULL)
{
temp2=new table_var();
tableVar[index]=temp2;
}
temp2->name="constant";
temp2->type="int";
}
/*68constant true*/
void constant_true()
{
int index=hash_funct("constant");
table_var* temp2=tableVar[index];
while(temp2!=NULL)
{
if(temp2->name=="constant")
break;
else
temp2=temp2->next;
}
if(temp2==NULL)
{
temp2=new table_var();
tableVar[index]=temp2;
}
temp2->name="constant";
temp2->type="bool";
}
/*69constant false*/
void constant_false()
{
constant_true();
}
/*70call ID ( args )*/
void callID(string name)
{
int index=hash_funct("call");
table_var* temp2=tableVar[index];
while(temp2!=NULL)
{
if(temp2->name=="call")
break;
else
temp2=temp2->next;
}
if(temp2==NULL)
{
temp2=new table_var();
tableVar[index]=temp2;
}
temp2->name="call";
//查找ID
int index2=hash_funct(name);
table_funct* temp1=tableFunct[index2];
while(temp1!=NULL)
{
if(temp1->name==name)
break;
else
temp1=temp1->next;
}
if(temp1==NULL)
p_error("无法查找到"+name);
temp2->type=temp1->re_type;
//查找args
int index3=hash_funct("args");
table_funct* temp3=tableFunct[index3];
while(temp3!=NULL)
{
if(temp3->name==name)
break;
else
temp3=temp3->next;
}
if(temp3==NULL)
p_error("无法查找到args");
//比较参数列表
if(temp1->size_param!=temp3->size_param)
p_error("所调用函数参数不匹配");
else
{
for(int i=0;i<temp1->size_param;i++)
{
if(temp1->param_type[i]!=temp3->param_type[i])
{
p_error("参数不匹配!");
break;
}
}
}
}
/*71args arg-list*/
void args()
{
//bool b=true;
int index=hash_funct("args");
table_funct* temptable=tableFunct[index];
if(temptable!=NULL )
{
while(temptable!=NULL)
{
if(temptable->name=="args")
{
//b=false;
break;
}
else
temptable=temptable->next;
}
}
if(temptable==NULL)
{
temptable=new table_funct();
tableFunct[index]=temptable;
}
//查找
int index2=hash_funct("arg-list");
table_var* temp1=tableVar[index2];
while(temp1!=NULL)
{
if(temp1->name=="arg-list")
break;
else
temp1=temp1->next;
}
if(temp1==NULL)
p_error("无法查找到arg-list");
temptable->size_param=tableFunct[hash_funct("arg-list")]->size_param;
for(int i=0;i<temptable->size_param;i++)
{
temptable->param_type.push_back(tableFunct[hash_funct("arg-list")]->param_type[i]);
}
}
/*72args #*/
void args_void()
{
int index=hash_funct("args");
table_funct* temptable=tableFunct[index];
if(temptable!=NULL )
{
while(temptable!=NULL)
{
if(temptable->name=="args")
{
//b=false;
break;
}
else
temptable=temptable->next;
}
}
if(temptable==NULL)
{
temptable=new table_funct();
tableFunct[index]=temptable;
}
temptable->size_param=0;
temptable->param_type.push_back("void");
}
/*73arg-list arg-list , expression*/
void arglists_expression()
{
//查找arg-list
int index3=hash_funct("arg-list");
table_funct* temp3=tableFunct[index3];
while(temp3!=NULL)
{
if(temp3->name=="arg-list")
break;
else
temp3=temp3->next;
}
if(temp3==NULL)
p_error("无法查找到arg-list");
//查找expression
int index=hash_funct("expression");
table_var* temp=tableVar[index];
while(temp!=NULL)
{
if(temp->name=="expression")
break;
else
temp=temp->next;
}
if(temp==NULL)
p_error("无法查找到expression");
//将expression中的参数类型传递给arg-list
temp3->param_type.push_back(temp->type);
}
/*74arg-list expression*/
void arglist_expression()
{
//建立arglist
int index=hash_funct("arglist");
table_funct* temp2=tableFunct[index];
while(temp2!=NULL)
{
if(temp2->name=="arglist")
break;
else
temp2=temp2->next;
}
if(temp2==NULL)
{
temp2=new table_funct();
tableFunct[index]=temp2;
}
//查找expression
int index4=hash_funct("expression");
table_var* temp=tableVar[index4];
while(temp!=NULL)
{
if(temp->name=="expression")
break;
else
temp=temp->next;
}
if(temp==NULL)
p_error("无法查找到expression");
//arg-list的参数个数加一
temp2->size_param++;
//将expression的类型添加到arg-list的参数类型 表中
temp2->param_type.push_back(temp->type);
}
void sematic(int num_grammar,string name,string num_s)
{
int num;
num_grammar+=1;
switch(num_grammar)
{
case 1:break;
case 2:break;
case 3:break;
case 4:break;
case 5:break;
case 6:typeID(name);break;
case 7: num=atoi(num_s.c_str());
typeArray( name,num);break;
case 8:typeInt();break;
case 9: typeVoid();break;
case 10:typeBool();break;
case 11:fundecl(name);break;
case 12:paramlist();break;
case 13: paramvoid();break;
case 14:paramlist_s();break;
case 15:plist_param();break;
case 16:paramID(name);break;
case 17:paramArray(name);break;
case 18:compound_stmt();break;
case 19:break;
case 20:break;
case 21:statement_list();break;
case 22:break;
case 23:statement_expression();break;
case 24:break;
case 25:statement_select();break;
case 26:statement_iteration();break;
case 27:statement_return();break;
case 28:expressionstmt_expression();break;
case 29:expressionstmt();break;
case 30:selectstmt_if();break;
case 31:selectstmt_else();break;
case 32:iterationstmt();break;
case 33:returnstmt();break;
case 34:returnstmt_expression();break;
case 35:expressionvar();break;
case 36:simpleexpression();break;
case 37:varID(name);break;
case 38:varArray(name);break;
case 39:simple_relop();break;
case 40:simple_add();break;
case 41:break;
case 42:break;
case 43:break;
case 44:break;
case 45:break;
case 46:break;
case 47:additive_expression();break;
case 48:additive_term();break;
case 49:break;
case 50:break;
case 51:break;
case 52:break;
case 53:term_mulop();break;
case 54:term_unarry();break;
case 55:break;
case 56:break;
case 57:break;
case 58:break;
case 59:unaryexpression();break;
case 60: unary_factor();break;
case 61:unaryop_fist();break;
case 62:unaryop_secd();break;
case 63:factor_expression();break;
case 64:factor_var();break;
case 65:factor_call();break;
case 66:factor_constant();break;
case 67:constant_num();break;
case 68:constant_true();break;
case 69:constant_false();break;
case 70:callID(name);break;
case 71:args();break;
case 72:args_void();break;
case 73:arglists_expression();break;
case 74:arglist_expression();break;
default: perror("规约出错");
}
}
//-------------------------------------------------------------------------------------
int main()
{
sematic_init();//符号表初始化
ifstream infile;
infile.open("system\\action_goto_tables.txt");
//读ACTION和GOTO表
vector<string> tToString;
vector<string> ntToString;
string line="";
getline(infile,line);//读第一行的终极符号
string temp="";
for(int i=0;i<line.size();i++)
{
if(line[i]==' ')
{
tToString.push_back(temp);
temp="";
continue;
}
temp.push_back(line[i]);
}
getline(infile,line);//读第二行的非终极符号
temp="";
for(int i=0;i<line.size();i++)
{
if(line[i]==' ')
{
ntToString.push_back(temp);
temp="";
continue;
}
temp.push_back(line[i]);
}
getline(infile,line);//读第三行的状态数
int state_count=atoi(line.c_str());
int tCount=tToString.size();
int ntCount=ntToString.size();
int **action_table=new int*[state_count];
for(int i=0;i<state_count;i++)
{//读ACTION表,写入本地action_table中
action_table[i]=new int[tCount];
getline(infile,line);
temp="";
int thenum=0;
for(int j=0;j<line.size();j++)
{
if(line[j]==' ')
{
int num=atoi(temp.c_str());
action_table[i][thenum++]=num;
temp="";
}
else temp.push_back(line[j]);
}
}
int **goto_table=new int*[state_count];
for(int i=0;i<state_count;i++)
{//读GOTO表,写入本地goto_table中
goto_table[i]=new int[ntCount];
getline(infile,line);
temp="";
int thenum=0;
for(int j=0;j<line.size();j++)
{
if(line[j]==' ')
{
int num=atoi(temp.c_str());
goto_table[i][thenum++]=num;
temp="";
}
else temp.push_back(line[j]);
}
}
/*测试一下
for(int i=0;i<state_count;i++)
{
for(int j=0;j<tCount;j++)
cout << action_table[i][j] << " ";
cout << endl;
}
for(int i=0;i<state_count;i++)
{
for(int j=0;j<ntCount;j++)
cout << goto_table[i][j] << " ";
cout << endl;
}
*/
//以上就是读action_goto_tables文件,把yacc相应的输出数据读进来用做语法分析
map<string,int> tToInt;
map<string,int> ntToInt;//这两个是查找符号名对应的下标
for(int i=0;i<tCount;i++)
tToInt[tToString[i]]=i;
for(int i=0;i<ntCount;i++)
ntToInt[ntToString[i]]=i;
int eposition=tToInt.find("#")->second;
//读grammar.txt文法文件,输入到pro中
ifstream grammarfile;
grammarfile.open("system\\grammar.txt");
if(!infile)
{//打开文件失败就退出
printf("Unable to open the LexText file.\n");
return -1;
}
vector<string> grammar1;//原始文法
string input="";
while(getline(grammarfile,input))
{
//输入串末尾加个空格,分词的时候容易些
input.push_back(' ');
grammar1.push_back(input);
}
//建立产生式的存储结构,我用vector<string>[]数组来存
vector<string> *production=new vector<string>[grammar1.size()];
for(int i=0;i<grammar1.size();i++)
{
input="";
//把原始文法分词后写入到产生式数组中
for(int j=0;j<grammar1[i].size();j++)
{
if(grammar1[i][j]!=' ') input.push_back(grammar1[i][j]);
else
{
production[i].push_back(input);
input="";
}
}
}
int productionCount=grammar1.size();
vector<pair<int,bool> > *pro=new vector<pair<int,bool> >[productionCount];
//把原始的文法变成可以很容易看出是否为非终极符和终极符的结构,pair的second为1时为非终极符,为0时为终极符
//pair的first为对应输入串的下标编号
for(int i=0;i<productionCount;i++)
{
for(int j=0;j<production[i].size();j++)
{
bool isNT=true;
int num=-1;
input=production[i][j];
if(ntToInt.find(input)!=ntToInt.end())
{
num=ntToInt.find(input)->second;
isNT=true;
}
else
{
num=tToInt.find(input)->second;
isNT=false;
}
pro[i].push_back(make_pair(num,isNT));
}
}
/*//用来测试转换后的产生式是否正确
for(int i=0;i<productionCount;i++)
{
for(int j=0;j<pro[i].size();j++)
{
if(pro[i][j].second) cout << ntToString[pro[i][j].first] << " " << pro[i][j].second << "\t";
else cout << tToString[pro[i][j].first] << " " << pro[i][j].second << "\t";
}
cout << endl;
}*/
//-----------------------------------------------------------------------------------
ifstream tokenfile;
tokenfile.open("system\\tokens.txt");
input="";
bool isfirst=true;//由于一个token由两部分组成,这个变量控制当前处理的是哪部分
string kind="";//用来表示token类别
vector<pair<string,string> > tokens;//表示输入进来的token序列,first表示大类,用来做语法分析,second表示小类,在分析树的建立时可能用到
while(tokenfile >> input)
{//把tokens存放到vector<pair<string,string>> tokens结构中
if(isfirst)
{
if(input=="ID") kind="ID";
else if(input=="NUM") kind="NUM";
else kind="";
isfirst=false;
}
else
{
if(kind=="ID") tokens.push_back(make_pair(kind,input));
else if(kind=="NUM") tokens.push_back(make_pair(kind,input));
else tokens.push_back(make_pair(input,input));
isfirst=true;
}
}
tokens.push_back(make_pair("$","$"));//把
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -