Example #1
0
        public SymTableNode FindID(String idname, bool ntype)
        {
            /***********************/
            /*从符号表栈中查找已经声明的标示符*/
            /****ntype=1时遍历所有层****/
            /****ntype=0时遍历当前层****/
            /***********************/
            ScopeNode ptr = scope.Peek();
            if (0 == scope.Count) return null;

            while (ptr != null)
            {
                SymTableNode nptr = new SymTableNode();
                nptr = ptr.front;
                while (nptr != null)
                {
                    if (nptr.name == null) { }
                    else if (nptr.name.Equals(idname))
                    {
                        return nptr;
                    }
                    if (nptr.EOFL == true) break;
                    nptr = nptr.next;
                }
                if (ntype == true) ptr = ptr.parent;
                else break;
            }
            return null;
        }
Example #2
0
 public bool insertNode(SymTableNode ptr)
 {
     /***************/
     /*向符号表插入一个节点*/
     /***************/
     if (ptr == null) return false;
     if (ptr.attrIR.kind != IdKind.prockind) ptr.EOFL = false;
     ptr.next = null;
     if (ptr.attrIR.kind == IdKind.prockind)
     {
         int l = ptr.attrIR.level;
         if (l > maxLayer) maxLayer = l;
     }
     if (ptr.attrIR.kind == IdKind.varkind)
     {
         int l = ptr.attrIR.level;
         if (l > maxLayer) maxLayer = l;
     }
     if (this.front == null)
     {
         front = rear = ptr;
     }
     else
     {
         rear.next = ptr;
         rear = rear.next;
     }
     return true;
 }
Example #3
0
 public bool insertMid(SymTableNode pre, SymTableNode next)
 {
     if (pre.next == null) pre.next = next;
     else
     {
         SymTableNode p = pre.next;
         pre.next = next;
         next.next = p;
     }
     return true;
 }
Example #4
0
 public bool newLayer(SymTableNode ptr)
 {
     /***************/
     /******新建一层****/
     /***************/
     ScopeNode nptr = new ScopeNode();
     nptr.front = ptr;
     if (0 == scope.Count) nptr.parent = null;
     else nptr.parent = scope.Peek();
     scope.Push(nptr);
     return true;
 }
Example #5
0
        public SymTableNode GetRear()
        {
            SymTableNode p = scope.Peek().front;

            while (p != null)
            {
                if (p.EOFL == true || p.next == null)
                {
                    return(p);
                }
                p = p.next;
            }
            return(null);
        }
Example #6
0
 public bool insertMid(SymTableNode pre, SymTableNode next)
 {
     if (pre.next == null)
     {
         pre.next = next;
     }
     else
     {
         SymTableNode p = pre.next;
         pre.next  = next;
         next.next = p;
     }
     return(true);
 }
Example #7
0
        public SymTableNode FindID(String idname, bool ntype)
        {
            /***********************/
            /*从符号表栈中查找已经声明的标示符*/
            /****ntype=1时遍历所有层****/
            /****ntype=0时遍历当前层****/
            /***********************/
            ScopeNode ptr = scope.Peek();

            if (0 == scope.Count)
            {
                return(null);
            }

            while (ptr != null)
            {
                SymTableNode nptr = new SymTableNode();
                nptr = ptr.front;
                while (nptr != null)
                {
                    if (nptr.name == null)
                    {
                    }
                    else if (nptr.name.Equals(idname))
                    {
                        return(nptr);
                    }
                    if (nptr.EOFL == true)
                    {
                        break;
                    }
                    nptr = nptr.next;
                }
                if (ntype == true)
                {
                    ptr = ptr.parent;
                }
                else
                {
                    break;
                }
            }
            return(null);
        }
Example #8
0
        public bool newLayer(SymTableNode ptr)
        {
            /***************/
            /******新建一层****/
            /***************/
            ScopeNode nptr = new ScopeNode();

            nptr.front = ptr;
            if (0 == scope.Count)
            {
                nptr.parent = null;
            }
            else
            {
                nptr.parent = scope.Peek();
            }
            scope.Push(nptr);
            return(true);
        }
Example #9
0
 public bool insertNode(SymTableNode ptr)
 {
     /***************/
     /*向符号表插入一个节点*/
     /***************/
     if (ptr == null)
     {
         return(false);
     }
     if (ptr.attrIR.kind != IdKind.prockind)
     {
         ptr.EOFL = false;
     }
     ptr.next = null;
     if (ptr.attrIR.kind == IdKind.prockind)
     {
         int l = ptr.attrIR.level;
         if (l > maxLayer)
         {
             maxLayer = l;
         }
     }
     if (ptr.attrIR.kind == IdKind.varkind)
     {
         int l = ptr.attrIR.level;
         if (l > maxLayer)
         {
             maxLayer = l;
         }
     }
     if (this.front == null)
     {
         front = rear = ptr;
     }
     else
     {
         rear.next = ptr;
         rear      = rear.next;
     }
     return(true);
 }
Example #10
0
	/************/
	/**过程声明部分**/
	/************/
	
	private void procDec( TreeNode ptr , int layer , bool mid  ) {
		TreeNode p ; 
		SymTableNode sym = new SymTableNode() ;
		ptr.childs[1].childs[0].symtPtr = sym ;
		
		sym.name = ptr.childs[1].childs[0].Data ;
		sym.next = new SymTableNode() ; 
		sym.attrIR.idtype = null ;
		sym.attrIR.kind = IdKind.prockind ;
		sym.attrIR.level = layer ;
		sym.attrIR.More.ProcAttr.Count.value = 0 ;
		sym.EOFL = true ;
		
		if( myScope.scope.Peek().front == null ) {
			myScope.scope.Peek().front = sym ;
			symTable.insertNode( sym ) ;
		}
		if( mid == false ) {
			symTable.insertNode( sym ) ;
		}
		else if( mid == true ) {
			SymTableNode s0 = myScope.GetRear() ;
			s0.EOFL = false ; 
			symTable.insertMid( s0 , sym ) ;
		}
		
		myScope.newLayer( null ) ;
		p = ptr.childs[3] ;
		paramList( p , layer , sym.attrIR.More.ProcAttr.Count , sym.attrIR.More.ProcAttr.param ) ;
		p = ptr.childs[6].childs[0] ;
		declarePart( p , layer , sym.attrIR.More.ProcAttr.Count ) ;
		p = ptr.childs[7].childs[0] ; 
		programBody( p ) ;
		
		myScope.DropLayer() ;
	} 
Example #11
0
	private void callStmRest( TreeNode ptr , SymTableNode sym ) {
		TreeNode p ; 
		TreeNode tar = new TreeNode() ;
		tar.IsTerminal=true ; tar.NonTerminal = nonTerminals.ActParamList  ;
		p = Search( ptr , tar , 0 ) ;
		if( p != null ) {
			if( sym.attrIR.kind.Equals( IdKind.prockind ) ) actParamList( p , sym.attrIR.More.ProcAttr.param ) ;
		}
		else {
			error( "语义错误:		" + sym.name + "并非过程标识符,而是" + sym.attrIR.kind + "类型" , p.childs[0].Line , p.childs[0].Row ) ;
		}
	}
Example #12
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 ) ;
		}
	}
Example #13
0
	/**************/
	/*****类型声明****/
	/**************/
	private void typeDecList( TreeNode ptr , int layer ) {
		TreeNode q ; 
		TreeNode tar = new TreeNode() ;
		if( ptr.ChildNum != 0 ) {
			SymTableNode p = new SymTableNode() ; 
			p.name = ptr.childs[0].childs[0].Data ;
			p.attrIR.kind = IdKind.typekind ;
			p.attrIR.level = layer ;
			p.next = new SymTableNode() ; 
			
			tar.IsTerminal=true ;  tar.NonTerminal =  nonTerminals.TypeDef  ;
			q = Search( ptr , tar , 0 ) ;
			if( q != null ) {
				typeDef( q , p.attrIR.idtype , layer ) ;
			}
            
			if( p.attrIR.idtype != null ) {	
				symTable.insertNode( p ) ;
				if( myScope.scope.Peek().front == null ) {
					ptr.symtPtr = p ;
					myScope.scope.Peek().front = p ; 
				}
			}
			else {
				String str ;
				str = "语义错误:		类型标识符" + p.name + "未定义" ;
				error( str , ptr.Line , ptr.Row ) ;
				p = null ; 
			}
			
		}
		
		tar.NonTerminal = nonTerminals.TypeDecMore ;
		q = Search( ptr , tar , 0 ) ;
		if( q != null ) {
			tar.NonTerminal = nonTerminals.TypeDecList ;
			q = Search( q , tar , 1 ) ;
			if( q != null ) typeDecList( q , layer ) ;
		}
	}
Example #14
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 ) ;
		}
	}
Example #15
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 ) ;
					}
				}
			}
		}
	}
Example #16
0
	private void invar( TreeNode ptr ) {
		if( ptr.ChildNum != 0 && ptr.childs[0].Terminal.Equals(LexType.ID) ) {
			SymTableNode sym = new SymTableNode() ;
			sym = myScope.FindID( ptr.childs[0].Data , true ) ;
			if( sym == null ) {
				String str ;
				str = "语义错误:		变量标识符" + ptr.childs[0].Data + "未定义" ;
				error( str , ptr.childs[0].Line , ptr.childs[0].Row ) ;
			}
			else if( sym.attrIR.kind.Equals( IdKind.varkind ) == false ) {
				error( "语义错误:		标识符" + ptr.childs[0].Data + "应为变量类型" , ptr.childs[0].Line , ptr.childs[0].Row ) ;
			}
			else ptr.childs[0].symtPtr = sym ;
		}
	}
Example #17
0
	private void otherStm( TreeNode ptr ) {
		TreeNode p , q , t ; 
		TreeNode tar = new TreeNode() ; tar.IsTerminal=true;
		
		SymTableNode sym = new SymTableNode() ; 
		sym = myScope.FindID( ptr.Data , true ) ;
		
		if( sym == null ) {
			String str ;
			tar.NonTerminal = nonTerminals.AssignmentRest ;
			p = Search( ptr.father.childs[1] , tar , 1 ) ;
			if( p != null ) str = "语义错误:		变量标识符" + ptr.Data + "未定义" ;
			else str = "语义错误:		过程标识符" + ptr.Data + "未定义" ;
			error( str , ptr.Line , ptr.Row ) ;
			return ;
		}
		else {
			ptr.symtPtr = sym ; 
			tar.NonTerminal = nonTerminals.AssignmentRest  ;
			q = Search( ptr.father.childs[1] , tar , 1 ) ;
			if( q != null ) {
				if( sym.attrIR.idtype.Kind.Equals( TypeKind.arrayTy ) ) {
					tar.IsTerminal = false ; tar.Terminal =  LexType.LMIDPAREN  ;
					t = Search( q.childs[0] , tar , 0 ) ;
					if( t == null ) {
						String str ; 
						str = "语法错误:		把数组" + ptr.Data + "当成标识符来使用了" ;
						error( str , ptr.Line , ptr.Row ) ;
					}
				}
				assignmentRest( q , sym ) ;
				return ; 
			}
			tar.NonTerminal = nonTerminals.CallStmRest  ;
			p = Search( ptr.father.childs[1] , tar , 1 ) ;
			
			if( p != null ) {
				callStmRest( p , sym ) ;
				return ; 
			}
		}
	}
Example #18
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 ) ;
			}
		}
	}
Example #19
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 ) ;
			}
		}
	}