📄 mycompiler.cpp
字号:
SetSourceFile();
getch();
break;
case 2:
BeginLex();
printf("\nFile Line No:%d , Column No:%d",LineNO,ColNO);
printf("\nI have finished BeginLex Func\nPress Any Key To Continue...");
getch();
break;
case 3:
ShowSymbolTable(::G_SymbolTable);
printf("\nPress Any Key to Continue...");
getch();
break;
case 4:
FindSymbol();
break;
case 5:
PrintAllProduct();
printf("\nPress Any key to continue..:)");
getch();
break;
case 6:
ComputeItemSS();
printf("\nPress Any key to continue..:)");
getch();
break;
case 7:
//PrintAnalyzeTable();
FPrintAnalyzeTable();
printf("\nPress Any key to continue..:)");
getch();
break;
case 8:
SetSourceFile();
printf("\nPress Any key to continue..:)");
getch();
break;
case 9:
BeginCompile();
printf("\nPress Any key to continue..:)");
getch();
break;
case 10:
SaveItemSS();
printf("\nPress Any key to continue..:)");
getch();
break;
case 11:
LoadItemSS();
printf("\nPress Any key to continue..:)");
getch();
break;
case 12:
PrintSimpleAnalyzeTable();
FPrintSimpleAnalyzeTable();
printf("\nPress Any key to continue..:)");
getch();
break;
case 13:
ShowInstrumentList(::G_InstrumentList);
printf("\nPress Any key to continue..:");
getch();
break;
case 14:
FShowInstrumentList(::G_InstrumentList);
printf("\nPress Any key to continue..:");
getch();
break;
case 0:
printf("Leaving now ......Press Any key to quit..:)");
getch();
}/*End switch*/
}/*end While*/
return 0;
}/*End of Main Func*/
/*Open product source file to build up anaylze table*/
void SetSourceFile()
{
printf("\nPlease Input your source file:");
scanf("%s",FileName);
if((pfp = fopen(FileName,"rt"))==NULL)
{
printf("Can't open the File %s !\nPress Any Key to Continue..",FileName);
getch();
return;
}
FileIsOpen = 1;
FilePosition = 0;
FileIsEnd = 0;
FileLength = FileSize(pfp);
fclose(pfp);
printf("\nYour File Info:\nFile Name:%s\tSize of File:%3d",FileName,FileLength);
}/*SetSourceFile End func*/
/*
put size of SIZEOFINPUTBUFFERH to half the InputBuffer;
return : 0->fail to open file ; 1->successful;2->File has been end;
*/
int GetPartToInputBuffer()
{
//int LengthToRead;
int counter;
if(FileIsEnd ==1) return 2;
if((pfp = fopen(FileName,"rt"))==NULL)return 0;
fseek(pfp,FilePosition,SEEK_SET);
/* if(SIZEOFINPUTBUFFERH-1>FileLength-FilePosition+1)
{
LengthToRead = FileLength-FilePosition+1;
FileIsEnd = 1;
}
else
{
LengthToRead = SIZEOFINPUTBUFFERH-1;
InputBuffer[(BufferPart+1)*SIZEOFINPUTBUFFERH-1]=EOS;
}
fread(&InputBuffer[BufferPart*SIZEOFINPUTBUFFERH],sizeof(char),LengthToRead,pfp);*/
for(counter = BufferPart*SIZEOFINPUTBUFFERH; counter < (BufferPart +1)*SIZEOFINPUTBUFFERH -1 && !feof(pfp);counter ++)
{
InputBuffer[counter] = fgetc(pfp);
}
if(feof(pfp)) FileIsEnd = 1;
else InputBuffer[counter] = EOF;
FilePosition = ftell(pfp);/*FilePosition + counter - BufferPart*SIZEOFINPUTBUFFERH;*/
BufferPart = !BufferPart;
/*lexforward = (lexforward +1)%SIZEOFINPUTBUFFERA;
CurChar = InputBuffer[lexforward];*/
fclose(pfp);
return 1;
}/*GetPartToInputBuffer func **/
void BeginLex()
{
FILE * fp;
pProductItemType TempProductItem;
int TempTokenNum=-1,TempTokenNum2,counter=0,counter2=0,tempFunID=-1;
/*int control;*/
lexbegin = 0; lexforward = 0; LineNO=0;ColNO=0;
if((fp = fopen("Result01.dat","wt"))==NULL)
{printf("\nCan't open Result01.dat to write");return;}
if((errorfp = fopen("Result03.dat","wt"))==NULL)
{printf("\nCan't open Result03.dat to write");return;}
GetPartToInputBuffer();
CurChar = InputBuffer[lexforward];
InitSyntaxVarSet();
TerminatorSet = ConstructFirstSet();
/*Set G_SymbolTable to CurrentSymbolTable,
So the lexan() func can put info into G_SymbolTable Through CurrentSymbolTable
*/
::CurSymbolTable = ::G_SymbolTable;
while((TempTokenNum = lexan())!=DONE){
tempFunID = ::token1;
TempTokenNum = lexan();
do{
TempTokenNum2 = lexan();
SyntaxBuffer[CurPosOfSynBuf++]=TempTokenNum2;
if(TempTokenNum2!=PFINISH && TempTokenNum2 < BASEOFSYN)
AddToFirstSet(TerminatorSet,TempTokenNum2); /*Add int info to FirstSet return: 0: failed 1:success*/
}while(TempTokenNum2!= PFINISH);
AddProduct(TempTokenNum,tempFunID);
ProductCounter ++;
CurPosOfSynBuf= 0;
}
//AddToFirstSet(TerminatorSet,KONG);
AddToFirstSet(TerminatorSet,DONE);
G_ProductSet.Length = ProductCounter;
G_ProductSet.Product = (pProductItemType *) malloc(sizeof(pProductItemType)*ProductCounter);
for(counter = 0 ;counter < SyntaxCounter;counter++)
{
TempProductItem = G_SyntaxVarSet.SyntaxVarList[counter]->ProductHead;
while(TempProductItem != NULL)
{
G_ProductSet.Product[counter2] = TempProductItem;
TempProductItem->Position = counter2;
counter2++;
TempProductItem = TempProductItem->NextPoint;
}
}
fclose(fp);
fclose(errorfp);
}/*BeginLex*/
/*Compile Language C Source File based on Analyze Table Generated by Product Source File*/
void BeginCompile()
{
FILE * fp;
int TempTokenNum=-1,counter=0,control=1,TopState,NextToken;
pGotoNodeType TempGotoNode;
pProductItemType TempProduct;
pStackNodeType TempStackNode;
char TempString[7];
lexbegin = 0; lexforward = 0; LineNO=0;ColNO=0;
FilePosition = 0;BufferPart = 0;
if((fp = fopen("Result01.dat","wt"))==NULL)
{printf("\nCan't open Result01.dat to write");return;}
if((errorfp = fopen("Result03.dat","wt"))==NULL)
{printf("\nCan't open Result03.dat to write");return;}
GetPartToInputBuffer();
CurChar = InputBuffer[lexforward];
/*Begin to analyze based on analyze table built up based on product */
::StateTokenStack = ConstructDoubleStack();
/*
Construct new Symbol Table,because this position is for the whole compile
root symboltable should be constructed here and assigned to CurSymbolTable
for the rest compilation. If in the rest part compiler identify function
definition, it will construct new SymbolTable added to its name's item in
RootSymbolTable and linked to parent, then change CurSymbolTable to it and
the rest work will be accomplished inside the new SymbolTable until it meet
the end of the function definition and returned back to RootSymbolTable.
The rest work is just iteration of these above.
*/
::RootSymbolTable = ::ConstructSymbolTable(NULL);
::CurSymbolTable = ::RootSymbolTable;
/*
We also need FourNodeList(G_InstrumentList) initialization
*/
::G_InstrumentList = ::ConstructFourNodeList();
/*Initialize Function Pointer List*/
::FuncListInitialization();
Push(DONE,0,StateTokenStack);
NextToken = lexan();
while(1)
{
TopState = GetTopState(StateTokenStack);
TempGotoNode = FindGotoNode(TopState,NextToken);
switch( TempGotoNode->FuncNum )
{
case GSHIFT:
Push(NextToken , TempGotoNode->ParaNum , StateTokenStack);
/*Specific for ID,NUM1,NUM2,STRINGS,CHARONE*/
TempStackNode = ::GetStackNode(0,StateTokenStack);
switch(NextToken)
{
case ID:
TempStackNode->Place = ::CurSymbol;
break;
case NUM1:
/*Generate a temporary variable place to hold Long Constant 16b */
TempStackNode->Place = ::GenerateTempVar();
::SetSymbolInfo(TempStackNode->Place,TINT,TCOMMON,::CurSymbolTable->MemOffset,NULL);
::CurSymbolTable->MemOffset += 2;
sprintf(TempString,"%d",::token1);
::AddFourNode(::G_InstrumentList,IASIGN,TempString,NULL,TempStackNode->Place->pInfo);
break;
case NUM2:
/*Generate a temporary variable place to hold Long Constant 32b */
TempStackNode->Place = ::GenerateTempVar();
::SetSymbolInfo(TempStackNode->Place,TFLOAT,TCOMMON,::CurSymbolTable->MemOffset,NULL);
::CurSymbolTable->MemOffset += 4;
sprintf(TempString,"%.2f",::token1);
::AddFourNode(::G_InstrumentList,IASIGN,TempString,NULL,TempStackNode->Place->pInfo);
break;
case STRINGS:
TempStackNode->Place = ::GenerateTempVar();
::SetSymbolInfo(TempStackNode->Place,TCHAR,TCOMMON,::CurSymbolTable->MemOffset,NULL);
::CurSymbolTable->MemOffset += strlen(::CurSymbol->pInfo);
::AddFourNode(::G_InstrumentList,IASIGN,::CurSymbol->pInfo,NULL,TempStackNode->Place->pInfo);
break;
case CHARONE:
TempStackNode->Place = ::GenerateTempVar();
::SetSymbolInfo(TempStackNode->Place,TCHAR,TCOMMON,::CurSymbolTable->MemOffset,NULL);
::CurSymbolTable->MemOffset += 1;
sprintf(TempString,"%c",::token1);
::AddFourNode(::G_InstrumentList,IASIGN,TempString,NULL,TempStackNode->Place->pInfo);
break;
}
NextToken = lexan();
break;
case GRECEIVE:
/*Transport the current product num to sematics method with global variable*/
::G_CurProductNum = TempGotoNode->ParaNum;
TempProduct = G_ProductSet.Product[TempGotoNode->ParaNum];
/*
We need to call sematics function when identify the receive but before
push and pop data.
*/
/*
This G_FuncList is just a arry of pointers to sematics function,
when we recognize this product, we have read user-defined FuncID,
and at this BeginCompile beginning part, we have run FuncListInitialization() to
init this func list.
*/
::G_FuncList[TempProduct->FunID]();
//
//
//if(TempProduct->TokenList[0]!=KONG)
// MyPopStack(TempProduct->Length,StateTokenStack);
//TopState = GetTopState(StateTokenStack);
//TempGotoNode = FindGotoNode(TopState,TempProduct->HeadToken);
//Push(TempProduct->HeadToken,TempGotoNode->ParaNum,StateTokenStack);
//PrintProduct(TempProduct->Position);
break;
case GACC:
printf("\nAcc\n");
getch();
return ;
break;
default:
printf("\nError \n");
getch();
break;
}
}
// while((TempTokenNum=lexan())!=-1&&TempTokenNum!=DONE&&control!='0')
// {
// #define GSHIFT 0
//#define GRECEIVE 1
//#define GGOTO 2
//#define GACC 3
//
// counter ++;
// printf("\n%3dth:",counter);
// fprintf(fp,"\n%3dth:",counter);
// ShowResult2(TempTokenNum);
// FShowResult2(TempTokenNum,fp);
// if(counter%30 ==0) control=getch();
// }
// counter ++;
// printf("\n%3dth:",counter);
// fprintf(fp,"\n%3dth:",counter);
// ShowResult2(TempTokenNum);
// FShowResult2(TempTokenNum,fp);
fclose(fp);
fclose(errorfp);
}
/*
input:nothing
object:
this is the most important code for this experiment.this is just
the begin to analyze the input buffer which is input from SourceFile;
return
-1:Fail to Read Buffer because buffer failed to read source file
*/
int lexan()
{
int MidIntUse=0,state = 1,Counter;
//int MidIntUse2;
// long IntResult;
//float FloatResult;
float MidFloatUse=1;
char * endptr, * temptr;
pSymbolType TempSymbol;
pSyntaxVarType TempSyntaxVar;
while(state)
{
switch(state){
case 1:
if(CurChar == ' ' || CurChar == '\t')
{
MoveToNextChar();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -