当前位置: 首页>>代码示例>>C++>>正文


C++ AST::getAncestor方法代码示例

本文整理汇总了C++中AST::getAncestor方法的典型用法代码示例。如果您正苦于以下问题:C++ AST::getAncestor方法的具体用法?C++ AST::getAncestor怎么用?C++ AST::getAncestor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在AST的用法示例。


在下文中一共展示了AST::getAncestor方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: getIsSibling

bool EvaluateSibling::getIsSibling(TYPE type1,TYPE type2,INDEX index1,INDEX index2){
	bool returnValue = false;
	
	if(type1 ==PROCEDURE&&type2 ==PROCEDURE){ //1. Sibling("procName1","procName2"
		if(index1!=NULL&&index2!=NULL) returnValue = true;	
	}
	if(Helper::isStatement(type1)&&Helper::isStatement(type2)){//2. Sibling(1,5),Sibling(4,2) for stmt/w/a/if/call
		if((type1==STATEMENT||Helper::isStatementTypeOf(type1,index1))&&(type2==STATEMENT||Helper::isStatementTypeOf(type2,index2))){
			returnValue = (EvaluateFollows::getIsFollowsStarResult(index1,index2)||EvaluateFollows::getIsFollowsStarResult(index2,index1));
		}
	}
	if(type1==VARIABLE&&(type2==STMT_LIST||type2==STATEMENT)){//3. a.Sibling("x",4) for while or if stmt,4 is stmtList
		AST * stmtListNode =NULL;
		AST_LIST * nodeList = pkb->getASTBy(index2);
		for(AST_LIST::iterator astItr=nodeList->begin();astItr!=nodeList->end();astItr++){
			if((**astItr).getRootType()==STMT_LIST){
				stmtListNode = *astItr;
				break;
			}
		} 
		if(stmtListNode!=NULL){
			AST * parentNode = stmtListNode->getAncestor();		//get the parentNode to check if it is while or if
			AST * varNode = parentNode->getFirstDescendant();	//get the varNode of while or if

			if(varNode->getRootType() ==VARIABLE&&varNode->getRootData()==index1)
				returnValue= true;	
		}
	}
	if(type2==VARIABLE&&(type1==STMT_LIST||type1==STATEMENT)){ //3. b.Sibling(4,"x") for while of if stmt
		AST * stmtListNode = NULL;
		AST_LIST * nodeList = pkb->getASTBy(index1);
		for(AST_LIST::iterator astItr=nodeList->begin();astItr!=nodeList->end();astItr++){
			if((**astItr).getRootType()==STMT_LIST){
				stmtListNode = *astItr;
				break;
			}
		} 
		if(stmtListNode!=NULL){
			AST * parentNode = stmtListNode->getAncestor();		//get the parentNode to check if it is while or if
			AST * varNode = parentNode->getFirstDescendant();	//get the varNode of while or if

			if(varNode->getRootType() ==VARIABLE&&varNode->getRootData()==index2)
				returnValue = true;
		}
		
	}
	if((type1==STMT_LIST||type1==STATEMENT)&&(type2==STMT_LIST||type2==STATEMENT)){//3. c. Sibling( 22,23) for stmtList of if
		AST * stmtListNode1 =NULL;
		AST_LIST * nodeList = pkb->getASTBy(index1);
		for(AST_LIST::iterator astItr=nodeList->begin();astItr!=nodeList->end();astItr++){
			if((**astItr).getRootType()==STMT_LIST){
				stmtListNode1 = *astItr;
				break;
			}
		} 
		AST * stmtListNode2=NULL;
		nodeList = pkb->getASTBy(index2);
		for(AST_LIST::iterator astItr=nodeList->begin();astItr!=nodeList->end();astItr++){
			if((**astItr).getRootType()==STMT_LIST){
				stmtListNode2 = *astItr;
				break;
			}
		} 
		if(stmtListNode1!=NULL&&stmtListNode2!=NULL){
			if(stmtListNode1->getAncestor()->getRootData()==stmtListNode2->getAncestor()->getRootData())
				returnValue= true;
		}
	}
	if(Helper::isExprElement(type1)&&Helper::isExprElement(type2)){//4. Sibling ("x"/"*"/"+"/"-","x"/"*"/"+"/"-"). operator data: -1
		DATA_LIST * assignList = pkb->getAllAssigns();
		
		for(DATA_LIST::iterator itr= assignList->begin();itr!=assignList->end();itr++){
			//get assignNode
			AST * assignNode = NULL;
			AST_LIST * nodeList = pkb->getASTBy(*itr);
			for(AST_LIST::iterator astItr=nodeList->begin();astItr!=nodeList->end();astItr++){
				if((**astItr).getRootType()==ASSIGNMENT){
					assignNode = *astItr;
					break;
				}
			} 
			//check if modified var and expression tree root are sibling
			AST * leftVarNode = assignNode->getFirstDescendant();
			AST * expressionRoot = leftVarNode->getRightSibling();
			
			int exprData = expressionRoot->getRootData();
			if(expressionRoot->getRootType()==CONSTANT) exprData = pkb->getConstantValue(exprData);

			//pattern is symmetric, find if x is matched with some elements in x=y+z+...
			bool isPattern1 = (leftVarNode->getRootType()==type1&&leftVarNode->getRootData()==index1)&&(expressionRoot->getRootType()==type2&&exprData==index2);
			bool isPattern2 =(expressionRoot->getRootType()==type1&&exprData==index1)&&(leftVarNode->getRootType()==type2&&leftVarNode->getRootData()==index2);
			if(isPattern1||isPattern2) 
				return true; // x = expression, x is matched with var or +,-,*

			//traverse the tree and check matched pattern, search in left hand side x= y+z...
			stack<AST *> exprStack;
			if(expressionRoot->getRootType()!=VARIABLE&&expressionRoot->getRootType()!=CONSTANT){
				exprStack.push(expressionRoot);
				AST * left;
				AST * right;
//.........这里部分代码省略.........
开发者ID:benchisa,项目名称:BasicProgram,代码行数:101,代码来源:EvaluateSibling.cpp


注:本文中的AST::getAncestor方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。