?? parse.cpp
字號:
/****************************************************/
/* File: parse.c */
/* Main program for c- compiler */
/* Compiler Construction: Principles and Practice */
/* Maker:Liukai */
/****************************************************/
#include"globals.h"
#include"util.h"
#include"scan.h"
#include"parse.h"
static TokenType token ;
static TreeNode * mulop(void);
static TreeNode * addop(void);
static TreeNode * empty(void);
static TreeNode * program(void);
static TreeNode * declaration_list(void);
static TreeNode * declaration(void);
static TreeNode * var_declaration(void);
static TreeNode * type_specifier(void);
static TreeNode * fun_declaration(void);
static TreeNode * params(void);
static TreeNode * para_list(void);
static TreeNode * param(void);
static TreeNode * compound_stmt(void);
static TreeNode * local_declaration(void);
static TreeNode * statement_list(void);
static TreeNode * statement(void);
static TreeNode * expression_stmt(void);
static TreeNode * iteration_stmt(void);
static TreeNode * return_stmt(void);
static TreeNode * expression(void);
static TreeNode * selection_stmt(void);
static TreeNode * var(void);
static TreeNode * simple_expression(void);
static TreeNode * relop(void);
static TreeNode * additive_expression(void);
static TreeNode * term(void);
static TreeNode * factor(void);
static TreeNode * call(void);
static TreeNode * args(void);
static TreeNode * arg_list(void);
static void syntaxError(char * );
static void match(TokenType);
/*1*/
TreeNode *program(void)
{
TreeNode *t;
t=declaration_list();
return t;
}
/*2*/
TreeNode *declaration_list(void)
{
TreeNode *t=declaration();
TreeNode *p=t;
while(token==INT||token==VOID)
{
TreeNode *q;
q=declaration();
if (q!=NULL)
{
if (t==NULL)
{
t=p=q;
}
else
{
p->sibling=q;
p=q;
}
}
}
return t;
}
/*3*/
TreeNode *declaration(void)
{
TreeNode *t=newExpNode(IdK);
//加入IDK屬性;
if (token==INT)
{
match(INT);
//加入INT類型屬性;
t->type=Int;//
}
else
{
match(VOID);
//加入VOID類型屬性;
t->type=Void;
}
if (token==ID)
{
//加入TOKENSTRING屬性;
t->attr.name=copyString(tokenString);
}
match(ID);
if (token==LPAREN)
{
//加入FUN屬性;
t->idtype1=FunK;
match(LPAREN);
t->child[0]=params();
match(RPAREN);
t->child[1]=compound_stmt();
}
else
{
//添加VAR屬性;
t->idtype1=VarK;
if (token==SEMI)
{
//說明沒有數組屬性;
t->is=no;
}
else
{
match(ZLPAREN);
if (token==NUM)
{
//說明有數組屬性;
t->is=yes;
//添加NUM屬性;數組中元素個數;
t->arryno=atoi(tokenString);
}
match(NUM);
match(ZRPAREN);
}
match(SEMI);
}
/*if()
t=var_declaration();
else
t=fun_declaration();*/
return t;
}
/*4*/
TreeNode *var_declaration(void)
{
TreeNode *t=newExpNode(IdK);
//添加ID屬性;
//添加VAR屬性;
t->idtype1=VarK;
if (token==INT)
{
match(INT);
//添加INT屬性;
t->type=Int;
}
else
{
match(VOID);
//添加VOID屬性;
t->type=Void;
}
if (token==ID)
{
//添加TOKENSTRING屬性;
t->attr.name=copyString(tokenString);
}
match(ID);
if (token==SEMI)
{
//說明沒有數組屬性;
t->is=no;
}
else
{
match(ZLPAREN);
if (token==NUM)
{
//說明有數組屬性;
t->is=yes;
//添加NUM屬性;
t->arryno=atoi(tokenString);
}
match(NUM);
match(ZRPAREN);
}
match(SEMI);
return t;
}
/*5*/
TreeNode *fun_declaration(void)
{
TreeNode *t=newExpNode(IdK);
//添加ID屬性;
//添加FUM屬性;
t->idtype1=FunK;
if(token==INT)
{
match(INT);
t->type=Int;
}
else
{
match(VOID);
t->type=Void;
}
if (token==ID)
{
// 添加TOKENSTRING屬性;
t->attr.name=copyString(tokenString);//
}
match(ID);
match(LPAREN);
t->child[0]=params();
match(RPAREN);
t->child[1]=compound_stmt();
return t;
}
/*6*/
TreeNode *params(void)
{
TreeNode *t;
if(token==VOID)
{
t=NULL;
match(token);
}
else
t=para_list();
return t;
}
/*7*/
TreeNode *para_list(void)
{
TreeNode *t=param();
TreeNode *p=t;
while(token==DOUHAO)
{
TreeNode *q;
match(DOUHAO);
q=param();
if (q!=NULL)
{
if (t==NULL)
{
t=p=q;
}
else
{
p->sibling=q;
p=q;
}
}
}
return t;
}
/*8*/
TreeNode *param(void)
{
TreeNode *t=newExpNode(IdK);
//添加ID屬性;
match(INT);
t->type=Int;//
t->attr.name=copyString(tokenString);//
match(ID);
if(token==ZLPAREN)
{
match(ZLPAREN);
match(ZRPAREN);
//說明是數組類型;
t->is=yes;
}
return t;
}
/*9*/
TreeNode *compound_stmt(void)
{
TreeNode *t=newStmtNode(CompoundK);
//添加COMPOUND類型;COMPOUND類型為一個STMT;
match(DLPAREN);
t->child[0]=local_declaration();
t->child[1]=statement_list();
match(DRPAREN);
return t;
}
/*10*/
TreeNode *local_declaration(void)
{
TreeNode *t=empty();
TreeNode *p=t;
while(token==INT||token==VOID)
{
TreeNode *q;
q=var_declaration();
if (q!=NULL)
{
if (t==NULL)
{
t=p=q;
}
else
{
p->sibling=q;
p=q;
}
}
}
return t;
}
/*11*/
TreeNode *statement_list(void)
{
TreeNode *t=empty();
TreeNode *p=t;
while(token==ID||token==DLPAREN||token==IF||token==WHILE||token==RETURN||token==LPAREN||token==NUM)
{
TreeNode *q;
q=statement();
if (q!=NULL)
{
if (t==NULL)
{
t=p=q;
}
else
{
p->sibling=q;
p=q;
}
}
}
return t;
}
/*TreeNode *statement_list(void)
{
TreeNode *t;
t=empty();
TreeNode *p;
p=t;
while()//
{
TreeNode *q;
q=statement();
p->sibling=q;
p=q;
}
return t;
}*/
/*12*/
TreeNode *statement(void)
{
TreeNode *t;
switch (token)
{
case ID:t=expression_stmt();break;
case SEMI:t=expression_stmt();break;
case LPAREN:t=expression_stmt();break;
case NUM:t=expression_stmt();break;
case DLPAREN:t=compound_stmt();break;
case IF:t=selection_stmt();break;
case WHILE:t=iteration_stmt();break;
case RETURN:t=return_stmt();break;
default:
syntaxError("unexpected error at ->>");break;
}
return t;
}
/*14*/
TreeNode *expression_stmt(void)
{
TreeNode *t;
if(token!=SEMI)
{
t=expression();
}
else
{
t=NULL;
}
match(SEMI);
return t;
}
/*17*/
TreeNode *return_stmt(void)
{
TreeNode *t=newStmtNode(ReturnK);
//添加RETURN屬性;
match(RETURN);
if(token==SEMI)
{
match(token);
}
else
{
t->child[0]=expression();
match(SEMI);
}
return t;
}
/*15*/
TreeNode *selection_stmt(void)
{
TreeNode *t=newStmtNode(IfK);
//添加IF屬性;
match(IF);
match(LPAREN);
t->child[0]=expression();
match(RPAREN);
t->child[1]=statement();
if(token==ELSE)
{
match(ELSE);
t->child[2]=statement();
}
return t;
}
/*16*/
TreeNode *iteration_stmt(void)
{
TreeNode *t=newStmtNode(WhileK);
//添加WHILE屬性;
match(WHILE);
match(LPAREN);
t->child[0]=expression();
match(RPAREN);
t->child[1]=statement();
return t;
}
/*18*/
TreeNode *expression(void)
{
TreeNode *t;
TreeNode *a=NULL;
TreeNode *q=NULL;
loop: switch (token)
{
case ID:
t=newExpNode(IdK);
//添加ID屬性;并添加TOKENSTRING;
t->attr.name=copyString(tokenString);//
match(ID);
if (token==LPAREN)
{
t->idtype1=FunK;
match(LPAREN);
t->child[0]=args();
match(RPAREN);
}
else
{
t->idtype1=VarK;
if (token==ZLPAREN)
{
match(ZLPAREN);
t->is=yes;
t->child[0]=expression();
match(ZRPAREN);
}
if (token==ASSIGN)
{
TreeNode *p=newStmtNode(AssignK);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -