?? asdasd.cpp
字號:
#include "stdio.h"
#include "stdlib.h"
#define MaxRuleNum 8
#define MaxVnNum 5
#define MaxVtNum 5
#define MaxStackDepth 20
#define MaxPLength 20
#define MaxStLength 50
/*-----------------main struct define-----------------*/
/*
聲明:非終結符序號 = 100 + Vn的下標
終結符序號 = Vn的下標
*/
/*++++++++++文法結構++++++++++*/
struct pRNode /*產生式右部結構*/
{
int rCursor; /*右部序號*/
struct pRNode *next;
};
struct pNode /*產生式結點結構*/
{
int lCursor; /*左部符號序號*/
int rLength; /*右部長度*/
/*注當rLength = 1 時,rCursor = -1為空產生式*/
struct pRNode *rHead; /*右部結點頭指針*/
};
char Vn[MaxVnNum + 1]; /*非終結符集*/
int vnNum;
char Vt[MaxVtNum + 1]; /*終結符集*/
int vtNum;
struct pNode P[MaxRuleNum]; /*產生式*/
int PNum; /*產生式實際個數*/
char buffer[MaxPLength + 1];
char ch; /*符號或string ch;*/
char st[MaxStLength]; /*要分析的符號串*/
/*++first and follow collect struct++*/
struct collectNode /*集合元素結點結構*/
{
int nVt; /*在終結符集中的下標*/
struct collectNode *next;
};
struct collectNode* first[MaxVnNum + 1]; /*first集*/
struct collectNode* follow[MaxVnNum + 1]; /*follow集*/
/*+++++++++++++analysis table struct++++++++++++++++*/
int analyseTable[MaxVnNum + 1][MaxVtNum + 1 + 1];
/*預測分析表存放為產生式的編號,+1用于存放結束符,多+1用于存放#(-1)*/
/*+++++++++++++++analysis stack struct++++++++++++++*/
int analyseStack[MaxStackDepth + 1]; /*分析棧*/
int topAnalyse; /*分析棧頂*/
/*int reverseStack[MaxStackDepth + 1]; /*顛倒順序棧*/
/*int topReverse; /*倒敘棧頂*/
/*------------------function declare---------------*/
void Init();/*初始化*/
int IndexCh(char ch);
/*返回Vn在Vn表中的位置+100、Vt在Vt表中的位置,-1表示未找到*/
void InputVt(); /*輸入終結符*/
void InputVn();/*輸入非終結符*/
void ShowChArray(char* collect, int num);/*輸出Vn或Vt的內容*/
void InputP();/*產生式輸入*/
bool CheckP(char * st);/*判斷產生式正確性*/
void First(int U);/*計算first集,U->xx...*/
void AddFirst(int U, int nCh); /*加入first集*/
bool HaveEmpty(int nVn); /*判斷first集中是否有空(-1)*/
void Follow(int V);/*計算follow集*/
void AddFollow(int V, int nCh, int kind);/*加入follow集,
kind = 0表加入follow集,kind = 1加入first集*/
void ShowCollect(struct collectNode **collect);/*輸出first或follow集*/
void FirstFollow();/*計算first和follow*/
void CreateAT();/*構造預測分析表*/
void ShowAT();/*輸出分析表*/
void Identify(char *st);/*主控程序,為操作方便*/
/*分析過程顯示操作為本行變換所用,與教程的顯示方式不同*/
void InitStack();/*初始化棧及符號串*/
void ShowStack();/*顯示符號棧中內容*/
void Pop();/*棧頂出棧*/
void Push(int r);/*使用產生式入棧操作*/
/********************************* LL1.CPP *************************************/
/*-----------------------------------------------*/
//#include "LL1.h"
/*-------------------main function--------------------*/
void main(void)
{
char todo,ch;
Init();
InputVn();
InputVt();
InputP();
getchar();
FirstFollow();
printf("所得first集為:");
ShowCollect(first);
printf("所得follow集為:");
ShowCollect(follow);
CreateAT();
ShowAT();
todo = 'y';
while('y' == todo)
{
printf("\n是否繼續進行句型分析?(y / n):");
todo = getchar();
while('y' != todo && 'n' != todo)
{
printf("\n(y / n)? ");
todo = getchar();
}
if('y' == todo)
{
int i;
InitStack();
printf("請輸入符號串(以#結束) : ");
ch = getchar();
i = 0;
while('#' != ch && i < MaxStLength)
{
if(' ' != ch && '\n' != ch)
{
st[i++] = ch;
}
ch = getchar();
}
if('#' == ch && i < MaxStLength)
{
st[i] = ch;
Identify(st);
}
else
printf("輸入出錯!\n");
}
}
getchar();
}
/*---------------function definition------------------*/
void Init()
{
int i,j;
vnNum = 0;
vtNum = 0;
PNum = 0;
for(i = 0; i <= MaxVnNum; i++)
Vn[i] = '\0';
for(i = 0; i <= MaxVtNum; i++)
Vt[i] = '\0';
for(i = 0; i < MaxRuleNum; i++)
{
P[i].lCursor = NULL;
P[i].rHead = NULL;
P[i].rLength = 0;
}
PNum = 0;
for(i = 0; i <= MaxPLength; i++)
buffer[i] = '\0';
for(i = 0; i < MaxVnNum; i++)
{
first[i] = NULL;
follow[i] = NULL;
}
for(i = 0; i <= MaxVnNum; i++)
{
for(j = 0; j <= MaxVnNum + 1; j++)
analyseTable[i][j] = -1;
}
}
/*返回Vn在Vn表中的位置+100、Vt在Vt表中的位置,-1表示未找到*/
int IndexCh(char ch)
{
int n;
n = 0; /*is Vn?*/
while(ch != Vn[n] && '\0' != Vn[n])
n++;
if('\0' != Vn[n])
return 100 + n;
n = 0; /*is Vt?*/
while(ch != Vt[n] && '\0' != Vt[n])
n++;
if('\0' != Vt[n])
return n;
return -1;
}
/*輸出Vn或Vt的內容*/
void ShowChArray(char* collect)
{
int k = 0;
while('\0' != collect[k])
{
printf(" %c ", collect[k++]);
}
printf("\n");
}
/*輸入非終結符*/
void InputVn()
{
int inErr = 1;
int n,k;
char ch;
while(inErr)
{
printf("\n請輸入所有的非終結符,注意:");
printf("請將開始符放在第一位,并以#號結束:\n");
ch = ' ';
n = 0;
/*初始化數組*/
while(n < MaxVnNum)
{
Vn[n++] = '\0';
}
n = 0;
while(('#' != ch) && (n < MaxVnNum))
{
if(' ' != ch && '\n' != ch && -1 == IndexCh(ch))
{
Vn[n++] = ch;
vnNum++;
}
ch = getchar();
}
Vn[n] = '#'; /*以“#”標志結束用于判斷長度是否合法*/
k = n; /*k用于記錄n以便改Vn[n]='\0'*/
if('#' != ch)
{
if( '#' != (ch = getchar()))
{
while('#' != (ch = getchar()))
;
printf("\n符號數目超過限制!\n");
inErr = 1;
continue;
}
}
/*正確性確認,正確則,執行下下面,否則重新輸入*/
Vn[k] = '\0';
ShowChArray(Vn);
ch = ' ';
while('y' != ch && 'n' != ch)
{
if('\n' != ch)
{
printf("輸入正確確認?(y/n):");
}
scanf("%c", &ch);
}
if('n' == ch)
{
printf("錄入錯誤重新輸入!\n");
inErr = 1;
}
else
{
inErr = 0;
}
}
}
/*輸入終結符*/
void InputVt()
{
int inErr = 1;
int n,k;
char ch;
while(inErr)
{
printf("\n請輸入所有的終結符,注意:");
printf("以#號結束:\n");
ch = ' ';
n = 0;
/*初始化數組*/
while(n < MaxVtNum)
{
Vt[n++] = '\0';
}
n = 0;
while(('#' != ch) && (n < MaxVtNum))
{
if(' ' != ch && '\n' != ch && -1 == IndexCh(ch))
{
Vt[n++] = ch;
vtNum++;
}
ch = getchar();
}
Vt[n] = '#'; /*以“#”標志結束*/
k = n; /*k用于記錄n以便改Vt[n]='\0'*/
if('#' != ch)
{
if( '#' != (ch = getchar()))
{
while('#' != (ch = getchar()))
;
printf("\n符號數目超過限制!\n");
inErr = 1;
continue;
}
}
/*正確性確認,正確則,執行下下面,否則重新輸入*/
Vt[k] = '\0';
ShowChArray(Vt);
ch = ' ';
while('y' != ch && 'n' != ch)
{
if('\n' != ch)
{
printf("輸入正確確認?(y/n):");
}
scanf("%c", &ch);
}
if('n' == ch)
{
printf("錄入錯誤重新輸入!\n");
inErr = 1;
}
else
{
inErr = 0;
}
}
}
/*產生式輸入*/
void InputP()
{
char ch;
int i = 0, n,num;
printf("請輸入文法產生式的個數:");
scanf("%d", &num);
PNum = num;
getchar(); /*消除回車符*/
printf("\n請輸入文法的%d個產生式,并以回車分隔每個產生式:", num);
printf("\n");
while(i < num)
{
printf("第%d個:", i);
/*初始化*/
for(n =0; n < MaxPLength; n++)
buffer[n] = '\0';
/*輸入產生式串*/
ch = ' ';
n = 0;
while('\n' != (ch = getchar()) && n < MaxPLength)
{
if(' ' != ch)
buffer[n++] = ch;
}
buffer[n] = '\0';
/* printf("%s", buffer);*/
if(CheckP(buffer))
{
/*填寫入產生式結構體*/
pRNode *pt, *qt;
P[i].lCursor = IndexCh(buffer[0]);
pt = (pRNode*)malloc(sizeof(pRNode));
pt->rCursor = IndexCh(buffer[3]);
pt->next = NULL;
P[i].rHead = pt;
n = 4;
while('\0' != buffer[n])
{
qt = (pRNode*)malloc(sizeof(pRNode));
qt->rCursor = IndexCh(buffer[n]);
qt->next = NULL;
pt->next = qt;
pt = qt;
n++;
}
P[i].rLength = n - 3;
i++;
/*調試時使用*/
}
else
printf("輸入符號含非法在成分,請重新輸入!\n");
}
}
/*判斷產生式正確性*/
bool CheckP(char * st)
{
int n;
if(100 > IndexCh(st[0]))
return false;
if('-' != st[1])
return false;
if('>' != st[2])
return false;
for(n = 3; '\0' != st[n]; n ++)
{
if(-1 == IndexCh(st[n]))
return false;
}
return true;
}
/*====================first & follow======================*/
/*計算first集,U->xx...*/
void First(int U)
{
int i,j;
for(i = 0; i < PNum; i++)
{
if(P[i].lCursor == U)
{
struct pRNode* pt;
pt = P[i].rHead;
j = 0;
while(j < P[i].rLength)
{
if(100 > pt->rCursor)
{
/*注:此處因編程出錯,使空產生式時
rlength同樣是1,故此處同樣可處理空產生式*/
AddFirst(U, pt->rCursor);
break;
}
else
{
if(NULL == first[pt->rCursor - 100])
{
First(pt->rCursor);
}
AddFirst(U, pt->rCursor);
if(!HaveEmpty(pt->rCursor))
{
break;
}
else
{
pt = pt->next;
}
}
j++;
}
if(j >= P[i].rLength) /*當產生式右部都能推出空時*/
AddFirst(U, -1);
}
}
}
/*加入first集*/
void AddFirst(int U, int nCh) /*當數值小于100時nCh為Vt*/
/*當處理非終結符時,AddFirst不添加空項(-1)*/
{
struct collectNode *pt, *qt;
int ch; /*用于處理Vn*/
pt = NULL;
qt = NULL;
if(nCh < 100)
{
pt = first[U - 100];
while(NULL != pt)
{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -