📄 parser.cpp
字号:
struct stmtNode *var() ////////////////////////////////
{
struct stmtNode *node=0;
struct Node token,token1;
node=(struct stmtNode *) malloc(sizeof(struct stmtNode));
token1=token=GetToken();
if(token.type==ID)
{
int i=0;
while(token.line[i])
{
node->line[i]=token.line[i];
i++;
}node->line[i]='\0';
strcpy(node->modifier,"$");
strcpy(node->nodetype,"var");
strcpy(node->retype,"$");
strcpy(node->vartype,"$");
strcpy(node->nodestring,token1.string);
node->next[1]=node->next[2]=0;
node->link=0;
token=GetToken();
if(token.type==18)
{
node->isArray='1';
node->next[0]=simple_expression();
token=GetToken();
if(token.type==19)
return node;
else
{
error(token);
return 0;
}
}
else
{
node->isArray='0';
node->next[0]=0;
BackGet();
return node;
}
}
else
{
error(token);
return 0;
}
}
struct stmtNode *return_stmt() ////////////////////////////////////
{
struct stmtNode *node=0;
struct Node token;
node=(struct stmtNode *) malloc(sizeof(struct stmtNode));
token=GetToken();
if(token.type==38)
{
node->isArray='$';
int i=0;
while(token.line[i])
{
node->line[i]=token.line[i];
i++;
}node->line[i]='\0';
strcpy(node->modifier,"$");
strcpy(node->nodestring,"$");
strcpy(node->nodetype,"return-stmt");
strcpy(node->retype,"$");
strcpy(node->vartype,"$");
node->next[1]=node->next[2]=0;
node->next[0]=simple_expression();
node->link=0;
token=GetToken();
if(token.type==11)
return node;
else error(token);
}
else error(token);
return 0;
}
struct stmtNode *statement() ;
struct stmtNode *iteration_stmt() /////////////////////////////////
{
struct stmtNode *node=0;
struct Node token;
node=(struct stmtNode *) malloc(sizeof(struct stmtNode));
token=GetToken();
if(token.type==39)
{
node->isArray='$';
int i=0;
while(token.line[i])
{
node->line[i]=token.line[i];
i++;
}node->line[i]='\0';
strcpy(node->modifier,"$");
strcpy(node->nodestring,"$");
strcpy(node->nodetype,"while-stmt");
strcpy(node->retype,"$");
strcpy(node->vartype,"$");
token=GetToken();
if(token.type==16)
{
node->next[0]=simple_expression();
token=GetToken();
if(token.type==17)
{
node->next[1]=statement();
node->next[2]=0;
node->link=0;
return node;
}
else error(token);
}
else error(token);
}
else error(token);
return 0;
}
struct stmtNode *selection_stmt() ////////////////////////////////
{
struct stmtNode *node=0;
struct Node token;
node=(struct stmtNode *) malloc(sizeof(struct stmtNode));
token=GetToken();
if(token.type==40)
{
node->isArray='$';
int i=0;
while(token.line[i])
{
node->line[i]=token.line[i];
i++;
}node->line[i]='\0';
strcpy(node->modifier,"$");
strcpy(node->nodestring,"$");
strcpy(node->nodetype,"if-else-stmt");
strcpy(node->retype,"$");
strcpy(node->vartype,"$");
token=GetToken();
if(token.type==16)
{
node->next[0]=simple_expression();
token=GetToken();
if(token.type==17)
{
node->next[1]=statement();
token=GetToken();
if(token.type==41)
{
node->next[2]=statement();
node->link=0;
return node;
}
else
{
BackGet();
node->next[2]=0;
node->link=0;
return node;
}
}
else error(token);
}
else error(token);
}
else error(token);
return 0;
}
struct stmtNode *expression_stmt() ///////////////////////
{
struct stmtNode *node;
struct Node token;
node=(struct stmtNode *) malloc(sizeof(struct stmtNode));
node=expression();
token=GetToken();
if(token.type==11)
return node;
else error(token);
return 0;
}
struct stmtNode *compund_stmt();
struct stmtNode *statement() //////////////////////////
{
struct stmtNode *node1=0;
struct stmtNode *node2=0;
struct Node token;
node1=(struct stmtNode *) malloc(sizeof(struct stmtNode));
node2=(struct stmtNode *) malloc(sizeof(struct stmtNode));
token=GetToken();
if(token.type==ID)
{
BackGet();
node2=expression_stmt();
}
else if(token.type==38)
{
BackGet();
node2=return_stmt();
}
else if(token.type==39)
{
BackGet();
node2=iteration_stmt();
}
else if(token.type==40)
{
BackGet();
node2=selection_stmt();
}
else if(token.type==20)
{
BackGet();
node2=compund_stmt();
}
else
{
error(token);
return 0;
}
// node1->isArray='$';
// int i=0;
// while(token.line[i])
// {
// node1->line[i]=token.line[i];
// i++;
// }node1->line[i]='\0';
// node1->modifier="$";
/// node1->nodestring="$";
// node1->nodetype="statement-stmt";
/// node1->retype="$";
// node1->vartype="$";
// node1->next[0]=node2;
// node1->next[1]=node1->next[2]=0;
return node2;
}
struct stmtNode *statement_list() ///////////////////////
{
struct stmtNode *node=0;
struct Node token;
node=(struct stmtNode *) malloc(sizeof(struct stmtNode));
token=GetToken();
if(token.type==ID || token.type==38 || token.type==39 || token.type==40)
{
BackGet();
node=statement();
if(node) node->link=statement_list();
}
else
{
BackGet();
node=0;
}
return node;
}
struct stmtNode *local_declaration() //////////////////////
{
struct stmtNode *node=0;
struct Node token;
node=(struct stmtNode *) malloc(sizeof(struct stmtNode));
token=GetToken();
if(token.type==42)
{
BackGet();
node=var_declaration();
if(node) node->link=local_declaration();
}
else
{
BackGet();
node=0;
}
return node;
}
struct stmtNode *exter_declaration() //////////////////////
{
struct stmtNode *node=0;
struct Node token,token1,token2;
node=(struct stmtNode *) malloc(sizeof(struct stmtNode));
token=GetToken();
token1=GetToken();
token2=GetToken();
if(token.type==42)
{
if(token1.type==ID)
{
if(token2.type!=16)
{
BackGet();
BackGet();
BackGet();
node=var_declaration();
if(node) node->link=exter_declaration();
}
else
{
BackGet(); BackGet(); BackGet();
node=0;
}
}
else
{
BackGet(); BackGet(); BackGet();
node=0;
}
}
else
{
BackGet(); BackGet(); BackGet();
node=0;
}
return node;
}
struct stmtNode *compund_stmt() ///////////////////////////////
{
struct stmtNode *node=0;
struct Node token;
node=(struct stmtNode *) malloc(sizeof(struct stmtNode));
token=GetToken();
if(token.type==20)
{
node->isArray='$';
int i=0;
while(token.line[i])
{
node->line[i]=token.line[i];
i++;
}node->line[i]='\0';
strcpy(node->modifier,"$");
strcpy(node->nodestring,"$");
strcpy(node->nodetype,"compund-stmt");
strcpy(node->retype,"$");
strcpy(node->vartype,"$");
node->next[0]=local_declaration();
node->next[1]=statement_list();
node->next[2]=0;
node->link=0;
token=GetToken();
if(token.type==21)
return node;
else error(token);
}
else error(token);
return 0;
}
struct stmtNode *args() /////////////////////////////
{
struct stmtNode *node=0;
struct Node token;
node=(struct stmtNode *) malloc(sizeof(struct stmtNode));
node->isArray='$';
int i=0;
token=GetToken();
while(token.line[i])
{
node->line[i]=token.line[i];
i++;
}node->line[i]='\0';
strcpy(node->modifier,"$");
strcpy(node->nodestring,"$");
strcpy(node->nodetype,"args-of-func");
strcpy(node->retype,"$");
strcpy(node->vartype,"$");
BackGet();
node->next[0]=simple_expression();
token=GetToken();
if(token.type==12)
{
node->link=args();
}
else
{
node->link=0;
BackGet();
}
node->next[1]=node->next[2]=0;
return node;
}
struct stmtNode *call() //////////////////////////////
{
struct stmtNode *node=0;
struct Node token,token1;
node=(struct stmtNode *) malloc(sizeof(struct stmtNode));
token1=token=GetToken();
if(token.type==ID)
{
node->isArray='$';
int i=0;
while(token.line[i])
{
node->line[i]=token.line[i];
i++;
}node->line[i]='\0';
strcpy(node->modifier,"$");
strcpy(node->nodestring,"$");
strcpy(node->nodetype,"call-func");
strcpy(node->retype,"$");
strcpy(node->vartype,"$");
token=GetToken();
if(token.type==16)
{
node->next[0]=args();
node->next[1]=node->next[2]=0;
node->link=0;
token=GetToken();
if(token.type==17)
return node;
else error(token);
}
else error(token);
}
else error(token);
return 0;
}
struct stmtNode *factor() ////////////////////////////////
{
struct stmtNode *node=0;
struct Node token;
node=(struct stmtNode *) malloc(sizeof(struct stmtNode));
struct Node token1;
token=GetToken();
if(token.type==16)
{
node=simple_expression();
token=GetToken();
if(token.type==17)
return node;
else error(token);
}
else if(token.type==NUMBER)
{
node->isArray='$';
int i=0;
while(token.line[i])
{
node->line[i]=token.line[i];
i++;
}node->line[i]='\0';
strcpy(node->modifier,"$");
strcpy(node->nodestring,token.string);
strcpy(node->nodetype,"factor-num");
strcpy(node->retype,"$");
strcpy(node->vartype,"$");
node->next[0]=node->next[1]=node->next[2]=0;
node->link=0;
return node;
}
else if(token.type==ID)
{
token1=token;
token=GetToken();
if(token.type==16)
{
BackGet();
BackGet();
node=call();
}
else
{
BackGet();
BackGet();
node=var();
}
return node;
}
else error(token);
return 0;
}
void tape(int step)
{
while(step)
{
out<<" ";
step--;
}
}
int step=1;
void writexml_tree(struct stmtNode *node) /////////////////////
{
if(node!=0)
{
tape(step);
out<<"<tree line=\""<<node->line;
if(node->nodetype[0] !='$')
out<<"\" nodetype=\""<<node->nodetype;
if(node->nodestring[0] != '$')
out<<"\" nodestring=\""<<node->nodestring;
if(node->vartype[0] !='$')
out<<"\" vartype=\""<<node->vartype;
if(node->retype[0] !='$')
out<<"\" retype=\""<<node->retype;
if(node->modifier[0] !='$')
out<<"\" modifier=\""<<node->modifier;
if(node->isArray !='$')
out<<"\" isArray=\""<<node->isArray;
out<<"\" />\n";
step++;
if(node->next[0]!=0)
{
tape(step);
out<<"<child>\n";
step++;
writexml_tree(node->next[0]);
step--;
tape(step);
out<<"</child>\n";
}
if(node->next[1]!=0)
{
tape(step);
out<<"<child>\n";
step++;
writexml_tree(node->next[1]);
step--;
tape(step);
out<<"</child>\n";
}
if(node->next[2]!=0)
{
tape(step);
out<<"<child>\n";
step++;
writexml_tree(node->next[2]);
step--;
tape(step);
out<<"</child>\n";
}
step--;
if(node->next[0]!=0 || node->next[1]!=0 || node->next[2]!=0)
{
tape(step);
out<<"</tree>\n";
}
if(node->link!=0)
writexml_tree(node->link);
}
}
void main()
{
char fileName1[20],fileName2[20];
char head[] = "<?xml version=\"1.0\"?>\n<root>\n";
char tail[] = "</root>";
char ch;
struct stmtNode *node=0;
gets(fileName1);
// strcpy(fileName1,"G:\\example01");
strcpy(fileName2,fileName1);
in.open (strcat(fileName1,".xml"),ios::nocreate);
if(!in)
{
cout<<"can't find file!!"<<endl;
return;
}
out.open(strcat(fileName2,"_syntaxTree.xml"));
in.get(ch);
while(ch!='t')
in.get(ch);
in.get(ch);
out<<head;
node=program();
writexml_tree(node);
out<<tail;
in.close ();
out.close ();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -