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

📄 mycompiler.cpp

📁 这可是我的倾情制作
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// MyCompiler.cpp : 定义控制台应用程序的入口点。
//
//#pragma once
#include "stdafx.h"

/*
Title:Lexer.c		Data:2004.10.30		Author:Dragon(Panglong)
Student ID:1023710510 	School:School of Software of HIT
Description:
	This programme is for my compiler course's experiment.This one
just to cumulative c-like language lexer.I use HashTable to save the
symboles which includes c-like lanuage resevered words and other identifiers.
I build two buffers which is devided by EOF.Each buffer has length of 128
bytes. There is also a LesBuffer to save the string identified by lexer.
    After lexer, I begin to write automatic computation for the LR(1) analyze 
table.U see, there is too much work for me to do. so there isn't enough time for
me to accomplish it perfectly. But now it can generate LR(1) analyze table based on
user-defined product list and then can save this table into file and load it back
from a specific file.Especially, when the computation is on, we need at least 80MB
space for about 100 product and generate about 700 states and their items output
(just all the state in analyzing process) is over 7MB. But after the first computation
if without conflicts, you can just save useful info into file, and load it back when 
it is needed. Under this way, there is only smaller than 5MB memory to finish ur 
compile task.
*/
#include <stdio.h>
#include <conio.h>
#include "TokeList.h"
#include <stdlib.h>
#include <math.h>


///*basic abstract data types used*/
//typedef union InfoPointer
//{
//	/*There should be a field pointing to ArrayList*/
//	pSymbolTableType FuncPoint;
//}InfoPointerType;

typedef struct SymbolTable * pSymbolTableType;

typedef struct SymbolElement
{
	struct SymbolElement * pNextOne;
	char * pInfo;
	int TokenNum;
	int Type;
	int Kind;
	int Offset;
	//InfoPointerType ChildPoint;			/*Link to specific info based on the attribute Kind*/
	union InfoPointer
	{
        pSymbolTableType FuncPoint;
	} ChildPoint;
} SymbolType, *pSymbolType;

typedef struct ParamNode
{
	pSymbolType ParamPoint;		/*Store the parameter's symbol item in its SymbolList*/
	struct ParamNode * NextPoint;	/*Link to next ParamPoint*/
} ParamNodeType,* pParamNodeType;

typedef struct SymbolTable
{
	struct SymbolTable * FatherPoint;		/*Link to its parent's point which include such function name's symbol*/
	pSymbolType SymbolList[SIZEOFSYMTABLE];	/*This table stores symbol of this function or global*/
	pParamNodeType ParamHead;			/*Store this functions' parameter list*/
	int MemOffset;						/*MemOffset Stores current memory allocation position*/
	int StmOffset;						/*StmOffset Stores this function's offset*/
    int ParamLength;					/*Identify the num of parameters of such function*/
	int TempCount;						/*Store the current Temporary variable index*/
} SymbolTableType ;

/*================================
Below is just for syntax lexer!!!
=================================*/
typedef struct ProductItem
{
	int * TokenList;					/*just a int array to store the token list's TokenNum*/
	int Length;							/*the num of Token in list above*/
	int HeadToken;                      /*left part of production*/
	int Position;                       /*position of G_ProductSet*/
	int FunID;							/*specify which fun to deal with it*/
	struct ProductItem * NextPoint;		/*point to next one to conduct a link*/
} ProductItemType, * pProductItemType;

typedef struct ProductSet
{
	pProductItemType *Product;		/*a pointer array to store ProductItem */
	int Length;						/*the num of how many ProductItem in array above*/
}ProductSetType, * pProductSetType;

typedef struct SyntaxVarItem
{
	pProductItemType ProductHead;	/*link list of product which is begin with this SyntaxVar*/
	int Length;						/*how many product which is began with this SyntaxVar*/
	pSymbolType BasicInfo;			/*link to the data which is inserted into SymbolTable when read from file*/
} SyntaxVarType,* pSyntaxVarType;

typedef struct SyntaxVarSet
{
	pSyntaxVarType * SyntaxVarList;	/*a Pointer Array to store the SyntaxVarItem */
	int Capacity;					/*This SyntaxVarList can save the maxium SyntaxVarItem*/
}SyntaxVarSetType,* pSyntaxVarSetType;

typedef struct Item
{
	int ProductID;				/*Identify Product through G_ProductSet index*/
	int Position;				/*statement the state of this item */
	int NextToken;				/*Identify Next Token behind this item */
	struct Item * NextPoint;	/*Link to Next Item*/
} ItemType, * pItemType;


typedef struct GotoNode
{
	int TokenNum;	/*it indicates for which node to go next*/
	int FuncNum;	/*0-Shift 1-Receive 2-Goto*/
	int ParaNum;	/*This acts as Paramter behind FuncNum above*/
	struct GotoNode * NextPoint	;	/*link to next one*/
} GotoNodeType,* pGotoNodeType;

typedef struct ItemSet
{
	pItemType ItemHead;		/*Link of Item for this set */
	int Length;				/*nuw of elements of the link above*/
	int ItemSetID;			/*to identify the ItemSet through array index*/
	pGotoNodeType GotoHead;	/* struct ItemSet * NextPoint;	Link to Next ItemSet */
	int GotoLength;			/*num of elements of the GotoNode */
} ItemSetType , *pItemSetType;

typedef struct ItemSetSet
{
	pItemSetType * ItemSet;	/*Link if ItemSet for this set set*/
	int Length;				/*num of set of the set set above */
	int Capacity;			/*this ItemSetSet can save the maxium ItemSet*/
} ItemSetSetType,* pItemSetSetType;

typedef struct FirstNode
{
	int TokenNum;					/*Token Num can be terminator or Non-terminator*/
	struct FirstNode * NextPoint;	/*link to next point*/
}FirstNodeType , * pFirstNodeType;

typedef struct FirstSet
{
	pFirstNodeType NodeHead;	/*head of link*/
	int IsHasE;					/*determine whether it has e for Non-terminator */
	int Length;					/*Length of link*/
}FirstSetType, * pFirstSetType;

typedef struct StmtNode			/*For backpath statment node which stores only the instrument index num*/
{
	int StmtNum;
	struct StmtNode * NextPoint;
}StmtNodeType,* pStmtNodeType;

typedef struct StmtList
{
	pStmtNodeType NodeHead;		/*Stores the head of the link*/
	pStmtNodeType NodeTail;		/*Stores the tail of the link*/
	int Length;					/*Stores the length of link*/
}StmtListType, * pStmtListType;

/*this type describes the double_stack node*/
typedef struct StackNode
{
	int StateNum;			/*this describes the state*/
	int TokenNum;			/*this describes the input token_num*/
	pSymbolType Place;		/*Store the pointer to temporary or id's Symbol position*/
	int Quad;				/*Sotre NextQuad after that syntax variable,but I simply don't know why it is called such a name...*/
	int Offset;				/*Now Reserved for Array dealer*/
	int Ndim;				/*Allso Reserved for Array dealer*/
	int Width;				/*Store the num of bits it will stay */
	int Type;				/*Specify which type it is,especially for the type syntax var*/
	pStmtListType TrueList;	/*Store true go to list,and ChainList for Non boolean expression*/
	pStmtListType FalseList;	/*Store false go to list*/
	struct StackNode * NextPoint;	/*just a link point to next one*/
} StackNodeType,* pStackNodeType;

/* double_stack head to save state and token */
typedef struct DoubleStack
{
	pStackNodeType StackHead;
	int Length;
} DoubleStackType,* pDoubleStackType;

typedef struct FourNode
{
	int InstrumentNum;			/*store Instrument num*/
    char FirstOne[32];	/*store its string name*/
	char SecondOne[32];	/*store its string name*/
	char TargetOne[32];	/*store its string name*/	
}FourNodeType,*pFourNodeType;

/*Store four node item in list*/
typedef struct FourNodeList
{
	int Capacity;					/*Largest num of current store*/
	int Length;						/*Actual length of list */
	pFourNodeType * NodeList;		/*Dynamic created list according to Capacity*/
}FourNodeListType,*pFourNodeListType;

/*for sematics, so a pointer to a pointer to a function*/
typedef void (** FunPointPointType)();	/*If a variable belongs to such a kind,this variable can be used to dynamically allocate memory to store pointers to fuction*/
typedef void (* FunPointType)();		/*this just a type which means a pointer to a function*/

/*pSymbolType 	SymbolTable[SIZEOFSYMTABLE];*/	/*211*/
pSymbolTableType G_SymbolTable = NULL;			/*Sotre Key words and unterminate token*/
pSymbolTableType CurSymbolTable = NULL;			/*Store Current fuction's symbol table*/
pSymbolTableType RootSymbolTable = NULL;		/*Store root symbol table*/
char 		InputBuffer[SIZEOFINPUTBUFFERA];	/*512*/
char 		LexBuffer[SIZEOFCHARBUFFER];		/*32*/
char 		FileName[20];						/*source file name*/
FILE 		* pfp,* errorfp;
int 		FileIsOpen=0,BufferPart=0,FileIsEnd=0,lexbegin=0,lexforward=0;
int			LineNO=0,ColNO=0,token0;			/*,BufferPosition=0;*/
long 		FilePosition=0,FileLength=0,token1;
double		token2;
int 		CurChar;							/*Current Char Coupled*/
pSymbolType	CurSymbol;							/*Latest used symbol for looked up or inserted*/

/*================================
Below is just for syntax lexer!!!
=================================*/
#define		BASEOFSYN	500
#define		BASESIZESS	200
#define		SIZEOFSYNTAXBUFFER	30
#define 	PFINISH		-10
#define		GSHIFT	0
#define		GRECEIVE	1
#define		GGOTO	2
#define		GACC   3
/*Look at TokenList.h at Line102 : KONG 353*/
int					SyntaxCounter;						/*Syntax encountered in Syntax description file*/
int					ProductCounter;						/*Product encountered in Syntax description file*/
int					CurPosOfSynBuf;						/*Current Position of Syntax Buffer*/
ProductSetType		G_ProductSet;						/*Global product set*/
SyntaxVarSetType	G_SyntaxVarSet;						/*Global syntax var set*/
int					SyntaxBuffer[SIZEOFSYNTAXBUFFER];	/*Buffer of identifying product*/
ItemSetSetType 		G_ItemSS;							/*Global ItemSetSet variable*/
pFirstSetType		TerminatorSet;						/*Stroe terminator used in file*/
pDoubleStackType 	StateTokenStack=NULL;				/* double state and token stack used to do syntax analyze */

/* Function prototype declaration */
void 			SetSourceFile();										/*Set Source File*/
int 			GetPartToInputBuffer();									/*Read Source File into the first / second Input Buffer*/
void 			BeginLex();												/*to cumuluate get all the words*/
int 			lexan();												/*Get Word from buffer->lex analyze*/
void			InitializeGlobalSymbolTable();							/*to initializejSymbolTable:put key words into it*/
void			DestructSymbolTable(pSymbolTableType SymbolTable_in);									/*Destruct a Symbol Table by parameter of the pointer*/
pSymbolTableType ConstructSymbolTable(pSymbolTableType FatherPoint_in);									/*Construct a Symbol Table dynamically and return the pointer back*/
pSymbolType 	InsertIntoSymbolTable(pSymbolTableType SymbolTable_in,char * inString,int TokenNum);	/*Add Element to Table*/
void			SetSymbolInfo(pSymbolType Symbol_in,int Type_in,int Kind_in,int Offset_in,pSymbolTableType SymbolTable_in);	/*Update a Symbol's other info like type , kind and so on*/
pSymbolType 	LookWordsUpSymbolTable(pSymbolTableType SymbolTable_in,char * inString);				/*to determine whether words input is stored in SymbolTable*/
void 			ShowSymbolTable(pSymbolTableType SymbolTable_in);										/*ShowSymbolTable*/
void 			FindSymbol();											/*Just to test Symbol Table's LookWordsUpSymbolTable*/
long 			FileSize(FILE *stream);									/*to get length of file */
void 			MoveToNextChar();										/*Move the CurChar to next*/
void 			error(char * inString);									/*Show Error Massage and Deal it*/
void 			GetIntoLexBufferFromInputBuffer();						/*Put Into Lex buffer from Input Buffer through LexForward,LexBegin */
int 			AbsoluteLength();										/*Get length of absolute used in InputBuffer*/

/*================================
Below is just for syntax lexer!!!
=================================*/
void			AddSyntaxVar(pSyntaxVarType NewSyntax);													/*Add a new syntax to G_SyntaxVarSet which is type of SyntaxVarSetType*/
void			InitSyntaxVarSet();																		/*just initialize the G_SyntaxVarSet to BASESIZESS = 200*/
int				AddProduct(int SyntaxVar_in,int FunID_in);												/*Just Add current of SyntaxBuffer[] to the product of SyntaxVar_in */
void			PrintAllProduct();																		/*Print out all productions*/
void			InitItemSetSet();																		/*Init the G_ItemSS*/
int				AddItemToSet(pItemSetType ItemSet_in,int ProductID,int Position,int NextToken);			/*add one item to item set return 0:failed 1:sucess*/
void			ComputeItemSS();																		/*Compute Item Set Set*/
void			ComputeClosure(pItemSetType ItemSet_in);												/*compute ItemSet_in's closure*/
pItemSetType	ConstructItemSet();																		/*Build up a new Item Set and return its point*/
int				DestructItemSet(pItemSetType ItemSet_in);												/*Destruct specific ItemSet0:fail(especially this ItemSet has been assigned ItemSetID ) 1:success*/
void			PrintItemSet(pItemSetType ItemSet_in);													/*Print ItemSet_in 's all items */
pFirstSetType	ComputeFirst(int * TokenList,int Length);												/*Compute the TokenList 's first set*/
pFirstSetType	ConstructFirstSet();																	/*Build up a new First Set*/
void			DestructFirstSet(pFirstSetType FirstSet_in);											/*Deconstruct the specific First Set*/
int				AddToFirstSet(pFirstSetType FirstSet_in,int info);										/*Add int info to FirstSet return: 0: failed 1:success*/
int				GetFromFirstSet(pFirstSetType FirstSet_in);												/*Get Top Info of queue and remove the top node return -1:no node */
void			PrintItemSetSet();																		/*!!!!Not Finished!!!*/			/*Print ItemSetSet_in's all items based on PrintItemSet() just above*/
void			AddItemSet(pItemSetType NewItemSet);													/*Add a new ItemSet to G_ItemSS which is type of ItemSetSet*/
pItemSetType	ComputeGoto(pItemSetType ItemSet_in, int TokenNum);										/*compute the Goto func*/
int				AddGotoNodeToItemSet(pItemSetType ItemSet_in, int TokenNum,int FuncNum,int ParaNum);	/*Add GotoNode to ItemSet's GotoHead*/
int				IsHasItemSet(pItemSetType ItemSet_in);													/*determine whether there has been already a ItemSet in G_ItemSS(ItemSetSetType) return -1:there is not >=0: there is*/
void			PrintAnalyzeTable();																	/* Show Analyze Table generated for LR(1) */
void			FPrintAnalyzeTable();																	/* Show Analyze Table generated for LR(1) into file */
void			FPrintItemSet(pItemSetType ItemSet_in,FILE *TempFp);									/*Print ItemSet_in 's all items into files*/
void			BeginCompile();																			/*Compile Language C Source File based on Analyze Table Generated by Product Source File*/
pDoubleStackType ConstructDoubleStack();																/*construct a DoubleStackType*/
pStackNodeType GetStackNode(int Depth_in,pDoubleStackType DoubleStack_in);								/*Get Specific Stack Node Below the Head Depth_in:how much is the num below current top.For instance 0:top 1:top->next .... DoubleStack_in: just the target DoubleStack. Especially when u got the target node, u should carefully deal with it. So I didn't provide other attribute manage methods for u. */
int				Push(int TokenNum_in,int StateNum_in,pDoubleStackType DoubleStack_in);					/*return  0:failed 1:success*/
int				MyPopStack(int Num_in,pDoubleStackType DoubleStack_in);									/*Pop the num point 0:fail there is not enough node to pop 1:success pop the Num_in node out */
int				GetTopState(pDoubleStackType DoubleStack_in);											/*get top state -1:failed >=0 success */
pGotoNodeType	FindGotoNode(int ItemSetID_in,int TokenNum_in);											/*find pGotoNodeType when its state of itemset ItemSetID_in meet the TokenNum_in return NULL: not found. pGotoNodeType: found*/
void			PrintProduct(int ProductID);															/*Print out specific production*/
void			PrintAnalyzeTable2(int ItemSetPosition);												/* Show Analyze Table generated for LR(1) */
void			PrintSimpleAnalyzeTable();																/*Don't print the ItemSet, the reason is that if the G_ItemSS is loaded from loadFile,it will not contain the ItemSet in it but just the GotoNode list.*/
void			FPrintSimpleAnalyzeTable();																/* Show Simple Analyze Table excluding Item list generated for LR(1) into file */
int				LoadItemSS();																			/*Load into Globle variable G_ItemSS from file containing GotoNode list and Item list*/
int				SaveItemSS();																			/*Save Globle variable G_ItemSS into file containing GotoNode list and Item list*/

/*================================
Below is just for Sematics Analysis
=================================*/

pFourNodeListType	G_InstrumentList=NULL;			/*Global variable to stores the four node list*/
FunPointType		G_FuncList[FUNCLISTSIZE];		/*Global array of pointers to sematics funtions*/
int					G_CurProductNum;				/*Current Product Num to be received (!!Not Func ID,FuncID is specified by user,but this G_CurProductNum is determined by compiler!!)*/
/*char 		LexBuffer[SIZEOFCHARBUFFER];	This one has be	*//*32*/
pStackNodeType		G_TempStackNode;				/*Global used temporary variable especially for sematics function*/
pParamNodeType		G_TempParamHead;			/*Store temporary parameter head*/
int					G_TempParamLength;

pStmtListType		ConstructStmtList(int StmtNum_in);							/*Construct Statement Num List with parameter StmtNum_in which indicates target statement num*/
void	DestructPartStmtList(pStmtListType StmtList_in);						/*Deconstruct Statement Num List [Partly]*/
void	DestructAllStmtList(pStmtListType StmtList_in);							/*Deconstruct Statement Num List [Toally]*/
void	MergeStmtList(pStmtListType TargetList_in,pStmtListType AddedList_in);	/*MergeStmtList merge two list together, the StmtList merged from this call is stored in the first pointer*/
void	BatchPatch(pStmtListType TargetList_in,int StmtNum_in);					/*Back patch the StmtList_in with StmtNum_in */
void	InstrToString(int InstNum);												/*get Instrument Characters List*/
void	FInstrToString(int InstNum,FILE outFile_in);							/*get Instrument Characters List into file*/
pFourNodeListType	ConstructFourNodeList();									/*Construct FourNodeList*/
void	DestructFourNodeList(pFourNodeListType NodeList_in);					/*Destruct FourNodeList*/
void	AddFourNode(pFourNodeListType NodeList_in,int InstrNum_in,char * First_in,char * Second_in,char * Target_in); /*Add FourNode into list*/
pSymbolType GenerateTempVar();													/*Generate a temporary var in CurSymbolTable and return it*/
void	PushParam(pParamNodeType ParamHead_in,pSymbolType Param_in);			/*Push a param into a specific param list*/
void	ShowInstrumentList(pFourNodeListType NodeList_in);						/*Show Instrument List*/
void	FShowInstrumentList(pFourNodeListType NodeList_in);						/*Show Instrument List into file*/

/*================================
Below is just for syntax lexer!!!
=================================*/
#include "ShowEnd.c"
#include "ShowEndF.c"
#include "ShowEnf2.c"


/*================================
Below is just for syntax lexer!!!
=================================*/
#include "ShowEnd2.c"

/*================================
Below is just for sematic analyzer!!!
=================================*/
#include "FuncList.cpp"
#include "FuncInit.cpp"


/*Main func*/
//void main(void)
int _tmain(int argc, _TCHAR* argv[])
{
	int ControlNum=-1;
	//InitializeSymbolTable();
	while(ControlNum)
	{
		//clrscr();
		printf("\n\n<<<=== Welcome to Dragon's Syntax Compiler.(MyProductIdentify)===>>>");
		printf("\n1.Open Expression Source File to Syntax Compiler");
		printf("\n2.Begin to Syntax Analyze");
		printf("\n3.List HashTable(SymbolTable) info");
		printf("\n4.Find a Symbol");
		printf("\n5.Show All Product Set By Order");
		printf("\n6.Compute Items Set Set( G ),now just one ItemSet");
		printf("\n7.Analyze Table");
		printf("\n8.Open Source File to Analyze");
		printf("\n9.Begin to Compile it");
		printf("\n13.Show Four Node");
		printf("\n14.Save Four Node List Into Table");
		printf("\n==Save & Load===\n10.Save G_ItemSS(Just Goto)");
		printf("\n11.Load G_ItemSS(Just Goto)");
		printf("\n12.Display Simple Analyze Table");
		printf("\n0.Exit\nPlease Choose:");
		scanf("%d",&ControlNum);
		switch(ControlNum)
		{
			case 1:
				InitializeGlobalSymbolTable();

⌨️ 快捷键说明

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