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

📄 analyuyi.java

📁 这是编译原理的语义分析器的java代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	    else 
		AnalyzeError(t,"operator is not compat!",null);
	}
    }
    return Eptr;
}			

/************************************************************/
/* 函数名  arrayVar                                         */
/* 功  能  该函数处理数组变量的下标分析                     */
/* 说  明  检查var := var0[E]中var0是不是数组类型变量,E是不*/
/*         是和数组的下标变量类型匹配。                     */
/************************************************************/
TypeIR arrayVar(TreeNode t)
{
    boolean present = false;
    SymbTable entry = new SymbTable();

    TypeIR Eptr0=null;
    TypeIR Eptr1=null;
    TypeIR Eptr = null;
	
	
    /*在符号表中查找此标识符*/

    present = FindEntry(t.name[0],entry);				
    t.table[0] = entry;	
    /*找到*/
    if(present)
    {
	/*Var0不是变量*/
	if (!(entry.attrIR.kind.equals("varkind")))
	{
	    AnalyzeError(t,"is not variable error!",t.name[0]);			
	    Eptr = null;
	}
	/*Var0不是数组类型变量*/
	else if(entry.attrIR.idtype!=null)
        {
	    if(!(entry.attrIR.idtype.kind.equals("arrayTy")))
	    {
		AnalyzeError(t,"is not array variable error !",t.name[0]);
		Eptr = null;
	    }
	    else
	    {	
		/*检查E的类型是否与下标类型相符*/
		Eptr0 = entry.attrIR.idtype.array.indexTy;
		if(Eptr0==null)
		    return null;
		Eptr1 = Expr(t.child[0],null);//intPtr;
		if(Eptr1==null)
		    return null;
		present = Compat(Eptr0,Eptr1);
		if(!present)
		{
		    AnalyzeError(t,"type is not matched with the array member error !",null);
		    Eptr = null;
		}
		else
		    Eptr = entry.attrIR.idtype.array.elementTy;
	    }
        }
    }
    else/*标识符无声明*/
	AnalyzeError(t,"is not declarations!",t.name[0]);
    return Eptr;
}

/************************************************************/
/* 函数名  recordVar                                        */
/* 功  能  该函数处理记录变量中域的分析                     */
/* 说  明  检查var:=var0.id中的var0是不是记录类型变量,id是 */
/*         不是该记录类型中的域成员。                       */
/************************************************************/
TypeIR recordVar(TreeNode t)
{
	boolean present = false;
	boolean result = false;
	SymbTable entry = new SymbTable();

	TypeIR Eptr0=null;
	TypeIR Eptr1=null;
	TypeIR Eptr = null;
	FieldChain currentP = new FieldChain();
	
	
	/*在符号表中查找此标识符*/
	present = FindEntry(t.name[0],entry);				
	t.table[0] = entry;	
	/*找到*/
	if(present)
	{
	    /*Var0不是变量*/
	    if (!(entry.attrIR.kind.equals("varkind")))
	    {
		AnalyzeError(t,"is not variable error!",t.name[0]);				
		Eptr = null;
	    }
	    /*Var0不是记录类型变量*/
	    else if(!(entry.attrIR.idtype.kind.equals("recordTy")))
	    {
		AnalyzeError(t,"is not record variable error!",t.name[0]);
		Eptr = null;
	    }
	    else/*检查id是否是合法域名*/
	    {
		Eptr0 = entry.attrIR.idtype;
		currentP = Eptr0.body;
		while((currentP!=null)&&(!result))
		{  
        	    result = t.child[0].name[0].equals(currentP.id);
		    /*如果相等*/
		    if(result)
			Eptr = currentP.unitType;
		    else
			currentP = currentP.next;
		}	 
		if(currentP==null)
		    if(!result)
		    {
		        AnalyzeError(t,"is not field type!",t.child[0].name[0]);    				                        Eptr = null;
		    }
	            /*如果id是数组变量*/
		    else if(t.child[0].child[0]!=null)
			Eptr = arrayVar(t.child[0]);
	    }
	}
	else/*标识符无声明*/
	    AnalyzeError(t,"is not declarations!",t.name[0]);
	return Eptr;
}
		
/****************************************************/
/* 函数名  CallSA				    */
/* 功  能  函数调用语句处理函数    		    */
/* 说  明  检查非函数标识符错,调用检查形实参是否   */	
/*		   相容函数			    */
/****************************************************/
void CallSA(TreeNode t)
{ 
	String Ekind=" ";
	boolean present = false;
	SymbTable entry=new SymbTable();
	TreeNode p = null;

	/*用id检查整个符号表*/
	present = FindEntry(t.child[0].name[0],entry);		
        t.child[0].table[0] = entry;

	/*未查到表示函数无声明*/
	if (!present)                     
	    AnalyzeError(t,"function is not declarationed!",t.child[0].name[0]);  
        else 
	    /*id不是函数名*/
	    if (!(entry.attrIR.kind.equals("prockind")))     
		AnalyzeError(t,"is not function name!",t.child[0].name[0]);
	    else/*形实参匹配*/
	    {
		p = t.child[1];
		/*paramP指向形参符号表的表头*/
		ParamTable paramP = entry.attrIR.proc.param;	
		while((p!=null)&&(paramP!=null))
		{
		    SymbTable paraEntry = paramP.entry;
		    TypeIR Etp = Expr(p,Ekind);/*实参*/
		    /*参数类别不匹配*/
		    if ((paraEntry.attrIR.var.access.equals("indir"))&&(Ekind.equals("dir")))
			AnalyzeError(t,"param kind is not match!",null);  
			/*参数类型不匹配*/
                    else if((paraEntry.attrIR.idtype)!=Etp)
			AnalyzeError(t,"param type is not match!",null);
		    p = p.sibling;
		    paramP = paramP.next;
		}
		/*参数个数不匹配*/
		if ((p!=null)||(paramP!=null))
		    AnalyzeError(t,"param num is not match!",null); 
	    }
}
/****************************************************/
/* 函数名  ReadSA				    */
/* 功  能  读语句处理函数	    		    */
/* 说  明  检查标识符未声明错,非变量标识符错	    */
/****************************************************/
void ReadSA(TreeNode t)
{ 
    SymbTable Entry=new SymbTable();
    boolean present=false;
    /*查找变量标识符*/
    present = FindEntry(t.name[0],Entry);
    /*变量在符号表中的地址写入语法树*/
    t.table[0] = Entry;

    if (!present)   /*检查标识符未声明错*/
	AnalyzeError(t," id no declaration in read ",t.name[0]);
    else if (!(Entry.attrIR.kind.equals("varkind")))   /*检查非变量标识符错*/ 
        AnalyzeError(t," not var id in read statement ", null);
}
/****************************************************/
/* 函数名  WriteSA				    */
/* 功  能  写语句处理函数	    		    */
/* 说  明  调用表达式处理函数,检查语义错误	    */
/****************************************************/
void WriteSA(TreeNode t)  
{ 
    TypeIR Etp = Expr(t.child[0],null);	
    if(Etp!=null)
	/*如果表达式类型为bool类型,报错*/
	if (Etp.kind.equals("boolTy"))
		AnalyzeError(t,"exprssion type error!",null);
}
/****************************************************/
/* 函数名  IfSA					    */
/* 功  能  条件语句处理函数	    		    */
/* 说  明  检查非布尔表达式错,并调用语句序列函数   */
/*	   处理then部分和 else 部分	            */	
/****************************************************/
void IfSA(TreeNode t)
{ 
    String Ekind=null;
    TypeIR Etp;
    Etp=Expr(t.child[0],Ekind);
    
    if (Etp!=null)   /*表达式没有错误*/
        if (!(Etp.kind.equals("boolTy")))   /*检查非布尔表达式错*/
	    AnalyzeError(t," not bool expression in if statement ",null);
	else
	{
	    TreeNode p = t.child[1];
	    /*处理then语句序列部分*/
	    while(p!=null)
	    {
		StatementA(p);
		p=p.sibling;
	    }
	    t = t.child[2];		/*必有三儿子*/
	    /*处理else语句部分*/
	    while(t!=null)
	    {
		StatementA(t);	
		t=t.sibling;
	    }
	}
}
/****************************************************/
/* 函数名  WhileSA				    */
/* 功  能  循环语句处理函数	    		    */
/* 说  明  检查非布尔表达式错,并调用语句序列函数   */
/*	   处理循环体			            */	
/****************************************************/
void  WhileSA(TreeNode t)
{ 
    TypeIR Etp;
    Etp=Expr(t.child[0],null);
   
    if (Etp!=null)  /*表达式没有错*/	  
        if (!(Etp.kind.equals("boolTy")))   /*检查非布尔表达式错*/
	    AnalyzeError(t," not bool expression in if statement ",null);
    /*处理循环体*/
    else
    {
	t = t.child[1];
	/*处理循环部分*/
	while(t!=null)
	{ 
	    StatementA(t);
	    t=t.sibling;
	}
    }
}
/****************************************************/
/* 函数名  ReturnSA				    */
/* 功  能  返回语句处理函数	    		    */
/* 说  明  若出现在主程序中,则语义错误		    */	
/****************************************************/
void  ReturnSA(TreeNode t)
{
    if (Level == 0)
	AnalyzeError(t," return statement cannot in main program ",null);
}

/****************************************************/
/*****************功能函数***************************/
/****************************************************/
/* 函数名  AnalyzeError				    */
/* 功  能  给出语义错误提示信息			    */
/* 说  明  Error设置为true,防止错误的传递	    */
/****************************************************/
void AnalyzeError(TreeNode t,String message,String s)
{   
    if (t==null)
        yerror=yerror+"\n>>> ERROR:"+"Analyze error "+":"+message+s+"\n"; 
    else
        yerror=yerror+"\n>>> ERROR :"+"Analyze error at "+String.valueOf(t.lineno)+": "+message+s+"\n";  

    /* 设置错误追踪标志Error为TRUE,防止错误进一步传递 */
    Error = true;
}
/****************************************************/
/* 函数名  initiate				    */
/* 功  能  建立整型,字符型,布尔类型的内部表示	    */
/* 说  明  这几个类型的内部表示式固定的,只需建立   */
/*	   一次,以后引用相应的引用即可	            */	
/****************************************************/
void initiate()
{   
    /*整数类型的内部表示*/
    intptr.kind="intTy";
    intptr.size=1;
    /*字符类型的内部表示*/
    charptr.kind="charTy";
    charptr.size=1;
    /*布尔类型的内部表示*/
    boolptr.kind="boolTy";
    boolptr.size=1;
}

/********************************************************/
/*************符号表相关函数*****************************/
/********************************************************/
/* 函数名  CreatSymbTable			        */
/* 功  能  创建一个符号表				*/
/* 说  明  并没有真正生成新的符号表,只是层数加一	*/
/********************************************************/
void  CreatSymbTable()
{ 	
    Level = Level +1; 
    scope[Level] = null;	
    Off = initOff;  /* 根据目标代码生成需要,initOff应为AR首地址sp
		       到形参变量区的偏移7 */	
}
/********************************************************/
/* 函数名  DestroySymbTable				*/
/* 功  能  删除一个符号表				*/
/* 说  明  并不真正释放这个符号表空间,只是改变scope栈  */
/********************************************************/
void  DestroySymbTable()
{
    PrintOneLayer(Level);
    /*用层数减1,来表示删除当前符号表*/
    Level = Level - 1;
}
/**********************************************************/
/* 函数名  Enter					  */
/* 功  能  将一个标识符及其属性登记到符号表		  */
/* 说  明  返回值决定标识符是否重复,由Entry带回此标识符  */
/*         在符号表中的位置,若重复,则不登记,Entry为找  */
/*	   到的那个标识符的位置				  */
/**********************************************************/
boolean Enter(String id,AttributeIR attribP,SymbTable entry)
{ 
    boolean present = false;
    boolean result = false;
    SymbTable curentry=null;
    SymbTable prentry=null;

    if(scope[Level]==null)
    {
	scope[Level] = new SymbTable();
	curentry = scope[Level];
    }
    else
    {
        curentry = scope[Level];
	while (curentry != null)
	{
	    prentry = curentry;
	    result = id.equals(curentry.idName);
	    if(result)
	    {
		AnalyzeError(null," Enter , id repeat declaration ",null);
		present = true;
	    }
	    else
		curentry = prentry.next;
	}   /*在该层符号表内检查是否有重复定义错误*/
    
	if(!present)
	{
	    prentry.next = new SymbTable();
	    curentry = prentry.next;
	}
    }
		
    /*将标识符名和属性登记到表中*/
    curentry.idName = id;

    curentry.attrIR.idtype = attribP.idtype;
    curentry.attrIR.kind = attribP.kind;
    if (attribP.kind.equals("typekind"))
	{}
    else if (attribP.kind.equals("varkind")) 
    {
        curentry.attrIR.var=new Var();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -