Ejemplo n.º 1
0
int* findForFollow(Hashtable H, Lexeme L) {
	int i, j;
	int* set;
	int *fset;
	fset = (int*)malloc(FIRST_SET_SIZE*sizeof(int));
	set = (int*)malloc(FOLLOW_SET_SIZE*sizeof(int));
	for (i=0; i<FIRST_SET_SIZE; ++i) {
		fset[i] = 0;
		set[i] = 0;
	}

	for (i=0; i<HASHTABLE_SIZE; ++i) {
		for (j=0; j<HPR; ++j) {
			Lexeme* l;
			l = H[i].nextLexeme[j];
			if (l == NULL) {
				continue;
			}
			do {
				if (strcmp(L.lexName, l->lexName) == 0) {
					//printf("%s\n", H[i].lexemeName);
					if (l->next != NULL) {
						fset = getFirstSet(fset, H, *(l->next));
						set = setUnion(set, fset);
					} else {
						if (strcmp(l->lexName, H[i].lexemeName) == 0)
						{}
						else {
							fset = getFollowSet(H, *(lexemize(H[i].lexemeName)));
							set = setUnion(set, fset);
						}
					}
				}
				l = l->next;
			} while (l != NULL);
		}
	}
	
	return set;
}
Ejemplo n.º 2
0
int* getfirstSet(int* firstSet, int firstsetsize, Hashtable H, Lexeme L, int hashtablesize)
{
	int *tset;
	tset = (int*)malloc(FIRST_SET_SIZE*sizeof(int));
	if (L.isTerminal == TRUE) {
		tset[0] = atoi(L.lexName);
		firstSet = setUnion(firstSet, tset);
		return firstSet;
	}
	int index;
	int j=0;
	
	Lexeme* temp;
	index = findinHashtable(H, L, hashtablesize);
	while(H[index].nextLexeme[j] != 0 && j<HPR)
	{
	 	temp = H[index].nextLexeme[j];
	 	if(temp->isTerminal==TRUE)
	 	{
	 		if(ifPresent(firstSet, atoi(temp->lexName), firstsetsize)==FALSE)
	 		{
		 		firstSet[fsc] = atoi(temp->lexName);
		 		fsc++;
		 		j++;
		 		continue; 
	 		}
	 		else continue;
	 	}
	 	else if(temp->isTerminal==FALSE)
	 	{
	 		firstSet = getFirstSet(firstSet, H, *temp);
	 		j++;
	 	}	
	}
	
	return firstSet;
}
Ejemplo n.º 3
0
void Parser::getFirstSet(string nonterm)
{
	set<string> productions = grammar[nonterm];
 	set<string> symbols;

	if(terminals.find(nonterm) != terminals.end())
			{
				symbols.insert(nonterm);
				firstSet[nonterm]=symbols;
				//cout<<"added "<<nonterm<<endl;
				return;

			}

	
   for (set<string>::iterator it=productions.begin(); it!=productions.end(); ++it)
	{
		if(strcmp((*it).c_str(),".e.")==0)
			{
				symbols.insert("e");
				continue;
			}

			//takes care of the case A -> .a.
			string term = (*it).substr(1,((*it).length())-2);

			if(terminals.find(term) != terminals.end())
			{

				symbols.insert(term);
				//cout<<"added "<<term<<endl;
				continue;

			}


			int i=0;
 			int len= (*it).size();
 			int prevdot = 0;
 			int seconddot;
			while(i<len)
 			{
 				//cout<<"i:"<<i<<endl;
 				string str= (*it);
 				//cout<<"string is:"<<str<<endl;

 				//cout<<"prevdot is:"<<prevdot<<endl;

 				//int seconddot = str.find(".",prevdot+1);
 				seconddot = getNextDotLocation(prevdot,str);
 				if(seconddot<0)
 					break;


 				//cout<<"second dot:"<<seconddot<<endl;
 				//cout<<"dotsplit"<<endl;


 				string s= getStringBetweenTwoDots(prevdot,seconddot, str);
 				//str.substr(prevdot+1,seconddot-prevdot-1);
 				//cout<<"split string is:"<<s<<endl;

				// check for terminals if found add and break
				if(terminals.find(s) != terminals.end())
				{
					symbols.insert(s);
					//cout<<"foudn in terminals so added: "<<s<<endl;
					break;
				}				
				

				//  first check if the firstset is already computed else compute it
				map<string, set<string> >::iterator mapit = firstSet.find(s);
				//cout<<"Searching for:"<<s<<"in already computed"<<endl;
				if(mapit == firstSet.end())
 					{
						getFirstSet(s);
	 				}	
	 				//cout<<"reached"<<endl;
 					set<string> prdfirstSet = firstSet[s];
 					//printSet(prdfirstSet);
 					/*debug
 					cout<<"Printing set "<<s<<endl;
 					printSet(prdfirstSet);
					/**/

 					for (set<string>::iterator it=prdfirstSet.begin(); it!=prdfirstSet.end(); ++it)
					  {
					  	symbols.insert(*it);
					  }

 					set<string>::iterator setit= prdfirstSet.find("e");
 					// check if the prdtnfirstset has 'e'
 					if(setit == prdfirstSet.end())
					{
 						break;
					 }
 					i++;
 					prevdot = seconddot;
					symbols.insert("e");	
					//cout<<"added e to symbols"<<endl;

			}
	}

	firstSet[nonterm]=symbols;

 }
Ejemplo n.º 4
0
void Parser::start()
 {

 	/*
 	check for left recursion and remove it
 	*/

 	/* loop through the grammar to find firtsets for all the nonterminals */
 	for(map<string, set<string> >::iterator iter = grammar.begin(); iter != grammar.end(); iter++ ) 
	{
		//cout<<"calling "<<iter->first<<endl;
 		getFirstSet(iter->first);
 		//cout<<endl;
 	}

 	for(sit iter = terminals.begin(); iter != terminals.end(); iter++ ) 
	{
		//cout<<"calling "<<*iter<<endl;
 		getFirstSet(*iter);
 		//cout<<endl;
 	}
 	cout<<"Firstset is:"<<endl;
	printMap(firstSet);
	cout<<endl;

 	

 	for(map<string, set<string> >::iterator iter = grammar.begin(); iter != grammar.end(); iter++ ) 
	{
 		getFollowSet(iter->first);

 	}

 	
	/*for debuggin*/
	//cout<<"follow overlap printing"<<endl;
 	//printMap(followOverlap);

	for(pit iter = followOverlap.begin(); iter != followOverlap.end(); iter++ ) 
	{
		//cout<<"iter first"<<iter->first<<endl;
		//printSet(iter->second);
		/* check if iter is A check if followOverlap[something]=A so that you update follow(A) before adding everything 
		 in follw(A) to B */
		
		if((iter->second).empty())
			continue;

		//cout<<"fololow ovcerlap\n";

		//printMap(followOverlap);
		for(pit iter2 = followOverlap.begin(); iter2 != followOverlap.end(); iter2++ ) 
		{
			set<string> overlapset = followOverlap[(iter2)->first];

			// iterate through the set to check if iter is present i.e if followOverlap[C]=A
			for (sit iter4=overlapset.begin(); iter4!=overlapset.end(); ++iter4)
			{
				if((iter->first)==(*iter4))
				{
					// copy everything in follow of iter2 to follow of iter i.e from folow of C to A
					string temp= iter2->first;
					//cout<<"iter2:"<<iter2->first<<endl;
					//cout<<"iter:"<<iter->first<<endl;
					set<string> fromset= followSet[temp];
					// set<string> tempset;
					// for (std::set<string>::iterator iter3=fromset.begin(); iter3!=fromset.end(); ++iter3)
					// 	tempset.insert(*iter3);
					//cout<<"inserting"<<endl;
					//cout<<"iter first: "<<iter->first<<endl;
					//cout<<"fromset: ";
					//cout << "ser" <<endl;
					//pit it4=followSet.find(iter->first);
					pit iter10=followSet.find(iter->first);
					if(iter10!=followSet.end())
					iter10->second.insert(fromset.begin(), fromset.end());
					//cout << "iter->secondset" << endl;
					//printSet(iter10->second);
					//followSet.insert(std::pair<string, set<string> >(iter->first, fromset));
				}

			}

		}

		string temp2= iter->first;

		set<string> copyset= followSet[temp2];
		set<string> overlapset= followOverlap[temp2];		
		//cout << "copyset" << iter->first << endl;
		//printSet(copyset);

		// loop through overlapset of A and insert followset of A into each element
		for(sit iter4=overlapset.begin(); iter4!=overlapset.end(); ++iter4)
			{		
				pit it5=followSet.find(*iter4);
				//cout << "iter4 " <<  *iter4 << endl;

				//sit inserter = followSet[*iter4];
			/*if(it5!=followSet.end())
				printSet(it5->second);*/
			
				//cout <<  *iter4 << "Not found" << endl;
				if(it5!=followSet.end())
					(it5->second).insert(copyset.begin(), copyset.end());
				//followSet.insert(std::pair<string, set<string> >((*iter4), copyset));

					//cout<<"iter first: "<<iter->first<<endl;
					//cout<<"copyset: ";
					//printSet(copyset);
				//printSet(prodnfollowset);
			}

	}

	cout<<"Followset is:"<<endl;
	printFollowMap(followSet);
	cout<<endl;



	 return;


 }
Ejemplo n.º 5
0
int populateParseTable(int ParseTable[HASHTABLE_SIZE][STATES], Hashtable H, int terminal, Lexeme L)
{
	fsc = 0;
	int index; int i,k;
	index = findinHashtable(H, L, HASHTABLE_SIZE);
	Lexeme* temp;
	Lexeme* tt;
	Lexeme* lex;
	lex = lexemize(H[index].lexemeName);
	int* firstset;
	firstset = (int*)malloc(FIRST_SET_SIZE*sizeof(int));
	int* follset;
	follset = (int*)malloc(FOLLOW_SET_SIZE*sizeof(int));
	
	int found = 0;
	
//	printf("%d\n", terminal);
	for(i=0; H[index].nextLexeme[i]!=0; i++)
	{
		temp = H[index].nextLexeme[i];
		tt = H[index].nextLexeme[i];
		while(temp!=NULL)
		{
			firstset = (int*)malloc(FIRST_SET_SIZE*sizeof(int));
//			firstset = initwithZeroes(firstset, FIRST_SET_SIZE);
			firstset = getFirstSet(firstset, H, *temp);
			//printSet(firstset);
			if((ifPresent(firstset, terminal, FIRST_SET_SIZE))==TRUE && (ifPresent(firstset, 9999, FIRST_SET_SIZE)==FALSE))
			{
				found = 1;
				ParseTable[index][terminal] = i;
				//printf("1st case: no eps present!\n");
				return index;
				break;
			}
			else if((ifPresent(firstset, terminal, FIRST_SET_SIZE))==TRUE && (ifPresent(firstset, 9999, FIRST_SET_SIZE))==TRUE)
			{
				ParseTable[index][terminal] = i;
				//printf("2nd case: element and eps both present!\n");
//				temp=temp->next;
				return index;
				continue;
			}
			else if((ifPresent(firstset, terminal, FIRST_SET_SIZE))==FALSE && (ifPresent(firstset, 9999, FIRST_SET_SIZE))==TRUE)
			{
				temp=temp->next;
				//printf("3rd case: element not present but eps present!\n");
				continue;
			}
			else break;
		}
		if(temp==NULL && (ifPresent(firstset, 9999, FIRST_SET_SIZE))==TRUE)
		{
//			follset = initwithZeroes(follset, FOLLOW_SET_SIZE);
			follset = (int*)malloc(FIRST_SET_SIZE*sizeof(int));
			follset = getFollowSet(H, L);
			if (strcmp(tt->lexName, "9999") == 0) {
				for(k=0; k<FOLLOW_SET_SIZE; k++)
				{
					if(follset[k] == 9999 || follset[k]== 0 || ParseTable[index][follset[k]] != -999)
						continue;
					ParseTable[index][follset[k]] = i;
				}
			}
		}
	}
	
	return index;
}