⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mycompiler.cpp

📁 这可是我的倾情制作
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	pParamNodeType tempParamNode;
	result = (pSymbolTableType) malloc(sizeof(SymbolTableType));	/*make sure that type inside sizeof should be type not pointer*/
	tempParamNode = (pParamNodeType) malloc(sizeof(ParamNodeType));
	tempParamNode->NextPoint = NULL;
	tempParamNode->ParamPoint = NULL;
	result->ParamHead = tempParamNode;
	result->FatherPoint = FatherPoint_in;
	result->MemOffset = 0;
	result->StmOffset = -1;
	result->TempCount = 0;
	for(i = 0 ; i<SIZEOFSYMTABLE; i++)
		result->SymbolList[i] = NULL;
	return result;	
}

/*Destruct a Symbol Table by parameter of the pointer*/
void DestructSymbolTable(pSymbolTableType SymbolTable_in)
{
	int counter;
	pSymbolType tempSymbol;
	for(counter=0;counter<SIZEOFSYMTABLE;counter++)
	{
		while(SymbolTable_in->SymbolList[counter] != NULL)
		{
			tempSymbol = SymbolTable_in->SymbolList[counter];
			SymbolTable_in->SymbolList[counter] = tempSymbol->pNextOne;
			delete tempSymbol->pInfo;
			if(tempSymbol->Kind == TFUNC)
				DestructSymbolTable(tempSymbol->ChildPoint.FuncPoint);
			delete tempSymbol;
			tempSymbol = NULL;
		}
	}
}

/*Add Element to Table*/
pSymbolType InsertIntoSymbolTable(pSymbolTableType SymbolTable_in,char * inString,int TokenNum)
{
	int HashNum = hashpjw(inString);
	pSymbolType temSymbol;
	if((temSymbol = (pSymbolType) malloc (sizeof(SymbolType)))==NULL)
	{
		printf("\nCan't allocate mem!\n");
		getch();
		exit(1);
	}
	
	temSymbol->TokenNum = TokenNum;
	temSymbol->Kind = 0;
	temSymbol->Offset = -1;
	temSymbol->Type = 0;
	temSymbol->ChildPoint.FuncPoint = NULL;

	if(TokenNum == STRINGS)
	{
		temSymbol->pInfo = inString;
	}
	else/*Deal with TokenNum which isnot STRINGS*/
	{
		if((temSymbol->pInfo = (char *) malloc (sizeof(char)*strlen(inString)+1))==NULL)
		{
		printf("\nCan't allocate mem!\n");
		getch();
		exit(1);
		}
		strcpy(temSymbol->pInfo,inString);
	}/*End if(TokenNum == STRINGS) Else*/
	temSymbol->pNextOne = SymbolTable_in->SymbolList[HashNum];
	SymbolTable_in->SymbolList[HashNum] = temSymbol ;
	return temSymbol;
}/*End func InsertIntoSymbolTable*/

/*Update a Symbol's other info like type , kind and so on*/
void SetSymbolInfo(pSymbolType Symbol_in,int Type_in,int Kind_in,int Offset_in,pSymbolTableType SymbolTable_in)
{
	if(Symbol_in == NULL) return;
	Symbol_in->Type = Type_in;
	Symbol_in->Kind = Kind_in;
	Symbol_in->Offset = Offset_in;
	Symbol_in->ChildPoint.FuncPoint = SymbolTable_in;
}

/*
input  	:
object 	:to determine whether words input is stored in SymbolTable
return 	:-1 ->fail ,not found ;
	 >0 ->success ,the words have found,and return its position;
*/
pSymbolType LookWordsUpSymbolTable(pSymbolTableType SymbolTable_in,char * inString)
{
	int TempHashNum = hashpjw (inString);
	pSymbolType pTempST = SymbolTable_in->SymbolList[TempHashNum];
	while(pTempST !=NULL)
	{
		if(strcmp(pTempST->pInfo,inString)==0)
			return CurSymbol = pTempST;
			/*return TempHashNum;*/
		pTempST = pTempST->pNextOne;
	}/*End while*/
	return NULL;
}/*LookWordsUpSymbolTable func end*/

/*ShowSymbolTable*/
void ShowSymbolTable(pSymbolTableType SymbolTable_in)
{
	int counter;//counter2;
	pSymbolType pTempST;
	FILE * fp;
	if((fp=fopen("Result02.dat","wt"))==NULL)
	{
		printf("\nCan't open Result02.dat to write!");
		getch();
		return;
	}
	printf("\n All Symbol Elements:");
	for(counter = 0;counter <SIZEOFSYMTABLE;counter++)
	{
		printf("\n%3d :",counter);
		fprintf(fp,"\n%3d :",counter);
		pTempST = SymbolTable_in->SymbolList[counter];
		while(pTempST != NULL)
		{
			printf("<<Token:%3d|Info:%10s>>",pTempST->TokenNum,pTempST->pInfo);
			fprintf(fp,"<<Token:%3d|Info:%10s>>",pTempST->TokenNum,pTempST->pInfo);
			pTempST = pTempST->pNextOne;
		}
		printf("<NULL>");
		fprintf(fp,"<NULL>");

		if(counter % SHOWPAGESIZE == 0 &&counter !=0)
		{
			printf("\nPress Any Key to continue.");
			getch();
			//clrscr();
			printf("\n All Symbol Elements:");
		}
	}
	fclose(fp);
}/*End func ShowSymbolTable*/

/*Just to test Symbol Table's LookWordsUpSymbolTable*/
void FindSymbol()
{
	char Symbol[32];
	pSymbolType TempSymbol;
	printf("\nInput Symbol you want to look up:");
	scanf("%s",Symbol);
	TempSymbol = LookWordsUpSymbolTable(::G_SymbolTable,Symbol);
	if( TempSymbol == NULL)
		printf("There isn't such Symbol\nPress Any Key to Continue..");
	else
		printf("It is on position:%3d\nPress Any Key to Continue..",TempSymbol->TokenNum);

	getch();
}/*FindSymbol End*/

/*to get length of file */
long FileSize(FILE *stream)
{
   long curpos, length;

   curpos = ftell(stream);
   fseek(stream, 0L, SEEK_END);
   length = ftell(stream);
   fseek(stream, curpos, SEEK_SET);
   return length;
}/*FileSize func end*/

/*Move the CurChar to next*/
void MoveToNextChar()
{
	lexforward = (lexforward + 1)%SIZEOFINPUTBUFFERA;
	CurChar = InputBuffer[lexforward];
/*	printf("\nChar:%c,NUM:%d",CurChar,CurChar); */
	if(CurChar == EOF)
		switch(GetPartToInputBuffer())
		{
			case 0:
				printf("\nFail to Read Source File.Exit...");
				//getch();
				exit(0);
				break;
			case 1:
				printf("\nSuccesful to put SIZEOFINPUTBUFFER into buffer");
				//getch();
				MoveToNextChar();
				break;
			case 2:
				printf("\nFile has got to its end");
				//getch();
				break;
		}
	else ColNO ++;
}/*MovtToNextChar func end*/

/*Show Error Massage and Deal it*/
void error(char * inString)
{
	printf("\n%3d:%3d->>%s",LineNO,ColNO,inString);
	fprintf(errorfp,"\n%3d:%3d->>%s",LineNO,ColNO,inString);
}

/*Put Into Lex buffer from Input Buffer through LexForward,LexBegin */
void GetIntoLexBufferFromInputBuffer()
{
	if(lexbegin < lexforward)
	{
		if( ( lexforward < SIZEOFINPUTBUFFERH - 1 ) || (lexbegin > SIZEOFINPUTBUFFERH - 1) )
		{
			strncpy(LexBuffer,InputBuffer+lexbegin,lexforward - lexbegin);
			LexBuffer[lexforward - lexbegin ] ='\0';
		}
		else
		{
			strncpy(LexBuffer,InputBuffer+lexbegin,SIZEOFINPUTBUFFERH - 1 - lexbegin);
			strncpy(LexBuffer+SIZEOFINPUTBUFFERH - 1 - lexbegin,InputBuffer+SIZEOFINPUTBUFFERH,lexforward - SIZEOFINPUTBUFFERH);
			LexBuffer[lexforward - lexbegin - 1] = '\0';
		}
	}
	else
	{
		strncpy(LexBuffer,InputBuffer + lexbegin,SIZEOFINPUTBUFFERA - 1 - lexbegin);
		strncpy(LexBuffer+SIZEOFINPUTBUFFERA - 1 - lexbegin,InputBuffer,lexforward);
		LexBuffer[SIZEOFINPUTBUFFERA - 1 - lexbegin+lexforward] = '\0';
	}
}/*End void GetIntoLexBufferFromInputBuffer() Func */

/*Get length of absolute used in InputBuffer*/
int AbsoluteLength()
{
	if(lexbegin < lexforward)
	{
		if( ( lexforward < SIZEOFINPUTBUFFERH - 1 ) || (lexbegin > SIZEOFINPUTBUFFERH - 1) )
		{
			return(lexforward - lexbegin);
		}
		else
		{
			return lexforward - lexbegin - 1;
		}
	}
	else
	{
		return SIZEOFINPUTBUFFERA - 1 - lexbegin+lexforward;
	}
}/*Func int AbsoluteLength() end*/

/*================================
Below is just for syntax lexer!!!
=================================*/
/*just initialize the G_SyntaxVarSet to BASESIZESS = 200*/
void	InitSyntaxVarSet()
{
	SyntaxCounter = 0;
	ProductCounter = 0;
	CurPosOfSynBuf = 0;
	G_SyntaxVarSet.Capacity = BASESIZESS;
	G_SyntaxVarSet.SyntaxVarList = (pSyntaxVarType *) malloc(sizeof(pSyntaxVarType) * G_SyntaxVarSet.Capacity);		
}

/*Add a new syntax to G_SyntaxVarSet which is type of SyntaxVarSetType*/
void	AddSyntaxVar(pSyntaxVarType NewSyntax)
{
	int i;
	pSyntaxVarType * TempSet;
	G_SyntaxVarSet.SyntaxVarList[SyntaxCounter - 1] = NewSyntax;
	if(SyntaxCounter == G_SyntaxVarSet.Capacity)
	{
		G_SyntaxVarSet.Capacity += BASESIZESS;
		TempSet = G_SyntaxVarSet.SyntaxVarList;
	G_SyntaxVarSet.SyntaxVarList = (pSyntaxVarType *) malloc(sizeof(pSyntaxVarType) * G_SyntaxVarSet.Capacity);
		for(i=0 ; i < SyntaxCounter;i++)
		{
	    G_SyntaxVarSet.SyntaxVarList[i] = TempSet [i];
		}
		free(TempSet);
	}
}

/*Just Add current of SyntaxBuffer[] to the product of SyntaxVar_in
return 0:failed 1:success
*/
int AddProduct(int SyntaxVar_in,int FunID_in)
{
	int i;
	int Position = SyntaxVar_in - BASEOFSYN;
	pProductItemType TempProductItem ;

	TempProductItem = (pProductItemType) malloc(sizeof(ProductItemType));
	TempProductItem ->FunID = FunID_in;

	if(CurPosOfSynBuf==1)
	{
		TempProductItem->TokenList = (int *)malloc(sizeof(int));
		TempProductItem->TokenList[0] = KONG;
		TempProductItem->Length = 1;
	}
	else
	{
		TempProductItem->TokenList =(int *) malloc(sizeof(int) * (CurPosOfSynBuf - 1));
		for(i=0;i<CurPosOfSynBuf - 1; i++)
			TempProductItem->TokenList[i] = SyntaxBuffer[i];
		TempProductItem->Length = CurPosOfSynBuf - 1;
	}

    /*we can add some check on user's grammer spcifiaction,but i skiped it*/

	TempProductItem->NextPoint = G_SyntaxVarSet.SyntaxVarList[Position]->ProductHead;
	G_SyntaxVarSet.SyntaxVarList[Position]->ProductHead = TempProductItem;
	G_SyntaxVarSet.SyntaxVarList[Position]->Length++;
	TempProductItem->HeadToken = SyntaxVar_in;

	return 1;
}

/*Print out all productions*/
void	PrintAllProduct()
{
	pProductItemType TempProduct;
	int i,j;
	printf("\nProduct\n");
	for(i=0;i<ProductCounter;i++)
	{
		TempProduct = G_ProductSet.Product[i];
		printf("\n%3d:%s->",i,G_SyntaxVarSet.SyntaxVarList[TempProduct->HeadToken - BASEOFSYN]->BasicInfo->pInfo);
		for(j=0;j<TempProduct->Length;j++)
		{
			if(TempProduct->TokenList[j]<BASEOFSYN)
				ShowResult2(TempProduct->TokenList[j]);
			else
				printf("%s",G_SyntaxVarSet.SyntaxVarList[TempProduct->TokenList[j] - BASEOFSYN]->BasicInfo->pInfo);
		}
		printf("  FunID:%3d",TempProduct->FunID);
		printf("\n");
	}


}

/*Print out specific production*/
void	PrintProduct(int ProductID)
{
	int j;
	pProductItemType TempProduct;
	TempProduct = G_ProductSet.Product[ProductID];
		printf("\n%3d:%s->",ProductID,G_SyntaxVarSet.SyntaxVarList[TempProduct->HeadToken - BASEOFSYN]->BasicInfo->pInfo);
		for(j=0;j<TempProduct->Length;j++)
		{
			if(TempProduct->TokenList[j]<BASEOFSYN)
				ShowResult2(TempProduct->TokenList[j]);
			else
				printf("%s",G_SyntaxVarSet.SyntaxVarList[TempProduct->TokenList[j] - BASEOFSYN]->BasicInfo->pInfo);
		}
		printf("\n");
}

/*Init the G_ItemSS*/
void	InitItemSetSet()
{
//	pItemSetType TempItemSet;
	G_ItemSS.Length = 0;
	G_ItemSS.Capacity = BASESIZESS;
	G_ItemSS.ItemSet = (pItemSetType *) malloc(sizeof(pItemSetType)*BASESIZESS);
}

/*Add a new ItemSet to G_ItemSS which is type of ItemSetSet*/
void	AddItemSet(pItemSetType NewItemSet)
{
	int i;
	pItemSetType * TempItemSet;
	G_ItemSS.ItemSet[G_ItemSS.Length] = NewItemSet;
	NewItemSet->ItemSetID = G_ItemSS.Length;
	G_ItemSS.Length++;

	if(G_ItemSS.Length == G_ItemSS.Capacity)
	{
		G_ItemSS.Capacity += BASESIZESS;
		TempItemSet = G_ItemSS.ItemSet;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -