SLR1文法分析实验报告Word文档下载推荐.docx
- 文档编号:17008963
- 上传时间:2022-11-27
- 格式:DOCX
- 页数:28
- 大小:80.89KB
SLR1文法分析实验报告Word文档下载推荐.docx
《SLR1文法分析实验报告Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《SLR1文法分析实验报告Word文档下载推荐.docx(28页珍藏版)》请在冰豆网上搜索。
a)const
booloperator==(constWF&
voidprint()
};
classClosure
voidprint(stringstr)
booloperator==(constClosure&
voidmake_item()
voiddfs(conststring&
x)
voidmake_first()
voidappend(conststring&
str1,conststring&
str2)
bool_check(constvector<
int>
&
id,conststringstr)
voidmake_follow()
voidmake_set()
voidmake_V()
voidmake_cmp(vector<
WF>
cmp1,inti,charch)
voidmake_go()
voidmake_table()
voidprint(strings1,strings2,strings3,strings4,strings5,strings6,strings7)
stringget_steps(intx)
stringget_stk(vector<
T>
stk)
stringget_shift(WF&
temp)
voidanalyse(stringsrc)
算法思想
SLR文法构造分析表的主要思想:
许多冲突性的动作都可能通过考察有关非终结符的FOLLOW集而获解决。
解决冲突的方法:
解决冲突的方法是分析所有含A和B的句型,考察集合FOLLOW(A)和FOLLOW(B),如果这两个集合不相交,而且也不包含b,那么当状态I面临输入符号a时,我们可以使用如下策略:
若a=b,则移进。
若a∈FOLLOW(A),则用产生式A→α进行归约;
若a∈FOLLOW(B),则用产生式B→α进行归约;
此外,报错*SLR的基本算法:
假定LR(0)规范族的一个项目集I中含有m个移进项目
A1→α•a1β1,A2→α•a2β2,…,Am→α•amβm;
同时含有n个归约项目
B1→α•,B2→α•,…,B3→α•,
如果集合{a1,…,am},FOLLOW(B1),…,FOLLOW(Bn)两两不相交(包括不得有两个FOLLOW集合有#),则隐含在I中的动作冲突可以通过检查现行输入符号a属于上述n+1个集合中的哪个集合而活的解决:
若a是某个ai,i=1,2,…,m,则移进。
若a∈FOLLOW(Bi),i=1,2,…,m,则用产生式Bi→α进行归约;
此外,报错
这种冲突的解决方法叫做SLR
(1)解决办法。
SLR语法分析表的构造方法:
首先把G拓广为G’,对G’构造LR(0)项目集规范族C和活前缀识别自动机的状态转换函数GO。
函数ACTION和GOTO可按如下方法构造:
若项目A→α•bβ属于Ik,GO(Ik,a)=Ij,a为终结符,置ACTION[k,a]为“把状态j和符号a移进栈”,简记为“sj”;
若项目A→α•属于Ik,那么,对任何非终结符a,a∈FOLLOW(A),置ACTION[k,a]为“用产生式A→α进行归约”,简记为“rj”;
其中,假定A→α为文法G’的第j个产生式
若项目S’→S•属于Ik,则置ACTION[k,#]为可“接受”,简记为“acc”;
若GO(Ik,A)=Ij,A为非终结符,则置GOTO[k,A]=j;
分析表中凡不能用规则1至4填入信息的空白格均填上“出错标志”。
语法分析器的初始状态是包含S’→•S的项目集合的状态
SLR解决的冲突只是移进-规约冲突和规约-规约冲突
3主要功能模块流程图
主函数功能流程图
图程序主要流程
4系统测试
图输入
图项目表
图FIRST集
图FOLLOW集
图CLOSURE表
图EDGE表
图LR(0)表
图文法分析步骤
5结论
LR分析法是一种自下而上进行规范归约的语法分析方法。
这里L是指从左到右扫描输入符号串。
R是指构造最右推倒的逆工程。
这种分析法比递归下降分析法、预测分析法和算符优先分析法对文法的限制要少得多。
附录程序源码清单
#include<
iostream>
cstdio>
algorithm>
cstring>
cctype>
vector>
string>
queue>
map>
set>
sstream>
#defineMAX507
rint();
}
{
if!
=())returnfalse;
for(inti=0;
i<
i++)
if(element[i]==[i])continue;
elsereturnfalse;
returntrue;
structContent
inttype;
intnum;
stringout;
Content(){type=-1;
Content(inta,intb)
:
type(a),num(b){}
vector<
wf;
map<
string,vector<
>
dic;
VN_set;
string,bool>
vis;
stringstart="
S"
;
Closure>
collection;
items;
charCH='
$'
intgo[MAX][MAX];
intto[MAX];
char>
V;
boolused[MAX];
Contentaction[MAX][MAX];
intGoto[MAX][MAX];
string,set<
first;
follow;
memset(to,-1,sizeof(-1));
();
VN_set[wf[i].left].push_back(i);
for(intj=0;
j<
=wf[i].();
j++)
stringtemp=wf[i].right;
()+j,CH);
dic[wf[i].left].push_back());
if(j)
to[()-1]=();
(WF(wf[i].left,temp,i,()));
#ifdefDEBUG
puts("
-------------------------项目表-------------------------"
);
printf("
%s->
%sback:
%did:
%d\n"
items[i].(),items[i].(),items[i].back,items[i].id);
--------------------------------------------------------"
#endif
}
if(vis[x])return;
vis[x]=1;
vector<
id=VN_set[x];
string&
left=wf[id[i]].left;
right=wf[id[i]].right;
if(isupper(right[j]))
dfs(j,1));
set<
temp=first[(j,1)];
:
iteratorit=();
boolflag=true;
for(;
it!
=();
it++)
if(*it=='
~'
)flag=false;
first[left].insert(*it);
if(flag)break;
else
first[left].insert(right[j]);
break;
map<
iteratorit2=();
it2!
it2++)
if(vis[it2->
first])continue;
elsedfs(it2->
first);
****************FIRST集***************************"
FIRST(%s)={"
it->
());
&
temp=it->
second;
iteratorit1=();
boolflag=false;
it1!
it1++)
if(flag)printf("
"
%c"
*it1);
flag=true;
}"
#endif
from=follow[str1];
to=follow[str2];
(*it);
intx=id[i];
if(wf[x].right==str)returntrue;
returnfalse;
while(true)
boolgoon=false;
id=it2->
WF&
tt=wf[id[i]];
left=;
conststring&
right=;
for(intj=()-1;
j>
=0;
j--)
if(flag)
inttx=follow[(j,1)].size();
append(left,(j,1));
inttx1=follow[(j,1)].size();
if(tx1>
tx)goon=true;
if(_check(id,"
~"
))
flag=false;
for(intk=j+1;
k<
k++)
if(isupper(right[k]))
stringidd=(k,1);
from=first[idd];
to=follow[(j,1)];
if(*it1!
='
)
(*it1);
follow[(j,1)].insert(right[k]);
elseflag=false;
if(!
goon)break;
***************FOLLOW集*******************"
FOLLOW(%s)={"
('
#'
boolhas[MAX];
if(items[i].left[0]=='
S'
items[i].right[0]==CH)
Closuretemp;
str=items[i].right;
element=;
(items[i]);
size_tx=0;
for(x=0;
x<
x++)
if(str[x]==CH)
memset(has,0,sizeof(has));
has[i]=1;
if(x!
=()-1)
queue<
q;
(x+1,1));
while(!
())
stringu=();
id=dic[u];
for(size_tj=0;
inttx=id[j];
if(items[tx].right[0]==CH)
if(has[tx])continue;
has[tx]=1;
if(isupper(items[tx].right[1]))
(items[tx].(1,1));
(items[tx]);
(temp);
for(size_ti=0;
int,Closure>
temp;
collection[i].();
stringstr=collection[i].element[j].right;
if(str[x]==CH)break;
if(x==()-1)
continue;
inty=str[x+1];
intii;
()+x);
()+x+1,CH);
WFcmp=WF(collection[i].element[j].left,str,-1,-1);
for(size_tk=0;
if(items[k]==cmp)
ii=k;
element=temp[y].element;
(items[ii]);
has[ii]=1;
x++;
(it->
second);
sort(collection[i].(),collection[i].());
for(size_tj=i+1;
if(collection[i]==collection[j])
()+j);
-------------CLOSURE---------------------"
stringstreamsin;
sin<
<
"
closure-I"
<
i;
sin>
>
out;
collection[i].print(out);
"
memset(used,0,sizeof(used));
str=wf[i].left;
if(used[str[j]])continue;
used[str[j]]=1;
(str[j]);
str1=wf[i].right;
if(used[str1[j]])continue;
used[str1[j]]=1;
(str1[j]);
sort(),());
size_tk;
for(k=0;
if(s
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- SLR1 文法 分析 实验 报告