?? parse.cpp
字號:
/****************************************************/
/* 文件 parse.c */
/* 說明 TINY編譯器的語法分析器實現 */
/* 主題 編譯器結構:原理和實例 */
/****************************************************/
/*********** 該文件所包含的頭文件 ****************/
#include "globals.h"
#include "util.h"
#include "scanner.h"
#include "parse.h"
#include "string.h"
static TokenType token;
char * temp_name;
static int line0;
/********** 遞歸調用的函數原型 ************/
static TreeNode * program(void);
static TreeNode * programHead(void);
static TreeNode * declarePart(void);
static TreeNode * typeDec(void);
static TreeNode * typeDeclaration(void);
static TreeNode * typeDecList(void);
static TreeNode * typeDecMore(void);
static void typeId(TreeNode * t);
static void typeName(TreeNode * t);
static void baseType(TreeNode * t);
static void structureType(TreeNode * t);
static void arrayType(TreeNode * t);
static void recType(TreeNode * t);
static TreeNode * fieldDecList(void);
static TreeNode * fieldDecMore(void);
static void idList(TreeNode * t);
static void idMore(TreeNode * t);
static TreeNode * varDec(void);
static TreeNode * varDeclaration(void);
static TreeNode * varDecList(void);
static TreeNode * varDecMore(void);
static void varIdList(TreeNode * t);
static void varIdMore(TreeNode * t);
static TreeNode * procDec(void);
static TreeNode * procDeclaration(void);
static void paramList(TreeNode * t);
static TreeNode * paramDecList(void);
static TreeNode * param(void);
static TreeNode * paramMore(void);
static void formList(TreeNode * t);
static void fidMore(TreeNode * t);
static TreeNode * procDecPart(void);
static TreeNode * procBody(void);
static TreeNode * programBody(void);
static TreeNode * stmList(void);
static TreeNode * stmMore(void);
static TreeNode * stm(void);
static TreeNode * assCall(void);
static TreeNode * assignmentRest(void);
static TreeNode * conditionalStm(void);
static TreeNode * loopStm(void);
static TreeNode * inputStm(void);
static TreeNode * outputStm(void);
static TreeNode * returnStm(void);
static TreeNode * callStmRest(void);
static TreeNode * actParamList(void);
static TreeNode * actParamMore(void);
static TreeNode * exp(void); /* 處理表達式函數 */
static TreeNode * simple_exp(void); /* 處理簡單表達式函數 */
static TreeNode * term(void); /* 處理項函數 */
static TreeNode * factor(void); /* 處理因子函數 */
static TreeNode * variable(void);
static void variMore(TreeNode * t);
static TreeNode * fieldvar(void);
static void fieldvarMore(TreeNode * t );
/************ 語法分析功能函數 **************/
/********************************************************************/
/* 函數名 syntaxError */
/* 功 能 語法錯誤處理函數 */
/* 說 明 將函數參數message指定的錯誤信息格式化寫入列表文件listing */
/* 設置錯誤追蹤標志Error為TRUE */
/********************************************************************/
static void syntaxError(char * message)
{
fprintf(listing,"\n>>> error : ");
fprintf(listing,"Syntax error at line %d: %s\n",token.lineshow,message);
Error = TRUE;
}
/********************************************************************/
/* 函數名 match */
/* 功 能 終極符匹配處理函數 */
/* 說 明 函數參數expected給定期望單詞符號與當前單詞符號token相匹配 */
/* 如果不匹配,則報非期望單詞語法錯誤 */
/********************************************************************/
static void match(LexType expected)
{
if (token.Lex == expected)
{
ReadNextToken(&token);
line0 = token.lineshow;
}
else
{
syntaxError("not match error ");
fprintf(listing,"'%s'\n",token.Sem);
ReadNextToken(&token);
exit(0);
}
}
/********************************************************************/
/* 函數名 program */
/* 功 能 總程序的處理函數 */
/* 產生式 < program > ::= programHead declarePart programBody . */
/* 說 明 函數根據文法產生式,調用相應的遞歸處理函數,生成語法樹節點 */
/* 語法樹的根節點的第一個子節點指向程序頭部分programHead, */
/* DeclaraPart為programHead的兄弟節點,程序體部分programBody */
/* 為declarePart的兄弟節點. */
/********************************************************************/
TreeNode * program(void)
{
TreeNode * t=programHead();
TreeNode * q=declarePart();
TreeNode * s=programBody();
TreeNode * root = newRootNode();
if(root!=NULL)
{
root->lineno = 0;
if(t!=NULL) root->child[0] = t;
else syntaxError("a program head is expected!");
if(q!=NULL) root->child[1] = q;
if(s!=NULL) root->child[2] = s;
else syntaxError("a program body is expected!");
}
match(DOT);
return root;
}
/********************************************************************/
/* 函數名 programHead */
/* 功 能 程序頭的處理函數 */
/* 產生式 < programHead > ::= PROGRAM ProgramName */
/* 說 明 函數根據文法產生式,調用相應的遞歸處理函數,生成語法樹節點 */
/********************************************************************/
TreeNode * programHead(void)
{
TreeNode * t = newPheadNode();
match(PROGRAM);
if((t!=NULL)&&(token.Lex==ID))
{
t->lineno = 0;
strcpy(t->name[0], token.Sem);
}
match(ID);
return t;
}
/*************************聲明部分***********************************/
/********************************************************************/
/* 函數名 declarePart */
/* 功 能 聲明部分的處理函數 */
/* 產生式 < declarePart > ::= typeDec varDec procDec */
/* 說 明 函數根據文法產生式,調用相應的遞歸處理函數,生成語法樹節點 */
/********************************************************************/
TreeNode * declarePart(void)
{
/*類型*/
TreeNode * typeP = newDecANode(TypeK);
TreeNode * pp = typeP;
if(typeP!=NULL)
{
typeP->lineno = 0;
TreeNode * tp1 = typeDec();
if(tp1!=NULL)
typeP->child[0] = tp1;
else
{
free(typeP);
typeP=NULL;
}
}
/*變量*/
TreeNode * varP = newDecANode(VarK);
if(varP != NULL)
{
varP->lineno = 0;
TreeNode * tp2 = varDec();
if(tp2 != NULL)
varP->child[0] = tp2;
else
{
free(varP);
varP=NULL;
}
}
/*函數*/
TreeNode * s = procDec();
if(s==NULL){}
if(varP==NULL){varP=s;}
if(typeP==NULL){pp=typeP=varP;}
if(typeP!=varP)
{
typeP->sibling = varP;
typeP = varP;
}
if(varP!=s)
{
varP->sibling = s;
varP = s;
}
return pp;
}
/**************************類型聲明部分******************************/
/********************************************************************/
/* 函數名 typeDec */
/* 功 能 類型聲明部分的處理函數 */
/* 產生式 < typeDec > ::= ε | TypeDeclaration */
/* 說 明 函數根據文法產生式,調用相應的遞歸處理函數,生成語法樹節點 */
/********************************************************************/
TreeNode * typeDec(void)
{
TreeNode * t = NULL;
switch (token.Lex)
{
case TYPE: t = typeDeclaration(); break;
case VAR:
case PROCEDURE:
case BEGIN: break;
default :
ReadNextToken(&token);
syntaxError("unexpected token is here!");
break;
}
return t;
}
/********************************************************************/
/* 函數名 TypeDeclaration */
/* 功 能 類型聲明部分的處理函數 */
/* 產生式 < TypeDeclaration > ::= TYPE TypeDecList */
/* 說 明 函數根據文法產生式,調用相應的遞歸處理函數,生成語法樹節點 */
/********************************************************************/
TreeNode * typeDeclaration(void)
{
match(TYPE);
TreeNode * t = typeDecList();
if (t==NULL)
{
syntaxError("a type declaration is expected!");
}
return t;
}
/********************************************************************/
/* 函數名 TypeDecList */
/* 功 能 類型聲明部分的處理函數 */
/* 產生式 < TypeDecList > ::= typeId = typeName ; typeDecMore */
/* 說 明 函數根據文法產生式,調用相應的遞歸處理函數,生成語法樹節點 */
/********************************************************************/
TreeNode * typeDecList(void)
{
TreeNode * t = newDecNode();
if (t != NULL)
{
t->lineno = line0;
typeId(t);
match(EQ);
typeName(t);
match(SEMI);
TreeNode * p = typeDecMore();
if (p!=NULL)
t->sibling = p;
}
return t;
}
/********************************************************************/
/* 函數名 typeDecMore */
/* 功 能 類型聲明部分的處理函數 */
/* 產生式 < typeDecMore > ::= ε | TypeDecList */
/* 說 明 函數根據文法產生式,調用相應的遞歸處理函數,生成語法樹節點 */
/********************************************************************/
TreeNode * typeDecMore(void)
{
TreeNode * t=NULL;
switch(token.Lex)
{
case VAR:
case PROCEDURE:
case BEGIN:
break;
case ID:
t = typeDecList();
break;
default:
ReadNextToken(&token);
syntaxError("unexpected token is here!");
break;
}
return t;
}
/********************************************************************/
/* 函數名 typeId */
/* 功 能 類型聲明部分的處理函數 */
/* 產生式 < typeId > ::= id */
/* 說 明 函數根據文法產生式,調用相應的遞歸處理函數,生成語法樹節點 */
/********************************************************************/
void typeId(TreeNode * t)
{
int tnum = (t->idnum);
if ((token.Lex==ID)&&(t!=NULL))
{
strcpy(t->name[tnum] ,token.Sem);
tnum = tnum+1;
}
t->idnum = tnum;
match(ID);
}
/********************************************************************/
/* 函數名 typeName */
/* 功 能 類型聲明部分的處理函數 */
/* 產生式 < typeName > ::= baseType | structureType | id */
/* 說 明 函數根據文法產生式,調用相應的遞歸處理函數,生成語法樹節點 */
/********************************************************************/
void typeName(TreeNode * t)
{
if (t!=NULL)
switch (token.Lex)
{
case INTEGER:
case CHAR: baseType(t);break;
case ARRAY:
case RECORD: structureType(t);break;
case ID:
t->kind.dec = IdK;
strcpy(t->attr.type_name ,token.Sem);
match(ID);
break;
default:
ReadNextToken(&token);
syntaxError("unexpected token is here!");
break;
}
}
/********************************************************************/
/* 函數名 baseType */
/* 功 能 類型聲明部分的處理函數 */
/* 產生式 < baseType > ::= INTEGER | CHAR */
/* 說 明 函數根據文法產生式,調用相應的遞歸處理函數,生成語法樹節點 */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -