实验三LR1分析报告法实验报告材料三点检验法检验表.docx

上传人:b****7 文档编号:25100193 上传时间:2023-06-05 格式:DOCX 页数:9 大小:16.29KB
下载 相关 举报
实验三LR1分析报告法实验报告材料三点检验法检验表.docx_第1页
第1页 / 共9页
实验三LR1分析报告法实验报告材料三点检验法检验表.docx_第2页
第2页 / 共9页
实验三LR1分析报告法实验报告材料三点检验法检验表.docx_第3页
第3页 / 共9页
实验三LR1分析报告法实验报告材料三点检验法检验表.docx_第4页
第4页 / 共9页
实验三LR1分析报告法实验报告材料三点检验法检验表.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

实验三LR1分析报告法实验报告材料三点检验法检验表.docx

《实验三LR1分析报告法实验报告材料三点检验法检验表.docx》由会员分享,可在线阅读,更多相关《实验三LR1分析报告法实验报告材料三点检验法检验表.docx(9页珍藏版)》请在冰豆网上搜索。

实验三LR1分析报告法实验报告材料三点检验法检验表.docx

实验三LR1分析报告法实验报告材料三点检验法检验表

实验三LR

(1)分析报告法实验报告材料_三点检验法检验表

一、实验目的构造LR

(1)分析程序,利用它进行语法分析,判断给出的符号串是否为该文法识别的句子,了解LR(K)分析方法是严格的从左向右扫描,和自底向上的语法分析方法。

二、LR

(1)分析法实验设计思想及算法

(1)总控程序,也可以称为驱动程序。

对所有的LR分析器总控程序都是相同的。

(2)分析表或分析函数,不同的文法分析表将不同,同一个文法采用的LR分析器不同时,分析表将不同,分析表又可以分为动作表(ACTION)和状态转换(GOTO)表两个部分,它们都可用二维数组表示。

(3)分析栈,包括文法符号栈和相应的状态栈,它们均是先进后出栈。

分析器的动作就是由栈顶状态和当前输入符号所决定。

LR分析器由三个部分组成:

其中:

SP为栈指针,S[i]为状态栈,X[i]为文法符号栈。

状态转换表用GOTO[i,X]=j表示,规定当栈顶状态为i,遇到当前文法符号为X时应转向状态j,X为终结符或非终结符。

ACTION[i,a]规定了栈顶状态为i时遇到输入符号a应执行。

动作有四种可能:

(1)移进:

action[i,a]=Sj:

状态j移入到状态栈,把a移入到文法符号栈,其中i,j表示状态号。

(2)归约:

action[i,a]=rk:

当在栈顶形成句柄时,则归约为相应的非终结符A,即文法中有A-B的产生式,若B的长度为R(即|B|=R),则从状态栈和文法符号栈中自顶向下去掉R个符号,即栈指针SP减去R,并把A移入文法符号栈,j=GOTO[i,A]移进状态栈,其中i为修改指针后的栈顶状态。

(3)接受acc:

当归约到文法符号栈中只剩文法的开始符号S时,并且输入符号串已结束即当前输入符是'#',则为分析成功。

(4)报错:

当遇到状态栈顶为某一状态下出现不该遇到的文法符号时,则报错,说明输入端不是该文法能接受的符号串。

三、程序结构描述

1、定义的变量

stringaction[12][6]={

{"s5","error","error","s4","error","error"},

{"error","s6","error","error","error","acc"},

{"error","r2","s7","error","r2","r2"},

{"error","r4","r4","error","r4","r4"},

{"s5","error","error","s4","error","error"},

{"error","r6","r6","error","r6","r6"},

{"s5","error","error","s4","error","error"},

{"s5","error","error","s4","error","error"},

{"error","s6","error","error","s11","error"},

{"error","r1","r7","error","r1","r1"},

{"error","r3","r3","error","r3","r3"},

{"error","r5","r5","error","r5","r5"}

};

stringgo[12][3]={

{"1","2","3"},

{"error","error","error"},

{"error","error","error"},

{"error","error","error"},

{"8","2","3"},

{"error","error","error"},

{"error","9","3"},

{"error","error","10"},

{"error","error","error"},

{"error","error","error"},

{"error","error","error"},

{"error","error","error"},

};//初始化预测分析表

charVt[6]={'i','+','*','(',')','#'};终结符表

stringLR[6]={"E->E+T","E->T","T->T*F","T->F","F->(E)","F->i"};//LR文法

stackS;状态栈

stackX;符号栈

charinput[10];输入字符

2、定义的函数

intnum(strings)判断字符串中的数字

voidprint(inti,char*c)剩余输入串的输出

intsame(chara)用于查找终结符

voidanalyse()分析程序

四、程序源代码及运行结果

#include

#include

#include

#include

usingnamespacestd;

//初始化预测分析表

stringaction[12][6]={

{"s5","error","error","s4","error","error"},

{"error","s6","error","error","error","acc"},

{"error","r2","s7","error","r2","r2"},

{"error","r4","r4","error","r4","r4"},

{"s5","error","error","s4","error","error"},

{"error","r6","r6","error","r6","r6"},

{"s5","error","error","s4","error","error"},

{"s5","error","error","s4","error","error"},

{"error","s6","error","error","s11","error"},

{"error","r1","r7","error","r1","r1"},

{"error","r3","r3","error","r3","r3"},

{"error","r5","r5","error","r5","r5"}

};

stringgo[12][3]={

{"1","2","3"},

{"error","error","error"},

{"error","error","error"},

{"error","error","error"},

{"8","2","3"},

{"error","error","error"},

{"error","9","3"},

{"error","error","10"},

{"error","error","error"},

{"error","error","error"},

{"error","error","error"},

{"error","error","error"},

};

charVt[6]={'i','+','*','(',')','#'};//终结符表

stringLR[6]={"E->E+T","E->T","T->T*F","T->F","F->(E)","F->i"};//LR文法

stackS;//状态栈

stackX;//符号栈

charinput[10];//输入字符

intnum(strings){//判断字符串中的数字

inti;

stringstr="";

for(intj=0;j

if(s[j]>='0'&&s[j]<='9')str=str+s[j];

}

i=atoi(str.c_str());

returni;

}

voidprint(inti,char*c)//剩余输入串的输出

{

for(intj=i;j<10;j++)

cout<

cout<<'\t';

}

intsame(chara){//用于查找终结符

for(inti=0;i<6;i++){

if(a==Vt[i])returni;

}

}

///**********分析程序*******************

voidanalyse(){

boolflag=true;//循环条件

intstep=1,point=0,state=0;//步骤、指针、状态

charch1,ch2;

intm,n,l;//用于判断终结符,分析表,表达式右部的长度

stringstr1;//用于判断对应分析表中的符号

stringstr2="#",str3="0";//记录符号栈的所有元素

cout<<"请输入要规约的字符串:

"<

cin>>input;

X.push('#');

S.push(0);

cout<<"步骤"<<'\t'<<"状态栈"<<'\t'<<"符号栈"<<'\t'<<"输入串"<<'\t'<<"动作"<

cout<

print(point,input);

cout<<"初始化"<

//************进入循环

while(flag){

state=S.top();

ch1=input[point];

m=same(ch1);

str1=action[state][m];

//***********移进动作

if(str1[0]=='s'){

n=num(str1);

S.push(n);

X.push(ch1);

str2=str2+ch1;

ch2=n+48;

str3=str3+ch2;

point++;

cout<

print(point,input);

cout<

'<<"移进"<

}

//**********归约动作

elseif(str1[0]=='r'){

n=num(str1);

l=LR[n-1].length()-3;

for(inti=1;i<=l;i++){S.pop();

str3=str3.substr(0,str3.length()-1);

X.pop();

str2=str2.substr(0,str2.length()-1);

}

X.push(LR[n-1][0]);

str2=str2+LR[n-1][0];

state=S.top();

if(LR[n-1][0]=='E'){S.push(num(go[state][0]));

ch2=num(go[state][0])+48;

str3=str3+ch2;

}

elseif(LR[n-1][0]=='T'){S.push(num(go[state][1]));

ch2=num(go[state][1])+48;

str3=str3+ch2;

}

elseif(LR[n-1][0]=='F'){S.push(num(go[state][2]));

ch2=num(go[state][2])+48;

str3=str3+ch2;

}

cout<

print(point,input);

cout<

'<

}

//*********出错

elseif(str1=="error"){

cout<<"ERROR"<<"程序错误,分析结束!

"<

flag=false;

}

//**********分析成功

elseif(str1=="acc"){cout<<"分析成功,终止程序"<

flag=false;

}

}

}

intmain(){

analyse();

return0;

}

测试:

i*i+i

结果:

五、实验总结

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 小学教育

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1