语法分析器的源代码Word文件下载.docx
- 文档编号:6476075
- 上传时间:2023-05-06
- 格式:DOCX
- 页数:33
- 大小:18.82KB
语法分析器的源代码Word文件下载.docx
《语法分析器的源代码Word文件下载.docx》由会员分享,可在线阅读,更多相关《语法分析器的源代码Word文件下载.docx(33页珍藏版)》请在冰点文库上搜索。
unnifo[]firstfollow=newunnifo[20];
//放first和follow;
pronode[]proNode=newpronode[30];
//产生式
pronode[,]table1=newpronode[15,15];
//分析表
stringvnstr;
//非终结符
stringvtstr;
//终结符
stringkong;
//含有空的终结符
intlenvn;
//定义非终结符的个数
intlenwenfa;
//定义产生式的数目
publicvoidgetvnstr()//取非终结符函数
inti;
intlen=proNode.Length;
for(i=0;
i<
len;
i++)
if(vnstr==null)
vnstr+=proNode[i].leftch;
else
intlenth=vnstr.Length;
intj;
for(j=0;
j<
lenth;
j++)
if(proNode[i].leftch==vnstr[j])
break;
if(j==lenth)
publicvoidgetvtstr()//取终结符函数
boolflag;
lenwenfa;
intlenth=proNode[i].rightch.Length;
flag=true;
if(isvn(proNode[i].rightch[j]))
flag=false;
if(proNode[i].rightch[j]=='
?
'
)
if(kong==null)
kong+=proNode[i].leftch;
intk;
intleng=kong.Length;
for(k=0;
k<
leng;
k++)
if(proNode[i].rightch[j]==kong[k])
if(k==leng+1)
if(flag)
if(vtstr==null)
vtstr+=proNode[i].rightch[j];
inth;
intlenvt=vtstr.Length;
for(h=0;
h<
lenvt;
h++)
if(proNode[i].rightch[j]==vtstr[h])
if(h==lenvt)
publicvoidcreatefirf()//定义first和follow集函数
intlen=vnstr.Length;
firstfollow[i].ch=vnstr[i];
firstfollow[i].first="
"
;
firstfollow[i].follow="
publicvoidgetlenvn()//取终结符个数函数
lenvn=vnstr.Length;
publicBooleanisvn(charch)//比较是非终结符
stringvn;
vn=textBox1.Text;
vn.Length;
if(vn[i]==ch)
returntrue;
returnfalse;
publicBooleanisvt(charch)//比较是终结符函数
stringvt;
vt=textBox2.Text;
vt.Length;
if(vt[i]==ch)
publicstringaddchar(stringvtstrch,charch)//在字符串中加字符
boolflag=true;
inttemlen=vtstrch.Length;
for(inti=0;
temlen;
if(vtstrch[0]==ch)
vtstrch=vtstrch+ch;
returnvtstrch;
publicBooleaniskongch(charch)//判断字符为能推出空集的非终结符;
stringtemkong;
temkong=textBox3.Text;
temkong.Length;
if(ch==temkong[i])
publicstringaddchtoch(stringchstr,stringotherarr)//在字符串终结字符串函数
intotherlen=otherarr.Length;
for(intj=0;
otherlen;
inttmlen=chstr.Length;
tmlen;
if(chstr[i]==otherarr[j])
if(otherarr[j]!
='
chstr=chstr+otherarr[j].ToString();
returnchstr;
publicvoidfirst1()//求first集的第一步骤
lenvn;
if(firstfollow[i].ch==proNode[j].leftch)
intlenth=firstfollow[i].first.Length;
if(firstfollow[i].first[k]==proNode[j].rightch[0])
if(k==lenth)
firstfollow[i].first+=proNode[j].rightch[0].ToString();
publicvoidfirst2()//求first集的第二步骤
charch;
=lenth-1;
if(isvn(firstfollow[i].first[j])==true)//判断first集中是否有非终结符;
ch=firstfollow[i].first[j];
for(inth=0;
if(firstfollow[h].ch==ch)
firstfollow[i].first=firstfollow[i].first.Remove(j,1);
firstfollow[i].first=addchtoch(firstfollow[i].first,firstfollow[h].first);
lenth=firstfollow[i].first.Length;
j=0;
publicvoidfollow1()//求follow集的第一步
firstfollow[0].follow=addchar(firstfollow[0].follow,'
#'
);
//开始符要先加‘#’;
proNode[j].rightch.Length;
if(proNode[j].rightch[k]==firstfollow[i].ch)//判断右边的字符串中的非终结符有等于等于要求follow集的非终结符;
if(k+1==proNode[j].rightch.Length)//判断要求follow集的非终结符为右边字符串的最后一个字符;
intlenth2=firstfollow[i].follow.Length;
if(lenth2==0)
firstfollow[i].follow=firstfollow[i].follow+proNode[j].leftch;
//将该文法产生式的左边字符加到follow集中
for(intm=0;
m<
lenth2;
m++)
if(firstfollow[i].follow[m]!
=proNode[j].leftch)
if(k+1<
proNode[j].rightch.Length)//判断要求follow集的非终结符的在右边字符串中出现且不是在最后一个字符
charch=proNode[j].rightch[k+1];
if(isvt(ch))//判断是终结符
intlenth1=firstfollow[i].follow.Length;
if(lenth1==0)
firstfollow[i].follow=firstfollow[i].follow+ch;
firstfollow[i].follow=addchar(firstfollow[i].follow,ch);
if(isvn(ch))//判断为非终结符
if(!
iskongch(ch))//该非终结不能推出空集;
for(intn=0;
n<
n++)
if(firstfollow[n].ch==ch)
addchtoch(firstfollow[i].follow,firstfollow[n].first);
for(intw=0;
w<
w++)
if(firstfollow[w].ch==ch)
addchtoch(firstfollow[i].follow,firstfollow[w].first);
intlenth3=firstfollow[i].follow.Length;
for(intl=0;
l<
lenth3;
l++)
if(firstfollow[i].follow[l]!
firstfollow[i].follow+=proNode[j].leftch;
publicvoidfollow2()//求follow集的第二步
intlenth=firstfollow[i].follow.Length;
if(isvn(firstfollow[i].follow[j]))//判断follow集中是否有非终结符;
ch=firstfollow[i].follow[j];
for(intk=0;
if(firstfollow[k].ch==ch)
firstfollow[i].follow=firstfollow[i].follow.Remove(j,1);
firstfollow[i].follow=addchtoch(firstfollow[i].follow,firstfollow[k].follow);
lenth=firstfollow[i].follow.Length;
publicvoidselect()//求select集
if(isvt(proNode[i].rightch[0]))
intlenth=proNode[i].select.Length;
if(proNode[i].select[j]==proNode[i].rightch[0])
proNode[i].select+=proNode[i].rightch[0];
if(proNode[i].rightch[0]=='
)//集中'
?
来表示空集;
if(firstfollow[k].ch==proNode[i].leftch)
proNode[i].select=addchtoch(proNode[i].select,firstfollow[k].follow);
if(isvn(proNode[i].rightch[0]))
if(firstfollow[h].ch==proNode[i].rightch[0])
intlen=firstfollow[h].first.Length;
intm;
proNode[i].select=addchtoch(proNode[i].select,firstfollow[h].first);
for(m=0;
if(firstfollow[h].first[m]=='
intn;
for(n=0;
if(firstfollow[n].ch==proNode[i].leftch)
proNode[i].select=addchtoch(proNode[i].select,firstfollow[n].follow);
publicintgetvn(charch)//求字符在非终结中的位置
returni;
return-1;
publicintgetvt(charch)//求字符在终结符中的位置
if(vt[j]==ch)
returnj;
publicvoidcreate_table()//建立分析表
introw;
intcol;
intlensel=proNode[k].select.Length;
lensel;
row=getvn(proNode[k].leftch);
col=getvt(proNode[k].select[m]);
table1[row,col]=proNode[k];
publicvoidprtable(intm,intn)//输出表中每个元素
if(m!
=-1&
&
n!
=-1)
richTextBox1.AppendText(table1[m,n].leftch+table1[m,n].midch+table1[m,n].rightch);
publicvoidshtabel()//显示表的内容
richTextBox1.AppendText("
\t"
richTextBox1.AppendText(vt[i]+"
if(vt[k]=='
\n"
if(k==vt.Length)
#"
+"
richTextBox1.AppendText(vn[j]+"
prtable(j,n);
publicchargetfirst(stringstr)//取第一个字符
intlen;
len=str.Length;
if(len==0)
return('
'
ch=str[0];
returnch;
publicstringremfirst(stringstr1)//删除第一个字符
intlen=str1.Length;
return("
str1
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语法 分析器 源代码