?? runtime.h
字號:
return 0;
else
return -1;
}
void runTime::GetWords()
{
fin2.open("words.txt"); //定義文件讀取指針
Layer=0;
SegmentIndex=-1;
VariantCount=0;
int state=0; //在一般情況下,state值為0時,讀到begin時層數需要向前滾
//但當在讀完program,procedure或是function后,讀到的第一個
//begin并不需要增加層數,故此時的state值為1
char TempWord[256]; //臨時變量,分別記錄當前單詞名稱和其值
int TempValue;
while(fin2>>TempWord)
{
if(strcmp(TempWord,"@")==0) continue; //對回車字符不加處理
if(strcmp(TempWord,"program")==0) //如果遇到了程序聲明符,則將頭信息記錄
{
fin2>>TempWord; //讀入 "id"
VariantCount++; //每當讀入id后都需要將計數器加1
Segment[VariantCount]=++SegmentIndex;
SegmentCount=SegmentIndex;
WordCount[SegmentIndex]=0; //單詞個數的初始化,頭信息不用記錄到單詞中
ParamentCount[SegmentIndex]=0; //準備記錄其參數的個數
fin2>>TempWord; //讀入 "("
fin2>>TempWord; //讀入 "id"或是")"
if(TempWord[0]!=')')
{
VariantCount++;
ParamentCount[SegmentIndex]++;
strcpy(Parament[SegmentIndex][ParamentCount[SegmentIndex]], VariantName[VariantCount]);
//將變量名稱存入參數信息中
fin2>>TempWord; //讀入","或是")"
while(TempWord[0]==',')
{
fin2>>TempWord; //讀入下一個參數"id"
VariantCount++;
ParamentCount[SegmentIndex]++;
strcpy(Parament[SegmentIndex][ParamentCount[SegmentIndex]], VariantName[VariantCount]);
//將變量名稱存入參數信息中
fin2>>TempWord; //讀入","或是")"
}
}
fin2>>TempWord; //讀入";"
state=1; //讓其后的第一個begin不進行操作
}
else if(strcmp(TempWord,"procedure")==0)//如果遇到了過程聲明符,則基本和program處理相似
{
Layer++;
fin2>>TempWord; //讀入 "id"
VariantCount++; //每當讀入id后都需要將計數器加1
Segment[VariantCount]=++SegmentIndex;
SegmentCount=SegmentIndex;
WordCount[SegmentIndex]=0; //單詞個數的初始化,頭信息不用記錄到單詞中
ParamentCount[SegmentIndex]=0; //準備記錄其參數的個數
fin2>>TempWord; //讀入 "("
fin2>>TempWord; //讀入 "id"或是")"
if(TempWord[0]!=')')
{
VariantCount++;
ParamentCount[SegmentIndex]++;
strcpy(Parament[SegmentIndex][ParamentCount[SegmentIndex]], VariantName[VariantCount]);
//將變量名稱存入參數信息中
fin2>>TempWord; //讀入","或是":"
while(TempWord[0]==',')
{
fin2>>TempWord; //讀入下一個參數"id"
VariantCount++;
ParamentCount[SegmentIndex]++;
strcpy(Parament[SegmentIndex][ParamentCount[SegmentIndex]], VariantName[VariantCount]);
//將變量名稱存入參數信息中
fin2>>TempWord; //讀入","或是":"
}
fin2>>TempWord>>TempValue; //讀入 "def x"
fin2>>TempWord; //讀入 ")"
}
fin2>>TempWord; //讀入";"
state=1;
}
else if(strcmp(TempWord,"function")==0) //如果遇到了函數聲明符,則記錄相應的信息
{
Layer++;
fin2>>TempWord; //讀入 "id"
VariantCount++; //每當讀入id后都需要將計數器加1
Segment[VariantCount]=++SegmentIndex;
SegmentCount=SegmentIndex;
WordCount[SegmentIndex]=0; //單詞個數的初始化,頭信息不用記錄到單詞中
ParamentCount[SegmentIndex]=0; //準備記錄其參數的個數
fin2>>TempWord; //讀入 "("
fin2>>TempWord; //讀入 "id"或是")"
if(TempWord[0]!=')')
{
VariantCount++;
ParamentCount[SegmentIndex]++;
strcpy(Parament[SegmentIndex][ParamentCount[SegmentIndex]], VariantName[VariantCount]);
//將變量名稱存入參數信息中
fin2>>TempWord; //讀入","或是":"
while(TempWord[0]==',')
{
fin2>>TempWord; //讀入下一個參數"id"
VariantCount++;
ParamentCount[SegmentIndex]++;
strcpy(Parament[SegmentIndex][ParamentCount[SegmentIndex]], VariantName[VariantCount]);
//將變量名稱存入參數信息中
fin2>>TempWord; //讀入","或是":"
}
fin2>>TempWord>>TempValue; //讀入 "def x"
fin2>>TempWord; //讀入 ")"
}
fin2>>TempWord; //讀入":"
fin2>>TempWord>>TempValue; //讀入"def x"
fin2>>TempWord; //讀入";"
state=1;
}
else if(strcmp(TempWord,"begin")==0)
{
if(state==0)
{
Layer++; //將所在層數增加
strcpy(Word[SegmentIndex][WordCount[SegmentIndex]],TempWord);
//并將其記錄到數組中
WordCount[SegmentIndex]++;
}
else
state=0;
if(Layer==0) //是主程序的開始
{
SegmentIndex=0;
}
}
else if(strcmp(TempWord,"end")==0)
{
Layer--; //將所在層數增加
//并在下面判斷是否已經到達最底線
if(Layer==0) //說明已經寫完一個函數或過程
{
fin1>>TempWord; //讀入每個函數或過程后面的";"
state=1; //此時如果后面讀到begin則不會被記錄
}
else if(Layer==-1) //已經到達文件的末尾
{
fin1>>TempWord; //讀入最后的"."
}
}
else
{
strcpy(Word[SegmentIndex][WordCount[SegmentIndex]],TempWord);
//如果是一般字符則將其送到數組中
if(
//如果遇到這些詞就將其后面所跟的屬性讀進來
(strcmp(TempWord,"number")==0)
||
(strcmp(TempWord,"op")==0)
||
(strcmp(TempWord,"def")==0)
||
(strcmp(TempWord,"relop")==0)
||
(strcmp(TempWord,"ids")==0)
)
fin2>>Value[SegmentIndex][WordCount[SegmentIndex]];
WordCount[SegmentIndex]++;
if(strcmp(TempWord,"id")==0) VariantCount++;
}
}
}
int runTime::Compile()
/*
主要模塊
需要完成的工作:1。檢查變量的作用域
2。從文件將信息獲取到變量
3。再將變量中信息按照執行順序存到目標文件
*/
{
Initialize();
if(CheckWords()!=0) return -1;
//下面的工作是從文件中讀取各個字符到變量中
GetWords();
return 0;
}
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -