Ejemplo n.º 1
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.º 2
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.º 3
0
	private void typeDef( TreeNode ptr , typeIR tIR , int layer ) {
		if( ptr.ChildNum == 0 ) {
			tIR = null ; 
			return ;
		}
		if( ptr.childs[0].IsTerminal == true ) {
			if( ptr.childs[0].NonTerminal.Equals(nonTerminals.BaseType ) ) {
				if( ptr.childs[0].childs[0].Data.Equals( "integer" ) ) tIR.copy( IntTy ) ;
				else if( ptr.childs[0].childs[0].Data.Equals( "char" ) ) tIR.copy( CharTy ) ;
			}
			else if( ptr.childs[0].NonTerminal.Equals(nonTerminals.StructureType ) ) 
				structureType( ptr.childs[0] , tIR , layer ) ;
		}
		else if( ptr.childs[0].Terminal.Equals( LexType.ID ) ) {
			SymTableNode p = myScope.FindID( ptr.childs[0].Data , true ) ;
			
			if( p == null ) {
				String str ; 
				str = "语义错误:		类型标识符" + ptr.childs[0].Data + "未定义" ;
				error( str , ptr.childs[0].Line , ptr.childs[0].Row ) ;
				tIR = null ; 
			}
			else {
				if( p.attrIR.kind != IdKind.typekind ) {
					String str ;
					str = "语义错误:		" + ptr.childs[0].Data + "非类型标识符" ;
					error( str , ptr.childs[0].Line , ptr.childs[0].Row ) ;
					tIR = null ;
				}
				else tIR.copy( p.attrIR.idtype ) ;
			}
		}
	} 
Ejemplo n.º 4
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.º 5
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 ) ;
		}
	}