实验五 SLR语法分析器.docx

上传人:b****1 文档编号:15255746 上传时间:2023-07-02 格式:DOCX 页数:16 大小:69.15KB
下载 相关 举报
实验五 SLR语法分析器.docx_第1页
第1页 / 共16页
实验五 SLR语法分析器.docx_第2页
第2页 / 共16页
实验五 SLR语法分析器.docx_第3页
第3页 / 共16页
实验五 SLR语法分析器.docx_第4页
第4页 / 共16页
实验五 SLR语法分析器.docx_第5页
第5页 / 共16页
实验五 SLR语法分析器.docx_第6页
第6页 / 共16页
实验五 SLR语法分析器.docx_第7页
第7页 / 共16页
实验五 SLR语法分析器.docx_第8页
第8页 / 共16页
实验五 SLR语法分析器.docx_第9页
第9页 / 共16页
实验五 SLR语法分析器.docx_第10页
第10页 / 共16页
实验五 SLR语法分析器.docx_第11页
第11页 / 共16页
实验五 SLR语法分析器.docx_第12页
第12页 / 共16页
实验五 SLR语法分析器.docx_第13页
第13页 / 共16页
实验五 SLR语法分析器.docx_第14页
第14页 / 共16页
实验五 SLR语法分析器.docx_第15页
第15页 / 共16页
实验五 SLR语法分析器.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

实验五 SLR语法分析器.docx

《实验五 SLR语法分析器.docx》由会员分享,可在线阅读,更多相关《实验五 SLR语法分析器.docx(16页珍藏版)》请在冰点文库上搜索。

实验五 SLR语法分析器.docx

实验五SLR语法分析器

《编译原理》实验报告

实验序号:

  05      实验项目名称:

SLR语法分析器

学  号

姓  名

专业、班

实验地点

指导教师

实验时间

一、实验目的及要求

利用SLR文法的原理,掌握非递归预测分析的编程方法。

 

二、实验设备(环境)及要求

确定开发工具,如TC、VC、VC++、Delphi等;花一周时间熟悉开发工具。

花一周时间确定被处理的语言的语法特点(初步确定,也可使用现成语言如Pascal、C等)。

写好实验报告,编好程序。

三、实验内容与步骤

利用c语言实现以下SLR文法的分析程序。

文法的开始符号为:

E-->E+T

E-->T

T-->T*F

T-->F

F-->(E)

F-->id

实验步骤

1、建立该文法的分析表;

2、编程实现LR分析程序。

四、实验结果与数据处理

1文法分析表:

2.程序分析:

实验源程序:

#include

#include

intAction[12][6]=

{

105,0,0,104,0,0,

0,106,0,0,0,-1,

0,52,107,0,52,52,

0,54,54,0,54,54,

105,0,0,104,0,0,

0,56,56,0,56,56,

105,0,0,104,0,0,

105,0,0,104,0,0,

0,106,0,0,111,0,

0,51,107,0,51,51,

0,53,53,0,53,53,

0,55,55,0,55,55};

intGoto[12][3]=

{

1,2,3,

0,0,0,

0,0,0,

0,0,0,

8,2,3,

0,0,0,

0,9,3,

0,0,10,

0,0,0,

0,0,0,

0,0,0,

0,0,0

};

charGrammar[20][10]={'\0'};

charVT[10],VN[10];

charAVT[6]={'i','+','*','(',')','#'};

charGVN[3]={'E','T','F'};

intvnNum,vtNum,stateNum=12;

intVNum[10];

intgrammarNum;

typedefstruct{

char*base;

char*top;

}SymbolStack;

typedefstruct{

int*base;

int*top;

}StateStack;

StateStackstate;

SymbolStacksymbol;

intScanGrammar()

{

FILE*fp=fopen("SLR文法.txt","r");

FILE*tp;

charsingleChar,nextChar;

inti=0,j=0,k,count;

while(!

feof(fp))

{

fscanf(fp,"%c",&singleChar);

if(singleChar=='?

')

{

Grammar[i][j]='\0';

break;

}

if(singleChar=='\n')

{

Grammar[i][j]='\0';

i++;

j=0;

continue;

}

if(singleChar=='-')

{

tp=fp;

fscanf(tp,"%c",&nextChar);

if(nextChar=='>')

{

fp=tp;

continue;

}

}

if(singleChar=='|')

{

Grammar[i+1][0]=Grammar[i][0];

Grammar[i][j]='\0';

i++;

j=1;

continue;

}

Grammar[i][j]=singleChar;

if(singleChar>='A'&&singleChar<='Z')

{

count=0;

while(VN[count]!

=singleChar&&VN[count]!

='\0')

{

count++;

}

if(VN[count]=='\0')

{

vnNum=count+1;

if(singleChar=='S')

{

j++;

continue;

}

VN[count]=singleChar;

vnNum=count+1;

}

}

else

{

count=0;

while(VT[count]!

=singleChar&&VT[count]!

='\0')

{

count++;

}

if(VT[count]=='\0')

{

VT[count]=singleChar;

vtNum=count+1;

}

}

j++;

}

printf("输入的文法:

\n");

for(k=0;k<=i;k++)

{

j=0;

while(Grammar[k][j]!

='\0')

{

if(j==1)

{

printf("->");

}

printf("%c",Grammar[k][j]);

j++;

}

printf("\n");

}

count=0;

printf("VT:

");

while(VT[count]!

='\0')

{

printf("%3c",VT[count]);

count++;

}

VT[count]='#';

vtNum=count+1;

printf("%3c",VT[count]);

printf("\nVN:

");

count=0;

while(VN[count]!

='\0')

{

printf("%3c",VN[count]);

count++;

}

printf("\n");

//

printf("\n%d%d\n",vtNum,vnNum);

fclose(fp);

grammarNum=i+1;

returni;

}

intvNumCount()

{

inti,j;

for(i=0;i

{

j=1;

while(Grammar[i][j]!

='\0')

{

j++;

}

VNum[i]=j;

//

printf("%3d",VNum[i]);

}

printf("\n");

return0;

}

voidInitStack()

{

state.base=(int*)malloc(100*sizeof(int));

if(!

state.base)exit

(1);

state.top=state.base;

*state.top=0;

symbol.base=(char*)malloc(100*sizeof(char));

if(!

symbol.base)exit

(1);

symbol.top=symbol.base;

*symbol.top='#';

}

intJudge(intstateTop,charinputChar)

{

inti,j;

for(i=0;i

{

if(stateTop==i)break;

}

for(j=0;j

{

if(inputChar==AVT[j])break;

}

returnAction[i][j];

}

intGetGoto(intstateTop,charinputChar)

{

inti,j;

for(i=0;i

{

if(stateTop==i)break;

}

for(j=0;j

{

if(inputChar==GVN[j])break;

}

returnGoto[i][j];

}

intprint(intcount,inti,charInput[],intaction,intgt,intsign)

{

int*p=state.base,stateNum;

intj,jj;

char*q=symbol.base,symbolNum;

printf("%d\t",count);

while(p!

=state.top+1)

{

stateNum=*p;

printf("%d",stateNum);

p++;

}

printf("\t");

while(q!

=symbol.top+1)

{

symbolNum=*q;

printf("%c",symbolNum);

q++;

}

printf("\t");

j=i;

jj=0;

while(jj

{

printf("");

jj++;

}

while(Input[j]!

='\0')

{

printf("%c",Input[j]);

j++;

}

printf("\t");

if(sign==1)

{

printf("\tS%d\t%d\n",action,gt);

}

if(sign==2)

{

printf("\tr%d\t%d\n",action,gt);

}

if(sign==3)

{

printf("\tacc\t%d\n",gt);

}

if(sign==0)printf("\t0\t0\n");

return0;

}

intPop(intaction)

{

int*p,stateNum,ssValue,i;

state.top--;

p=state.top;

stateNum=*p;

i=VNum[action]-1;

while(i!

=0)

{

symbol.top--;

i--;

}

symbol.top++;

*symbol.top=Grammar[action][0];

ssValue=GetGoto(stateNum,Grammar[action][0]);

if(ssValue==0)returnssValue;

state.top++;

*state.top=ssValue;

returnssValue;

}

intReduction()

{

charInput[20];

inti=0,count=1;

intssValue,action;

intstateTop,gt;

intsign=-1;//移进1,规约2,接受3

scanf("%s",&Input);

while(Input[i]!

='\0')

{

if(Input[i]>='A'&&Input[i]<='Z')

{

printf("输入的不是有效的表达式!

");

return0;

}

i++;

}

i=0;

printf("步骤\t状态栈\t符号栈\t输入串\t\tACTION\tGOTO\n");

while(Input[i]!

='\0')

{

if(count==1)

{

print(count,i,Input,0,0,0);

count++;

}

stateTop=*state.top;

ssValue=Judge(stateTop,Input[i]);

if(ssValue==0)

{

state.top--;

if(*symbol.top=='#')

{

printf("规约出错!

");

return0;

}

continue;

}

if(ssValue==-1)

{

sign=3;

print(count,i,Input,ssValue,0,sign);

count++;

return1;

}

if(ssValue>=100)

{

sign=1;

action=ssValue-100;

state.top++;

*state.top=action;

symbol.top++;

*symbol.top=Input[i];

i++;

print(count,i,Input,action,0,sign);

count++;

}

if(ssValue>=50&&ssValue<100)

{sign=2;

action=ssValue-50;

gt=Pop(action);

print(count,i,Input,action,gt,sign);

count++;

}

}

return0;

}

intmain()

{

ScanGrammar();

vNumCount();

InitStack();

Reduction();

return0;

}

五、分析与讨论

通过此次试验我学会了:

掌握非递归预测分析的编程方法。

成绩

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

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2