📄 syntax_analyser.cpp
字号:
#include "token.h"
#include "syntaxNode.h"
#include <iostream>
using namespace std;
Token token;
#define In(x) cout<<"In "<<x<<endl;
#define Out(x) cout<<"Out "<<x<<endl;
#define SyntaxError(message) cout<<"\n>>>"<<"Syntax error at line "<<lineNo<<" "<<message;
char* StrCopy(char* str)
{
if(str==NULL) return NULL;
else
{
char *tem=new char[strlen(str)+1];
strcpy(tem,str);
return tem;
}
}
void main(int argc,char *argv[])
{
if(argc<2)
{
cout<<"Open Source file failed!"<<endl;
exit(0);
}
if(!InitScanner(argv[1]))
{
cout<<"此法分析器初始化失败!"<<endl;
exit(1);
}
TreeNode *root=parse();
cout<<"\n***************************输出语法树****************************************\n"<<endl;
parseTree(root);
cout<<"\n**************************语法分析完毕****************************************\n"<<endl;
}
void Match(TokenType expected)
{
if(token.type!=ENDOFFILE)
{
// cout<<"MatchToken: ( "<<token.type<<" , "<<token.lex<<")"<<endl;
if(token.type==expected)
token=GetToken();
else
{
SyntaxError("MatchToken Unexpected Token:");
PrintToken(token);
}
}
}
TreeNode *StmtNode(StmtKind kind)
{
TreeNode* temp=(TreeNode*) malloc(sizeof(TreeNode));
if(temp==NULL)
{
cout<<"TreeNode construction failed!"<<endl;
exit(0);
}
else
{
for(int i=0;i<MAX_CHILD;i++)
temp->child[i]=NULL;
temp->sibling=NULL;
temp->nodeKind=StmtK;
temp->kind.stmt=kind;
temp->lineno=lineNo;
}
return temp;
}
TreeNode *ExpNode(ExpKind kind)
{
TreeNode* temp=(TreeNode*) malloc(sizeof(TreeNode));
if(temp==NULL)
{
cout<<"TreeNode construction failed!"<<endl;
exit(0);
}
else
{
for(int i=0;i<MAX_CHILD;i++)
temp->child[i]=NULL;
temp->sibling=NULL;
temp->nodeKind=ExpK;
temp->kind.exp=kind;
temp->lineno=lineNo;
}
return temp;
}
static int indent=1;
void parseTree(TreeNode *root)
{
indent+=1;
for(int i=0;i<indent;i++)
cout<<" ";
while(root!=NULL)
{
if(root->nodeKind==StmtK)
{
switch(root->kind.stmt)
{
case IfK:
cout<<"If "<<endl;
break;
case RepeatK:
cout<<"Repeat "<<endl;
break;
case AssignK:
cout<<"Assign to ";
if(root->attr.name)cout<<"NULL"<<endl;
cout<<root->attr.name<<endl;
break;
case ReadK:
cout<<"Read ";
cout<<root->attr.name<<endl;
break;
case WriteK:
cout<<"Write "<<endl;
break;
default:
cout<<"Unknown StmtNodeKind!"<<endl;
break;
}
}
else if(root->nodeKind==ExpK)
{
switch(root->kind.exp)
{
case OpK:
cout<<"\t\tOp:";
cout<<root->attr.name<<endl;
break;
case ConstK:
cout<<"Const:";
cout<<root->attr.val<<endl;
break;
case IdK:
cout<<"Id:";
cout<<root->attr.name<<endl;
break;
default:
cout<<"Unkown ExpNode!"<<endl;
break;
}
}
else
{
cout<<"Unkown NodeKind!"<<endl;
}
for(int i=0;i<MAX_CHILD;i++)
parseTree(root->child[i]);
root=root->sibling;
}
indent-=1;
}
TreeNode *parse()
{
cout<<"\n***************************语法分析开始**************************************\n"<<endl;
cout<<"\t以下打印语法分析的路径:\n"<<endl;
TreeNode *temp;
token=GetToken();
temp=stmt_sequence();
if(token.type!=ENDOFFILE)
SyntaxError("Syntax analysed failed!");
return temp;
}
TreeNode* stmt_sequence()
{
In("stmt_sequence");
TreeNode *temp=statement();
TreeNode *tem=temp;
if((token.type!=ENDOFFILE)&&(token.type!=END)&&(token.type!=ELSE)&&(token.type!=UNTIL))
{
TreeNode *p;
Match(SEMICOLON);
p=statement();
if(p!=NULL)
{
if(temp==NULL)temp=tem=p;
else
{
tem->sibling=p;
tem=p;
}
}
}
Out("stmt_sequence");
return temp;
}
TreeNode* statement()
{
In("statement");
TreeNode *temp=NULL;
switch(token.type)
{
case IF:
temp=if_stmt();
break;
case REPEAT:
temp=repeat_stmt();
break;
case ID:
temp=assign_stmt();
break;
case READ:
temp=read_stmt();
break;
case WRITE:
temp=write_stmt();
break;
default:
SyntaxError("Statement Unexpected token:");
PrintToken(token);
token=GetToken();
break;
}
Out("statement");
return temp;
}
TreeNode* if_stmt()
{
In("if_stmt");
TreeNode *temp =StmtNode(IfK);
Match(IF);
if(temp!=NULL)
temp->child[0]=exp();
Match(THEN);
if(temp!=NULL)
temp->child[1]=stmt_sequence();
if(token.type==ELSE)
{
Match(ELSE);
if(temp!=NULL)
temp->child[2]=stmt_sequence();
}
Match(END);
Out("if_stmt");
return temp;
}
TreeNode* repeat_stmt()
{
In("repeat_stmt");
TreeNode *temp =StmtNode(RepeatK);
Match(REPEAT);
if(temp!=NULL)
temp->child[0]=stmt_sequence();
Match(UNTIL);
if(temp!=NULL)
temp->child[1]=exp();
Out("repeat_stmt");
return temp;
}
TreeNode* assign_stmt()
{
In("assign_stmt");
TreeNode *temp =StmtNode(AssignK);
if(temp!=NULL&&token.type==ID)
temp->attr.name=StrCopy(token.lex);
Match(ID);
Match(ASSIGN);
if(temp!=NULL)
temp->child[0]=exp();
Out("assign_stmt ");
return temp;
}
TreeNode* read_stmt()
{
In("read_stmt");
TreeNode *temp = StmtNode(ReadK);
Match(READ);
if((temp!=NULL)&&(token.type==ID))
{
temp->attr.name=StrCopy(token.lex);
}
Match(ID);
Out("read_stmt");
return temp;
}
TreeNode* write_stmt()
{
In("write_stmt");
TreeNode *temp =StmtNode(WriteK);
Match(WRITE);
if(temp!=NULL)
temp->child[0]=exp();
Out("write_stmt ");
return temp;
}
TreeNode* exp()
{
In("exp_stmt");
TreeNode *temp =simple_exp();
while((token.type==LESS)||(token.type==EQUAL))
{
TreeNode *p =ExpNode(OpK);
if(p!=NULL)
{
p->child[0]=temp;
p->attr.name=StrCopy(token.lex);
temp=p;
}
Match(token.type);
if(temp!=NULL)
temp->child[1]=simple_exp();
}
Out("exp_stmt");
return temp;
}
TreeNode* simple_exp()
{
In("simple_exp_stmt");
TreeNode *temp=term();
while((token.type==ADD)||(token.type==SUB))
{
TreeNode *p=ExpNode(OpK);
if(p!=NULL)
{
p->child[0]=temp;
p->attr.name=StrCopy(token.lex);
cout<<"Op"<<p->attr.name<<endl;
temp=p;
Match(token.type);
temp->child[1]=term();
}
}
Out("asimple_exp_stmt ");
return temp;
}
TreeNode* term()
{
In("trem-stmt");
TreeNode *temp=factor();
while((token.type==MUL)||(token.type==DIV))
{
TreeNode *p=ExpNode(OpK);
if(p!=NULL)
{
p->child[0]=temp;
p->attr.name=StrCopy(token.lex);
temp=p;
Match(token.type);
p->child[1]=factor();
}
}
Out("trem_stmt");
return temp;
}
TreeNode* factor()
{
In("factor_stmt");
TreeNode *temp=NULL;
switch(token.type)
{
case NUM:
temp=ExpNode(ConstK);
if(temp!=NULL&&token.type==NUM)
temp->attr.val=atoi(token.lex);
Match(NUM);
break;
case ID:
temp=ExpNode(IdK);
if(temp!=NULL&&token.type==ID)
temp->attr.name=token.lex;
Match(ID);
break;
case L_BRACKET:
Match(L_BRACKET);
temp=exp();
Match(R_BRACKET);
break;
default:
SyntaxError("Factor Unexpected token->");
cout<<token.lex<<endl;
token=GetToken();
break;
}
Out("factor_stmt");
return temp;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -