void DesignExtractor::buildCFG(TNode currNode, vector<std::pair<int,int>> * graph){
	int nodeType = currNode.getNodeType();
	vector<int> children = currNode.getChildren();	
	vector<std::pair<int,int>>::iterator it = graph->begin();
	int currStmtNum = currNode.getStmtNumber();
	switch(nodeType){
		case PROCEDURE:
		{
			//go into statement list
			buildCFG(ast->getChild(currNode, 0), graph);
			//set end nodes
			setEndNode(graph);
			break;
		}
		case STMT_LST:
		{
			//for each node of stmtLst visit it
			for(int i = 0; i < children.size(); ++i){
				buildCFG(ast->getChild(currNode, i), graph);
			}
			break;
		}
		case CALL:
		case ASSIGN:
		{
			//first we check if we can find follows, otherwise find a parent's follows.
			STMT followStmtNum = follows->getFollows(1,currStmtNum);
			if(followStmtNum == -1) {
				graph->at(currStmtNum).first = findLoopback(currStmtNum);
				if(graph->at(currStmtNum).first == 0)
					graph->at(currStmtNum).first = findFollows(currStmtNum);
			}
			else{
				graph->at(currStmtNum).first = followStmtNum;
			}
			return;
			break;
		}
		case WHILE:
		{
			//connect to first stmt of my stmtLst
			STMT firstStmtNum = ast->getChild(ast->getChild(currNode, 1), 0).getStmtNumber();
			graph->at(currStmtNum).first = firstStmtNum;
			//first we check if we can find follows, otherwise find a parent's follows.
			STMT followStmtNum = follows->getFollows(1,currStmtNum);
			if(followStmtNum == -1) {
				graph->at(currStmtNum).second = findLoopback(currStmtNum);
				if(graph->at(currStmtNum).second == 0)
					graph->at(currStmtNum).second = findFollows(currStmtNum);
			}
			else{
				graph->at(currStmtNum).second = followStmtNum;
			}
			// call buildCFG again on the stmtLst
			buildCFG(ast->getChild(currNode, 1), graph);
			break;
		}
		case IF:
		{
			//connect to first stmt of then stmtLst
			TNode thenNode = ast->getChild(currNode, 1);
			STMT thenStmtNum = ast->getChild(thenNode, 0).getStmtNumber();
			graph->at(currStmtNum).first = thenStmtNum;
			//connect to first stmt of else stmtLst
			TNode elseNode = ast->getChild(currNode, 2);
			STMT elseStmtNum = ast->getChild(elseNode, 0).getStmtNumber();
			graph->at(currStmtNum).second = elseStmtNum;
			//call buildCFG of then
			buildCFG(thenNode, graph);
			//call buildCFG of else
			buildCFG(elseNode, graph);
			break;
		}
		default:
			break;
	}
}
Example #2
0
std::shared_ptr<GNode> NextExtractor::converseAST(shared_ptr<CFG> cfg,TNode* curTNode, shared_ptr<GNode> curGNodeParent, shared_ptr<stack<shared_ptr<GNode>>> level)
{
	if (curTNode == NULL){
		return curGNodeParent;
	}
	string nodeType = curTNode->getNodeType();
	
		//ignore procedure and stmtlst TNode
	if (nodeType == "STMTLST_NODE") {
		return converseAST(cfg,  curTNode->getChild(0),curGNodeParent,level);
							   }						   
	else if(nodeType=="PROC_NODE") {
		return converseAST(cfg, curTNode->getChild(0),curGNodeParent,level);
								 }
	else if(nodeType=="THEN_NODE"){
		return converseAST(cfg ,curTNode->getChild(0), curGNodeParent, level);
						   }
	else if(nodeType=="ELSE_NODE"){
		return converseAST(cfg ,curTNode->getChild(0), curGNodeParent, level);
						   }
	else if(nodeType=="ASSIGN_NODE"){
		shared_ptr<GNode> gNode = setlink(cfg, curTNode, curGNodeParent, level);
		return converseAST(cfg ,  curTNode->getRightSibling(), gNode, level);

							 }
	else if(nodeType=="CALL_NODE"){
		shared_ptr<GNode> gNode = setlink(cfg, curTNode, curGNodeParent, level);
		return converseAST(cfg , curTNode->getRightSibling(), gNode, level);
						   }

	else if(nodeType=="IF_NODE"){
		shared_ptr<GNode> gNode = setlink(cfg, curTNode, curGNodeParent, level);
		level->push(gNode);
		converseAST(cfg ,  curTNode->getChild(0), gNode, level); // converse then node
		gNode = converseAST(cfg, curTNode->getChild(0)->getRightSibling(), gNode, level); // converse else node
		nodeMap.insert(pair<int, shared_ptr<GNode>>(gNode->getStmtNum(), gNode));

		if(lastIfWhileStmtStack.size() > 0 && lastIfWhileStmtStack.top().second == curTNode){

			TNode* curTNodeParent =  curTNode->getParent();
			string curTNodeType = curTNode->getNodeType();

			//this is for while-if code
			if(curTNodeParent->getNodeType() == "STMTLST_NODE" && curTNodeParent->getParent()->getNodeType().compare("WHILE_NODE") == 0)
			{
				cfg->setNext(gNode, level->top());
				level->pop();
			}

			//this is for if-if-LEFT code
			else if(curTNodeParent->getNodeType() == "STMTLST_NODE" && curTNodeParent->getParent()->getData().compare("then") == 0)
			{
				cout << "test" << endl;
				shared_ptr<GNode> nextG = make_shared<GNode>(INT_MIN, "EMPTY");
				lastIfNode.insert(pair<int,shared_ptr<GNode>>(level->top()->getStmtNum(), nextG));
				firstIfNode.insert(pair<shared_ptr<GNode> ,int>(nextG , level->top()->getStmtNum()));
				cfg->setNext(gNode,nextG);
				level->pop();
				level->push(nextG);
			}

			//this is for if-if-RIGHT code
			else if(curTNodeParent->getNodeType() =="STMTLST_NODE" && curTNodeParent->getParent()->getData().compare("else") == 0)
			{
				shared_ptr<GNode> nextG = level->top();
				cfg->setNext(gNode,level->top());
				level->pop();
				lastIfWhileStmtStack.pop();
				return nextG;
			}

			
			lastIfWhileStmtStack.pop();
			return gNode;
		}
		else
		{
			return converseAST(cfg,curTNode->getRightSibling(),gNode,level);//recursive !! curG is the empty node
		}
						 }

	else if(nodeType=="WHILE_NODE")
		{
			shared_ptr<GNode> gNode = setlink(cfg, curTNode, curGNodeParent, level);
			level->push(gNode);
			converseAST(cfg, curTNode->getChild(0), gNode, level); //converse while node

			if(lastIfWhileStmtStack.size() > 0 && lastIfWhileStmtStack.top().second == curTNode){

				TNode* curTNodeParent = curTNode->getParent();
				string curTNodeType = curTNode->getNodeType();

				//this is for while-while
				if(curTNodeParent->getNodeType() == "STMTLST_NODE" && curTNodeParent->getParent()->getNodeType().compare("WHILE_NODE") == 0) 
				{
					cfg->setNext(gNode, level->top());
					level->pop();
				}

				//this is for if-while-LEFT code
				else if(curTNodeParent->getNodeType() == "STMTLST_NODE" && curTNodeParent->getParent()->getData().compare("then") == 0)
				{
					shared_ptr<GNode> nextG = make_shared<GNode>(INT_MIN, "EMPTY");
					lastIfNode.insert(pair<int,shared_ptr<GNode>>(level->top()->getStmtNum(), nextG));
					firstIfNode.insert(pair<shared_ptr<GNode>,int>(nextG , level->top()->getStmtNum()));
					cfg->setNext(gNode,nextG);
					level->pop();
					level->push(nextG);
				}

				//this is for if-while-RIGHT code
				else if(curTNodeParent->getNodeType() == "STMTLST_NODE" && curTNodeParent->getParent()->getData().compare("else") == 0)
				{
					shared_ptr<GNode> nextG = level->top();
					cfg->setNext(gNode,level->top());
					level->pop();
					lastIfWhileStmtStack.pop();
					return nextG;
				}

				lastIfWhileStmtStack.pop();
				return gNode;
			}
			else
			{
				return converseAST(cfg,curTNode->getRightSibling(),gNode,level);//recurTNodesive
			}
		}else{
	return NULL;
	}
}