資源簡介
C語言寫的語法分析器,對于給出的任意LL1文法,實現了求First集,Follow集,預測分析表的生成,分析棧分析句子的合法性。原創的

代碼片段和文件信息
#include?
#include?
#define?Stack_Size?50
char?proc[50][50];?/*產生式*/
char?start;???/*開始符號*/
char?non_ter[50];?/*非終結符*/
char?termin[50];??/*終結符*/
int?count;???/*產生式的個數*/
char?first[50][50];?/*first集*/
char?follow[50][50];/*follow集*/
char?M[10][10][50]={‘\0‘};?/*預測分析表*/
typedef?struct{
???char?elem[Stack_Size];
???int?top;
}SeqStack;
void?InitStack(SeqStack?*S){???/*初始化順序棧*/
???S->top?=?-1;
}
int?Push(SeqStack?*Schar?*x){??/*進棧*/
???if(S->top?==Stack_Size-1)
???return?0;
???S->top++;
???S->elem[S->top]=x;
???return?1;
}
int?Pop(SeqStack?*S){??/*出棧*/
??if(S->top==-1)
??return?0;
??else{
S->top--;
return?1;
??}
}
int?GetTop(SeqStack?*Schar?*x){??/*取棧頂元素*/
??if(S->top==-1)
??return?0;
??else{
??*x=S->elem[S->top];
??return?1;
??}
}
void?ShowStack1(SeqStack?*S){??/*顯示棧的字符,先輸出棧底元素*/
??char?c;
??int?i;
??for(i=0;i<=S->top;i++)
??printf(“%c“S->elem[i]);
}
void?ShowStack2(SeqStack?*S){??/*顯示棧的字符,先輸出棧頂元素*/
??char?c;
??int?i;
??for(i=S->top;i>=0;i--)
??printf(“%c“S->elem[i]);
}
int?iss(char?s[]char?cint?n){??/*判斷字符數組s中,是否有字符c,返回下標*/
???int?i;
???for(i=n;i ??????if(c==s[i])
??break;
???}
???if(i==strlen(s))
???return?-1;
???return?i;
}
void?removeEqqual(char?s[]){??/*除去字符數組中重復的元素*/
???char?str[50];
???int?ijm=0;
???for(i=0;i ???????str[m++]?=?s[i];
???for(j=0;j ???if(s[j]==str[m-1]){
???m--;???break;
???}
???}
???for(i=0;i ???s[i]=str[i];
???s[m]=‘\0‘;
}
void?init(){???/*初始化?產生式,開始符號,非終結符,終結符?*/
char?ch;
????FILE?*fp;?
????int?ijm;
????if((fp=fopen(“change.txt““r“))==NULL)
????{
????????printf(“cannot?open?the?file(source.txt)!“);
????????exit(0);
????}
????ch=fgetc(fp);
start=?ch;
????for(i?=0;ch!=EOF;i++){
count++;
j=0;
non_ter[i]=ch;
????????while(ch!=‘\n‘&&ch!=EOF){ ?
??????????proc[i][j++]?=?ch;
??????????ch?=?fgetc(fp);
????????}
????proc[i][j]=‘\0‘;
????ch?=?fgetc(fp);
????}
m=0;
????for(i=0;i for(j=3;j if(iss(non_terproc[i][j]0)==-1&&proc[i][j]!=‘$‘&&proc[i][j]!=‘|‘){
????termin[m++]?=?proc[i][j];
}
}
}
????removeEqqual(termin);
removeEqqual(non_ter);
????printf(“共有%d句文法:\n“count);
????for(i=0;i ???????printf(“\t%s\n“proc[i]);
printf(“終結符有:?“);
for(i=0;i printf(“%c??“termin[i]);
printf(“\n非終結符有:?“);
for(i=0;i printf(“%c??“non_ter[i]);
printf(“\n“);
}
void?firstSet(){???/*求?first?集*/
???int?ijnmqkp;
???int?flag=1;
???char?c1[50]c2[50]c3[50];
???for(i=strlen(non_ter)-1;i>=0;i--){??
???for(j=3;j ???m=0;?????????????????/*先記錄形如A->aB|的?a?*/
???????first[i][m++]?=?proc[i][3];
???q?=?iss(non_terproc[i][3]0);
???p=3;
???while(q!=-1&&iss(first[q]‘$‘0)!=-1){??/*若A->aB??first[a]中有$時,將B的first集加入A中*/
???first[i][m++]?=?proc[i][p+1];
???????p?=?iss(proc[i]‘|‘p+1);
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????.CA....????????58??2010-06-07?08:33??語法分析\change.txt
????.CA....????200789??2010-06-08?00:17??語法分析\Debug\test.exe
????.CA....????249920??2010-06-08?00:17??語法分析\Debug\test.ilk
????.CA....?????26284??2010-06-08?00:17??語法分析\Debug\test.obj
????.CA....????181604??2010-06-07?16:47??語法分析\Debug\test.pch
????.CA....????508928??2010-06-08?00:17??語法分析\Debug\test.pdb
????.CA....?????33792??2010-06-08?00:17??語法分析\Debug\vc60.idb
????.CA....?????53248??2010-06-08?00:17??語法分析\Debug\vc60.pdb
????.CA....????????38??2010-05-12?13:18??語法分析\source.txt
????.CA....??????8346??2010-06-07?21:50??語法分析\TEST.BAK
????.CA....??????9732??2010-06-08?00:17??語法分析\test.c
????.CA....??????3375??2010-06-08?00:17??語法分析\test.dsp
????.CA....???????533??2010-06-08?00:18??語法分析\test.dsw
????.CA....?????41984??2010-06-08?00:18??語法分析\test.ncb
????.CA....?????48640??2010-06-08?00:18??語法分析\test.opt
????.CA....??????5798??2010-06-08?00:17??語法分析\test.plg
????.C.D...?????????0??2010-06-08?00:21??語法分析\Debug
????.C.D...?????????0??2010-06-11?20:39??語法分析
-----------?---------??----------?-----??----
??????????????1373069????????????????????18
評論
共有 條評論