Esempio 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;
}
Esempio n. 2
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;


 }
Esempio n. 3
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;
}