void CreateAT() {
int i;
struct pRNode *pt;
struct collectNode *ct; for(i = 0; i < PNum; i++) {
pt = P[i].rHead;
while(NULL != pt && HaveEmpty(pt->rCursor)) {
/*处理非终结符,当为终结符时,定含空为假跳出*/ ct = first[pt->rCursor - 100]; while(NULL != ct) {
if(-1 != ct->nVt)
analyseTable[P[i].lCursor - 100][ct->nVt] = i; ct = ct->next; }
pt = pt->next; }
if(NULL == pt) {
/*NULL == pt,说明xyz->,用到follow中的符号*/ ct = follow[P[i].lCursor - 100]; while(NULL != ct) {
if(-1 != ct->nVt)
analyseTable[P[i].lCursor - 100][ct->nVt] = i; else /*当含有#号时*/
analyseTable[P[i].lCursor - 100][vtNum] = i; ct = ct->next; } } else {
if(100 <= pt->rCursor) /*不含空的非终结符*/ {
ct = first[pt->rCursor - 100]; while(NULL != ct) {
analyseTable[P[i].lCursor - 100][ct->nVt] = i; ct = ct->next; } }
else /*终结符或者空*/ {
if(-1 == pt->rCursor) /*-1为空产生式时*/ {
ct = follow[P[i].lCursor - 100]; while(NULL != ct) {
if(-1 != ct->nVt)
analyseTable[P[i].lCursor - 100][ct->nVt] = i; else /*当含有#号时*/
analyseTable[P[i].lCursor - 100][vtNum] = i; ct = ct->next; } }
else /*为终结符*/ {
analyseTable[P[i].lCursor - 100][pt->rCursor] = i; } } } } }
/*输出分析表*/ void ShowAT() {
int i,j;
1. printf(\构造预测分析表如下:\\n\
printf(\
for(i = 0; i < vtNum; i++) {
printf(\ }
printf(\
2. printf(\
for(i = 0; i <= vtNum; i++) printf(\ printf(\
3. for(i = 0; i < vnNum; i++)
{
printf(\ for(j = 0; j <= vtNum; j++) {
if(-1 != analyseTable[i][j])
printf(\
else
printf(\ }
printf(\ } }
/*=================主控程序=====================*/ void Identify(char *st) {
int current,step,r; /*r表使用的产生式的序号*/ printf(\的分析过程:\\n\
printf(\步骤\\t分析符号栈\\t当前指示字符\\t使用产生式序号\\n\
step = 0;
current = 0; /*符号串指示器*/ printf(\ ShowStack();
printf(\4. while('#' != st[current])
{
if(100 > analyseStack[topAnalyse]) /*当为终结符时*/ {
if(analyseStack[topAnalyse] == IndexCh(st[current])) {
/*匹配出栈,指示器后移*/ Pop(); current++; step++;
printf(\ ShowStack();
printf(\出栈、后移\\n\ } else {
printf(\不匹配!\ printf(\此串不是此文法的句子!\\n\ return; } }
else /*当为非终结符时*/ {
r = analyseTable[analyseStack[topAnalyse] - 100][IndexCh(st[current])]; if(-1 != r) {
Push(r); /*产生式右部代替左部,指示器不移动*/ step++;
printf(\ ShowStack();
printf(\ } else {
printf(\无可用产生式,此串不是此文法的句子!\\n\ return; } } }
if('#' == st[current]) {
5. if(0 == topAnalyse && '#' == st[current])
{
step++;
printf(\ ShowStack();
printf(\分析成功!\\n\ printf(\是给定文法的句子!\\n\ } else
{ while(topAnalyse > 0)
{ if(100 > analyseStack[topAnalyse]) /*当为终结符时*/ { printf(\无可用产生式,此串不是此文法的句子!\\n\ return; } else
{ r = analyseTable[analyseStack[topAnalyse] - 100][vtNum]; if(-1 != r)
{ Push(r); /*产生式右部代替左部,指示器不移动*/ step++;
printf(\ ShowStack();
if(0 == topAnalyse && '#' == st[current])
{ printf(\分析成功!\\n\ printf(\是给定文法的句子!\\n\ }
else
printf(\ } else
{ printf(\无可用产生式,此串不是此文法的句子!\\n\
return; } } } } } }
/*初始化栈及符号串*/ void InitStack() { int i;
/*分析栈的初始化*/
for(i = 0; i < MaxStLength; i++) st[i] = '\\0';
analyseStack[0] = -1; /*#(-1)入栈*/ analyseStack[1] = 100; /*初始符入栈*/ topAnalyse = 1; }
/*显示符号栈中内容*/ void ShowStack() { int i;
for(i = 0; i <= topAnalyse; i++) { if(100 <= analyseStack[i])
printf(\ else
{ if(-1 != analyseStack[i])
printf(\ else
printf(\ } } }
/*栈顶出栈*/ void Pop()
{ topAnalyse--; }
/*使用产生式入栈操作*/ void Push(int r) { int i;
struct pRNode *pt; Pop();
pt = P[r].rHead;
if(-1 == pt->rCursor) /*为空产生式时*/ return;
topAnalyse += P[r].rLength;
for(i = 0; i < P[r].rLength; i++)