Ejemplo n.º 1
0
	private void relExp( TreeNode ptr ) {
		TreeNode p ; 
		TreeNode tar = new TreeNode() ;
		
		typeIR tIR = new typeIR() ;
		integer ntype = new integer( 0 ) ; 
		
		tar.IsTerminal=true ; tar.NonTerminal = nonTerminals.Exp  ;
		p = Search( ptr , tar , 1 ) ;
		if( p != null ) {
			Exp( p , tIR , ntype ) ;
			if( tIR.Equals( CharTy ) ) tIR.copy( IntTy ) ;
			p = Search( ptr.childs[1] , tar , 2 ) ;
			ntype.value = 1 ;
			Exp( p , tIR , ntype ) ;
		}
	}
Ejemplo n.º 2
0
	private void Exp( TreeNode ptr , typeIR tIR , integer ntype ) {
		TreeNode p ;
		TreeNode tar = new TreeNode() ;
		tar.IsTerminal=true ; tar.NonTerminal = nonTerminals.Term  ;
		
		p = Search( ptr , tar , 1 ) ;
		if( p != null ) {
			term( p , tIR , ntype ) ;
		}
		
		tar.NonTerminal = nonTerminals.OtherTerm  ;
		p = Search( ptr , tar , 0 ) ;
		if( p != null ) {
			otherTerm( p , tIR , ntype ) ;
		}
	}
Ejemplo n.º 3
0
	private void formList( TreeNode ptr , int layer , integer Count , ParamTable param , typeIR tIR , bool ntype ) {
		TreeNode p  ; 
		TreeNode tar = new TreeNode() ;
		
		SymTableNode sym = new SymTableNode() ;
		sym.name = ptr.childs[0].Data ;
		sym.next = new SymTableNode() ;
		sym.attrIR.idtype.copy( tIR ) ;
		sym.attrIR.kind = IdKind.varkind ;
		if( ntype == true ) sym.attrIR.More.VarAttr.access = AccessKind.indir ;
		else sym.attrIR.More.VarAttr.access = AccessKind.dir ; 
		sym.attrIR.level = layer ;
		sym.attrIR.More.VarAttr.offset.value = Count.value ; 
		
		if( myScope.FindID( sym.name , false ) != null ) {
			String str = "语义错误:		参数标识符" + sym.name + "重复定义!" ;
			error( str , ptr.Line , ptr.Row ) ;
			sym = null ;
			tar.IsTerminal=true ; tar.NonTerminal = nonTerminals.FormList  ;
			p = Search( ptr.childs[1] , tar , 2 ) ;
			if( p != null ) formList( p , layer , Count , param , tIR , ntype ) ;
		}
		else {
			Count.value += tIR.Count.value ;
			symTable.insertNode( sym ) ;
			param.type.copy( tIR ) ; 
			param.symPtr = sym ;
			if( myScope.scope.Peek().front == null ) myScope.scope.Peek().front = sym ;
			
			tar.IsTerminal=true ; tar.NonTerminal = nonTerminals.FormList  ;
			p = Search( ptr.childs[1] , tar , 2 ) ;
			if( p != null ) {
				param.next = new ParamTable() ;
				formList( p , layer , Count , param.next , tIR , ntype ) ;
			}
		}
	}
Ejemplo n.º 4
0
	private void outputStm( TreeNode ptr ) {
		TreeNode p ; 
		TreeNode tar = new TreeNode() ;
		
		typeIR tIR = new typeIR() ;
		integer  ntype = new integer( 0 ) ; 
		
		tar.IsTerminal=true ; tar.NonTerminal = nonTerminals.Exp  ;
		
		p = Search( ptr , tar , 0 ) ;
		if( p != null ) {
			Exp( p , tIR , ntype ) ;
		}
	}
Ejemplo n.º 5
0
	private void actParamList( TreeNode ptr , ParamTable param ) {
		TreeNode p ;
		TreeNode tar = new TreeNode() ;
		integer ntype = new integer( 1 ) ; 
		typeIR tIR = new typeIR() ; 
		
		tar.IsTerminal=true ;  tar.NonTerminal = nonTerminals.Exp  ;
		p = Search( ptr , tar , 1 ) ;
		if( p != null ) {
			if( param == null ) {
				error( "语义错误:		过程调用实参数目过多" , ptr.Line , ptr.Row ) ;
				return  ;
			}
			if( param.type.Equals( CharTy ) ) {
				ntype.value = 0 ;
				Exp( p , tIR = IntTy , ntype ) ;
			}
			else Exp( p , tIR = param.type , ntype ) ;
			
			tar.NonTerminal = nonTerminals.ActParamMore  ;
			p = Search( ptr , tar , 0 ) ;
			
			if( p != null ) {
				tar.NonTerminal = nonTerminals.ActParamList  ;
				p = Search( p , tar , 0 ) ;
				if( p != null ) actParamList( p , param.next ) ;
				else if( param.next != null ) {
					error( "语义错误:		过程调用实参数目不完整" , ptr.Line , ptr.Row ) ;
					return ;
				}
			}
			
		}
		else if( param != null ) {
			error( "语义错误:		过程调用实参数目不完整" , ptr.Line , ptr.Row ) ;
			return ;
		}
	}
Ejemplo n.º 6
0
	private void paramDecList( TreeNode ptr , int layer , integer Count , ParamTable param ) {
		TreeNode p ;
		TreeNode tar = new TreeNode() ; 
		tar.IsTerminal=true ;
		
		if( ptr.ChildNum != 0 && ptr.childs[0].NonTerminal.Equals(nonTerminals.Param ) ) {
			typeIR tIR = new typeIR() ;
			
			tar.NonTerminal = nonTerminals.TypeDef  ;
			p = Search( ptr , tar , 1 ) ;
			if( p != null ) {
				typeDef( p , tIR , layer ) ;
				if( tIR != null ) {
					p = p.father.childs[1] ;
					formList( p , layer , Count , param , tIR , false ) ;
				}
			}
			else {
                tar.IsTerminal = false; tar.Terminal = LexType.VAR;
				p = Search( ptr , tar , 1 ) ; 
				if( p != null ) {
					if( p.father.childs[1].NonTerminal.Equals(nonTerminals.TypeDef ) ) {
						typeDef( p.father.childs[1] , tIR , layer ) ;
                        tar.IsTerminal = true; tar.NonTerminal = nonTerminals.FormList;
						p = Search( p.father.childs[1] , tar , 1 ) ;
						if( tIR != null && p != null ) formList( p , layer , Count , param , tIR , true ) ;
					}
				}
			}
		}
		
		tar.NonTerminal = nonTerminals.ParamMore  ;
		p = Search( ptr , tar , 0 ) ;
		if( p != null ) {
			tar.NonTerminal = nonTerminals.ParamDecList  ;
			p = Search( p , tar , 0 ) ;
			if( p != null ) {
				if( param != null ) {
					ParamTable pm = param ;
					while( pm.next != null ) pm = pm.next ; 
					paramDecList( p , layer , Count , pm.next ) ;
				}
				else paramDecList( p , layer , Count , param ) ; 
			}
		}
	}
Ejemplo n.º 7
0
	/************/
	/****变量声明***/
	/************/
	
	private void varDecList( TreeNode ptr , int layer , integer offset ) {
		TreeNode p ; 
		TreeNode tar = new TreeNode() ;
		tar.IsTerminal=true ; tar.NonTerminal = nonTerminals.TypeDef  ;
		
		p = Search( ptr , tar , 0 ) ;
		if( p != null ) {
			typeIR tIR = new typeIR() ; 
			typeDef( p , tIR , layer ) ;
			if( tIR != null ) {
				varIdList( p.father.childs[1] , layer , offset , tIR ) ;
			}
		}
		
		tar. NonTerminal = nonTerminals.VarDecMore  ;
		p = Search( ptr , tar , 0 ) ;
		if( p != null ) {
			tar.NonTerminal = nonTerminals.VarDecList ;
			p = Search( p , tar , 1 ) ;
			if( p != null ) {
				varDecList( p , layer , offset ) ;
			}
		}
	}
Ejemplo n.º 8
0
	private void otherTerm( TreeNode ptr , typeIR tIR , integer ntype ) {
		TreeNode p ; 
		TreeNode tar = new TreeNode() ;
		tar.IsTerminal=true ; tar.NonTerminal = nonTerminals.Exp  ;
		
		p = Search( ptr , tar , 2 ) ;
		if( p != null ) {
			Exp( p , tIR , ntype ) ;
		}
	}
Ejemplo n.º 9
0
	private void fieldDecList( TreeNode ptr , fieldChain body , integer Count , int layer ) {
		TreeNode p ; 
		TreeNode tar = new TreeNode() ;
		tar.IsTerminal=true ; tar.NonTerminal = nonTerminals.BaseType  ;
		
		p = Search( ptr , tar , 1 ) ;
		if( p != null ) {
			if( p.childs[0].Terminal.Equals( LexType.INTEGER ) ) 
				idList( p.father.childs[1] , p.father.childs[1] , body , layer , Count , IntTy ) ;
			else if( p.childs[0].Terminal.Equals( LexType.CHAR ) )
				idList( p.father.childs[1] , p.father.childs[1] , body , layer , Count , CharTy ) ;
			return ;
		}
		
		tar.NonTerminal = nonTerminals.ArrayType ;
		p = Search( ptr , tar , 1 ) ;
		if( p != null ) {
			typeIR tIR = new typeIR() ;
			arrayType( p , tIR , layer ) ;
			idList( p.father.childs[1] , p.father.childs[1] , body , layer , Count , tIR ) ;
		}
	}
Ejemplo n.º 10
0
	private void idList( TreeNode ptr , TreeNode parent , fieldChain body , int layer , integer Count , typeIR tIR ) {
		body = new fieldChain() ;
		TreeNode p ; 
		body.idname = ptr.childs[0].Data ;
		body.offset.value = Count.value ;
		body.unitType = tIR ;
		body.next = new fieldChain() ;
		Count.value += tIR.Count.value ;
		
		TreeNode tar = new TreeNode() ;
		tar.IsTerminal=true ;  tar.NonTerminal = nonTerminals.IdMore  ;
		p = Search( ptr , tar , 0 ) ;
		if( p != null ) {
			idList( ptr.childs[1].childs[1] , parent , body.next , layer , Count , tIR ) ;
			return ;
		}
		
		tar.NonTerminal = nonTerminals.FieldDecList  ;
		p = Search( parent.father.childs[4] , tar , 0 ) ;
		if( p != null ) {
			fieldDecList( p , body.next , Count , layer ) ;
		}
	}
Ejemplo n.º 11
0
	private void varDecpart( TreeNode ptr , int layer , integer offset ) {
		TreeNode p ; 
		TreeNode tar = new TreeNode() ;
		
		tar.IsTerminal=true ;  tar.NonTerminal = nonTerminals.VarDecList  ;
		
		if( ptr.ChildNum != 0 && ptr.childs[0].ChildNum == 0 ) return ;
		
		p = Search( ptr , tar , 2 ) ;
		
		if( p != null ) varDecList( p , layer , offset ) ;
		
	}
Ejemplo n.º 12
0
	private void declarePart( TreeNode ptr , int layer , integer offset ) {
		TreeNode p ; 
		TreeNode tar = new TreeNode() ;
		tar.IsTerminal=true ;  tar.NonTerminal = nonTerminals.TypeDecpart  ;
		p = Search( ptr , tar , 1 ) ;
		if( p != null ) typeDecpart( p , layer ) ;
		
		tar.NonTerminal = nonTerminals.VarDecpart  ;
		
		p = Search( ptr , tar , 0 ) ;
		if( p != null ) varDecpart( p , layer , offset ) ;
		
		ptr.symtPtr = myScope.scope.Peek().front ;
		
		tar.NonTerminal = nonTerminals.ProcDecpart ;
		p = Search( ptr , tar , 0 ) ;
		if( p != null ) {
			tar.NonTerminal = nonTerminals.ProcDecpart ;
			TreeNode temp ;
			temp = Search( p , tar , 2 ) ;
			if( temp != null ) procDecpart( p , layer + 1 ) ;
		}
	}
Ejemplo n.º 13
0
	private void program( TreeNode Root ) {
		TreeNode p ;
		TreeNode tar = new TreeNode() ;
		tar.IsTerminal=true ; 	tar.NonTerminal= nonTerminals.DeclarePart ;
		
		p = Search( Root , tar , 0 ) ;
		if( p != null ) {
			SymTableNode ptr = new SymTableNode() ;
			ptr.EOFL = true ;
			myScope.newLayer( null ) ;
			integer offset = new integer( OFFSET ) ;
			declarePart( p , 0 , offset ) ;
		}
		
		tar.NonTerminal = nonTerminals.ProgramBody  ;
		p = Search( Root , tar , 0 ) ;
		if( p != null ) {
			programBody( p ) ;
		}
	}
Ejemplo n.º 14
0
	private void variMore( TreeNode ptr , SymTableNode sym , typeIR tIR ) {
		TreeNode p ; 
		TreeNode tar = new TreeNode() ;
		integer ntype = new integer( 2 ) ; 
		
		tar.IsTerminal=true ; tar.NonTerminal = nonTerminals.Exp  ;
		p = Search( ptr , tar , 0 ) ;
		if( p != null ) {
			tIR.copy( sym.attrIR.idtype.More.ArrayAttr.indexTy ) ;
			Exp( p , tIR , ntype ) ;
		}
		else {
			tar.NonTerminal = nonTerminals.FieldVar  ;
			p = Search( ptr , tar , 0 ) ;
			if( p != null ) {
				if( p.ChildNum != 0 && p.childs[0].Terminal.Equals( LexType.ID ) ) {
					fieldChain body = new fieldChain() ;
					body = sym.attrIR.idtype.More.body ;
					while( body != null ) {
						if( body.idname.Equals( p.childs[0].Data) ) break ; 
						body = body.next ;
					}
					if( body != null ) {
						tIR.copy( IntTy ) ;
						tar.NonTerminal = nonTerminals.FieldVarMore  ;
						p = Search( p , tar , 0 ) ;
						if( p != null ) {
							tar.NonTerminal = nonTerminals.Exp  ;
							p = Search( p , tar , 0 ) ;
							ntype.value = 2 ;
							Exp( p , tIR , ntype ) ;
						}
					}
					else {
						error( "语义错误:		" + p.childs[0].Data+"并非过程标识符,而是"+sym.name+"类型" , p.childs[0].Line , p.childs[0].Row ) ;
					}
				}
			}
		}
	}
Ejemplo n.º 15
0
	private void varIdList( TreeNode ptr , int layer , integer offset , typeIR tIR ) {
		SymTableNode p = new SymTableNode() ;
		p.name = ptr.childs[0].Data ;
		p.next = new SymTableNode() ;
		p.attrIR.kind = IdKind.varkind ;
		p.attrIR.idtype.copy( tIR ) ;
		p.attrIR.More.VarAttr.offset.value = offset.value ;
		p.attrIR.level = layer ;
		p.attrIR.More.VarAttr.access = AccessKind.dir ;
		if( myScope.FindID( p.name , false ) == null ) {
			if( p.attrIR.idtype != null ) {
				offset.value += p.attrIR.idtype.Count.value ;
				symTable.insertNode( p ) ;
				if( myScope.scope.Peek().front == null ) {
					myScope.scope.Peek().front = p ; 
					ptr.symtPtr = p ; 
				}
			}
			else return ;
		}
		else {
			error( "语义错误:		变量" + p.name + "重复定义" , ptr.Line , ptr.Row ) ;
			return ;
		}
		
		TreeNode q ; 
		TreeNode tar = new TreeNode() ;
		tar.IsTerminal=true ; tar.NonTerminal = nonTerminals.VarIdMore  ;
		q = Search( ptr , tar , 0 ) ;
		if( q != null ) {
			tar.NonTerminal = nonTerminals.VarIdList  ;
			q = Search( q , tar , 0 ) ;
			if( q != null ) {
				varIdList( q , layer , offset , tIR ) ;
			}
		}
	}
Ejemplo n.º 16
0
	private void term( TreeNode ptr , typeIR tIR , integer ntype) {
		TreeNode p , q ;
		TreeNode tar = new TreeNode() ;
		tar.IsTerminal=true ; tar.NonTerminal = nonTerminals.Factor  ;
		
		p = Search( ptr , tar , 1 ) ;
		if( p != null ) {
			tar.NonTerminal = nonTerminals.Exp  ;
			q = Search( p , tar , 0 ) ;
			if( q !=null ) Exp( q , tIR , ntype) ;
			else {
				tar.NonTerminal = nonTerminals.Variable  ;
				q = Search( p , tar , 1 ) ;
				if( q != null ){
					variable( q , tIR , ntype ) ;
				}
				else if( tIR != null && p.ChildNum != 0 && ( q = p.childs[0] ).Terminal.Equals( LexType.INTC)) {
					if( !tIR.Equals( IntTy ) && ntype.value != 2 ){
						error( "语义错误:		类型应该为" + tIR.Kind + ",而不应该是整型" , q.Line , q.Row ) ;
					}
				}
			}
			tar.NonTerminal = nonTerminals.OtherFactor ;
			q = Search( ptr , tar , 0 ) ;
			if( q != null ) {
				tar.NonTerminal = nonTerminals.Term  ;
				q = Search( q , tar , 0 ) ;
				if( q != null ) term( q , tIR , ntype ) ;
			}
		}
	}
Ejemplo n.º 17
0
	private void paramList( TreeNode ptr , int layer , integer Count , ParamTable param ) {
		TreeNode p ; 
		TreeNode tar = new TreeNode() ;
		tar.IsTerminal=true ; tar.NonTerminal = nonTerminals.ParamDecList  ;
		p = Search( ptr , tar , 1 ) ;
		if( p != null ) {
			paramDecList( p , layer , Count , param ) ;
		}
	}
Ejemplo n.º 18
0
	private void variable( TreeNode ptr , typeIR tIR , integer ntype ) {
		TreeNode p , q , t ;
		TreeNode tar = new TreeNode() ;
		integer type = new integer( 0 ) ; 
		
		tar.IsTerminal = false ;  tar.Terminal = LexType.ID  ;
		p = Search( ptr , tar , 2 ) ;
		if( p != null ) {
			SymTableNode sym = myScope.FindID( p.Data , true ) ;
			if( sym != null ) {
				p.symtPtr = sym ;
				if( !sym.attrIR.kind.Equals( IdKind.varkind ) ) {
					error( "语义错误:		标识符"+ptr.childs[0].Data+"应为变量类型" , ptr.childs[0].Line , ptr.childs[0].Row ) ;
				}
				else {
					if( sym.attrIR.idtype.Kind.Equals( TypeKind.arrayTy ) ) {
						TreeNode temp = new TreeNode() ;
						TreeNode tmp ;
						temp.IsTerminal =false ; temp.Terminal =  LexType.LMIDPAREN  ;
						tmp = Search( ptr.childs[1] , temp , 0 ) ;
						if( tmp == null ) {
							String str ; 
							str = "语法错误:		把数组" + ptr.childs[0].Data + "当成标识符来使用了" ;
							error( str , ptr.childs[0].Line , ptr.childs[0].Row ) ;
						}
					}
					if( ptr.childs[1] != null && ptr.childs[1].ChildNum != 0 ) {
						
						tar.IsTerminal=true ; tar.NonTerminal = nonTerminals.Exp  ;
						q = Search( ptr.childs[1] , tar , 0 ) ;
						if( q != null ) {
							if( !sym.attrIR.idtype.Kind.Equals( TypeKind.arrayTy ) ){
								String str ;
								str = "语义错误:		标识符" + ptr.childs[0].Data + "类型应该为" + " arrayTy" ;
								error( str , ptr.childs[0].Line , ptr.childs[0].Row ) ;
								return ;
							}
							if( ntype.value != 0 && !sym.attrIR.idtype.More.ArrayAttr.elemTy.Equals( tIR ) ){
								String str ;
								str = "语义错误:		标识符" + ptr.childs[0].Data + "类型应该为" + tIR.Kind ;
								error( str , ptr.childs[0].Line , ptr.childs[0].Row ) ;
								return ; 
							}
							else {
								typeIR tIR0 = new typeIR() ;
								tIR0.copy( IntTy ) ;
								type.value = 2 ;
								Exp( q , tIR0 , type ) ;
								if( ntype.value == 0 ) {
									ntype.value = 1 ; 
									tIR.copy( sym.attrIR.idtype.More.ArrayAttr.elemTy ) ; 
								}
							}
						}
						tar.NonTerminal = nonTerminals.FieldVar  ;
						q = Search( ptr.childs[1] , tar , 0 ) ;
						if( q != null ) {
							if( !sym.attrIR.idtype.Kind.Equals( TypeKind.fieldTy ) ) {
								String str ; 
								str = "语义错误:		标识符" + ptr.childs[0].Data + "类型应该为" + TypeKind.fieldTy ;
								error( str , ptr.childs[0].Line , ptr.childs[0].Row ) ;
								return ;
							}
							tar.IsTerminal = false ; tar.Terminal =  LexType.ID  ;
							q = Search( q , tar , 1 ) ;
							if( q != null ) {
								String idName = q.Data ;
								fieldChain body = sym.attrIR.idtype.More.body ;
								while( body != null ) {
									if( body.idname.Equals( idName ) ) break ; 
									body = body.next ; 
								}
								if( body == null ) {
									String str ; 
									str = "语义错误:		变量" + idName + "非纪录" + p.Data + "成员变量" ;
									error( str , ptr.childs[0].Line , ptr.childs[0].Row ) ;
									return ;
								}
								else {
									tar.IsTerminal = true ; tar.NonTerminal = nonTerminals.Exp  ;
									t = Search( q.father.childs[1] , tar , 2 ) ;
									if( t != null ) {
										if( !body.unitType.Kind.Equals( TypeKind.arrayTy ) ) {
											String str ; 
											str = "语义错误:		纪录" + p.Data + "成员变量标识符" + idName + "类型并非数组类型" ;
											error( str , q.Line , q.Row ) ;
											return ; 
										}
										if( ntype.value != 0  && !body.unitType.More.ArrayAttr.elemTy.Equals( tIR ) ) {
											String str = "语义错误:		标识符" + idName + "类型应该为" +tIR.Kind ;
											error( str , q.Line , q.Row ) ;
											return ; 
										}
										else {
											typeIR tIR0 = new typeIR() ;
											tIR0.copy( IntTy ) ;
											type.value = 2 ;
											Exp( t , tIR0 , type ) ;
											if( ntype.value == 0 ) {
												ntype.value = 1 ; 
												tIR.copy( body.unitType.More.ArrayAttr.elemTy ) ; 
											}
										}
									}
									else {
										if( ntype.value != 0 && !body.unitType.Equals( tIR ) ){
											String str = "语义错误:		标识符"+ idName + "类型应该为" + tIR.Kind ;
											error( str , q.Line , q.Row ) ;
											return ; 
										}
									}
								}
							}
							else {
								String str = "语义错误:		此处不应该出现纪录类型" ;
								error( str , q.Line , q.Row ) ;
								return ;
							}
						}
					}
					else {
						if( ntype.value == 0 ){
							tIR.copy( sym.attrIR.idtype ) ;
							ntype.value = 1 ;
						}
						else if( ntype.value == 1 ) {
							if( !sym.attrIR.idtype.Equals( tIR ) ) {
								String str = "语义错误:		标识符" + ptr.childs[0].Data + "类型应该为" + tIR.Kind ;
								error( str , ptr.childs[0].Line , ptr.childs[0].Row ) ;
							}
						}
						else if( ntype.value == 2 && !sym.attrIR.idtype.Equals( IntTy ) ) {
							String str = "语义错误:		标识符" + ptr.childs[0].Data + "类型应该为" + "intTy" ;
							error( str , ptr.childs[0].Line , ptr.childs[0].Row ) ;
						}
					}
				}
			}
			else {
				String str = "语义错误:		变量标识符" + ptr.childs[0].Data + "未定义" ;
				error( str , ptr.childs[0].Line , ptr.childs[0].Row ) ;
				return ;
			}
		}
	}
Ejemplo n.º 19
0
	private void assignmentRest( TreeNode ptr , SymTableNode sym ) {
		TreeNode p ; 
		TreeNode tar = new TreeNode() ;
		typeIR tIR = new typeIR() ;
		tIR.copy( sym.attrIR.idtype ) ; 
		integer ntype = new integer( 1 ) ; 
		
		tar.IsTerminal=true ; tar.NonTerminal = nonTerminals.VariMore  ;
		p = Search( ptr , tar , 0 ) ;
		if( p != null ) variMore( p , sym , tIR ) ;
		if( tIR.Equals( CharTy ) ) tIR.copy( IntTy ) ;
		tar.NonTerminal = nonTerminals.Exp  ;
		p = Search( ptr , tar , 0 ) ;
		if( p != null ) {
			Exp( p , tIR , ntype ) ;
		}
	}