⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parse.cpp

📁 snl语言是一个简单的具有嵌套过程定义的过程式语言
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/****************************************************/
/* 文件 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 + -