public bool equals(typeIR b) { if (this.Count.value != b.Count.value) { return(false); } if (this.Kind != b.Kind) { return(false); } if (this.More.ArrayAttr.low != b.More.ArrayAttr.low) { return(false); } if (this.More.ArrayAttr.top != b.More.ArrayAttr.top) { return(false); } if (this.More.ArrayAttr.elemTy != b.More.ArrayAttr.elemTy) { return(false); } if (this.More.ArrayAttr.indexTy != b.More.ArrayAttr.indexTy) { return(false); } if (this.More.body != b.More.body) { return(false); } return(true); }
public void copy(typeIR b) { this.Count.value = b.Count.value; this.Kind = b.Kind; this.More.ArrayAttr.low = b.More.ArrayAttr.low; this.More.ArrayAttr.top = b.More.ArrayAttr.top; this.More.ArrayAttr.elemTy = b.More.ArrayAttr.elemTy; this.More.ArrayAttr.indexTy = b.More.ArrayAttr.indexTy; this.More.body = b.More.body; }
public bool equals(typeIR b) { if (this.Count.value != b.Count.value) return false; if (this.Kind != b.Kind) return false; if (this.More.ArrayAttr.low != b.More.ArrayAttr.low) return false; if (this.More.ArrayAttr.top != b.More.ArrayAttr.top) return false; if (this.More.ArrayAttr.elemTy != b.More.ArrayAttr.elemTy) return false; if (this.More.ArrayAttr.indexTy != b.More.ArrayAttr.indexTy) return false; if (this.More.body != b.More.body) return false; return true; }
private void initTy() { IntTy = new typeIR() ; CharTy = new typeIR() ; BoolTy = new typeIR() ; IntTy.Kind = TypeKind.intTy ; IntTy.Count.value = 2 ; CharTy.Kind = TypeKind.charTy ; CharTy.Count.value = 1 ; BoolTy.Kind = TypeKind.boolTy ; BoolTy.Count.value = 1 ; }
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 ) ; } } }
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 ) ; } } } } }
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 ) ; } }
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 ) ; } }
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 ) ; } }
private void structureType( TreeNode ptr , typeIR tIR , int layer ) { TreeNode p ; TreeNode tar = new TreeNode() ; tar.IsTerminal = true; tar.NonTerminal = nonTerminals.ArrayType; p = Search( ptr , tar , 0 ) ; if( p != null ) { arrayType( p , tIR , layer ) ; return ; } tar.NonTerminal = nonTerminals.RecType ; p = Search( ptr , tar , 0 ) ; if( p != null ) { recType( p , tIR , layer ) ; return ; } tIR = null ; }
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 ) ; } }
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 ; } }
/************/ /****变量声明***/ /************/ 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 ) ; } } }
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 ) ; } }
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 ) ; } }
private void recType( TreeNode ptr , typeIR tIR , int layer ) { tIR.Kind = TypeKind.fieldTy ; tIR.Count.value = 0 ; TreeNode p ; TreeNode tar = new TreeNode() ; tar.IsTerminal=true ; tar.NonTerminal=nonTerminals.FieldDecList ; p = Search( ptr , tar , 0 ) ; if( p != null ) fieldDecList( ptr , tIR.More.body , tIR.Count , layer ) ; }
private void arrayType( TreeNode ptr , typeIR tIR , int layer ) { tIR.Kind = TypeKind.arrayTy ; TreeNode p = ptr.childs[2] ; if( p.NonTerminal.Equals(nonTerminals.Low) ) tIR.More.ArrayAttr.low = int.Parse(p.childs[0].Data); p = ptr.childs[4]; if( p.NonTerminal.Equals(nonTerminals.Top) ) tIR.More.ArrayAttr.top = int.Parse(p.childs[0].Data); tIR.More.ArrayAttr.indexTy = IntTy ; p = ptr.childs[7]; if( p.childs[0].Terminal.Equals( LexType.INTEGER ) ) tIR.More.ArrayAttr.elemTy = IntTy ; else tIR.More.ArrayAttr.elemTy = CharTy ; if( tIR.More.ArrayAttr.top <= tIR.More.ArrayAttr.low ) { String str = "语义错误: 数组上界小于了下界" ; error(str, ptr.childs[2].Line, ptr.childs[2].Row); return ; } tIR.Count.value = tIR.More.ArrayAttr.top - tIR.More.ArrayAttr.low + 1 ; tIR.Count.value *= tIR.More.ArrayAttr.elemTy.Count.value ; }
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 ) ; } }
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 ) ; } } }
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 ; } } }
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 ) ; } } }
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 ) ; } } }
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 ) ; } } }