编译原理实验报告.docx

上传人:b****8 文档编号:30059117 上传时间:2023-08-04 格式:DOCX 页数:19 大小:54.99KB
下载 相关 举报
编译原理实验报告.docx_第1页
第1页 / 共19页
编译原理实验报告.docx_第2页
第2页 / 共19页
编译原理实验报告.docx_第3页
第3页 / 共19页
编译原理实验报告.docx_第4页
第4页 / 共19页
编译原理实验报告.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

编译原理实验报告.docx

《编译原理实验报告.docx》由会员分享,可在线阅读,更多相关《编译原理实验报告.docx(19页珍藏版)》请在冰豆网上搜索。

编译原理实验报告.docx

编译原理实验报告

 

编译原理实验报告

 

系别

专业

班级学号

姓名

XXX

指导教师

2010年12月27月

实验一:

词法分析程序的设计与实现

目的:

设计、编制、调试一个具体的词法分析程序,加深对词法分析原理的理解。

要求:

(1)通过对PL/0词法分析程序(GETSYM)的分析,认真研读描述PL/0语法规则的语法图或EBNF,并在此基础上按照PL/0的语法,编制一个词法分析程序。

此程序应具有如下功能:

(a)输入为字符串(待进行词法分析的源程序),输出为单词串,即由(单词,类别)所组成的二元组序列。

(b)有一定检查错误的能力,例如能发现2A这类不能作为单词的字符串。

实验代码:

#include

#include

#include

#include

#include

#defineNULL0

FILE*fp;

charcbuffer;

char*key[8]={"if","else","for","while","do","return","break","continue"};

char*border[6]={",",";","{","}","(",")"};

char*arithmetic[4]={"+","-","*","/"};

char*relation[6]={"<","<=","=",">",">=","<>"};

char*consts[20];

char*label[20];

intconstnum=0,labelnum=0;

intsearch(charsearchchar[],intwordtype)

{

inti=0;

switch(wordtype)

{

case1:

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

{

if(strcmp(key[i],searchchar)==0)

return(i+1);

}

case2:

{for(i=0;i<=5;i++)

{

if(strcmp(border[i],searchchar)==0)

return(i+1);

}

return(0);

}

case3:

{for(i=0;i<=3;i++)

{

if(strcmp(arithmetic[i],searchchar)==0)

{

return(i+1);

}

}

return(0);

}

case4:

{for(i=0;i<=5;i++)

{

if(strcmp(relation[i],searchchar)==0)

{

return(i+1);

}

}

return(0);

}

case5:

{for(i=0;i<=constnum;i++)

{

if(strcmp(consts[i],searchchar)==0)

{

return(i+1);

}

}

consts[i-1]=(char*)malloc(sizeof(searchchar));

strcpy(consts[i-1],searchchar);

constnum++;

return(i);

}

case6:

{for(i=0;i<=labelnum;i++)

{

if(strcmp(label[i],searchchar)==0)

{

return(i+1);

}

}

label[i-1]=(char*)malloc(sizeof(searchchar));

strcpy(label[i-1],searchchar);

labelnum++;

return(i);

}

}

}

charalphaprocess(charbuffer)

{

intatype;

inti=-1;

charalphatp[20];

while((isalpha(buffer))||(isdigit(buffer)))

{

alphatp[++i]=buffer;

buffer=fgetc(fp);

}

alphatp[i+1]='\0';

if(atype=search(alphatp,1))

printf("%s(1,%d)\n",alphatp,atype-1);

else

{

atype=search(alphatp,6);

printf("%s(6,%d)\n",alphatp,atype-1);

}

return(buffer);

}

chardigitprocess(charbuffer)

{

inti=-1;

chardigittp[20];

intdtype;

while((isdigit(buffer)))

{

digittp[++i]=buffer;

buffer=fgetc(fp);

}

digittp[i+1]='\0';

dtype=search(digittp,5);

printf("%s(5,%d)\n",digittp,dtype-1);

return(buffer);

}

charotherprocess(charbuffer)

{

inti=-1;

charothertp[20];

intotype,otypetp;

othertp[0]=buffer;

othertp[1]='\0';

if(otype=search(othertp,3))

{

printf("%s(3,%d)\n",othertp,otype-1);

buffer=fgetc(fp);

gotoout;

}

if(otype=search(othertp,4))

{

buffer=fgetc(fp);

othertp[1]=buffer;

othertp[2]='\0';

if(otypetp=search(othertp,4))

{

printf("%s(4,%d)\n",othertp,otypetp-1);

gotoout;

}

else

othertp[1]='\0';

printf("%s(4,%d)\n",othertp,otype-1);

gotoout;

}

if(buffer==':

')

{

buffer=fgetc(fp);

if(buffer=='=')

printf(":

=(2,2)\n");

buffer=fgetc(fp);

gotoout;

}

else

{

if(otype=search(othertp,2))

{

printf("%s(2,%d)\n",othertp,otype-1);

buffer=fgetc(fp);

gotoout;

}

}

if((buffer!

='\n')&&(buffer!

=''))

printf("%cerror,notaword\n",buffer);

buffer=fgetc(fp);

out:

return(buffer);

}

voidmain()

{

inti;

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

{

label[i]=NULL;

consts[i]=NULL;

}

if((fp=fopen("example.c","r"))==NULL)

printf("error\n");

else

{

cbuffer=fgetc(fp);

while(cbuffer!

=EOF)

{

if(isalpha(cbuffer))

cbuffer=alphaprocess(cbuffer);

elseif(isdigit(cbuffer))

cbuffer=digitprocess(cbuffer);

elsecbuffer=otherprocess(cbuffer);

}

printf("over\n");

getchar();

}

}

截图如下:

实验二:

递归子程序法语法分析程序的设计与实现

目的:

设计、编制、调试一个典型的语法分析程序,实现对如下文法的递归子程序语法分析,进一步掌握常用的语法分析方法。

要求:

程序的输入是任意符号串,输出是本次输入的符号串是否是该文法的句子的结论。

实验代码:

#include

#include

#include

#include

chara[50],b[50],d[200],e[10];

charch;

intn1,i1=0,flag=1,n=5;

intE();

intE1();

intT();

intG();

intS();

intF();

voidinput();

voidinput1();

voidoutput();

voidmain()

{

intf,p,j=0;

charx;

d[0]='E';

d[1]='=';

d[2]='>';

d[3]='T';

d[4]='G';

d[5]='#';

printf("请输入字符串(长度〈50,以#号结束)\n");

do{

scanf("%c",&ch);

a[j]=ch;

j++;

}while(ch!

='#');

n1=j;

ch=b[0]=a[0];

printf("文法\t分析串\t\t分析字符\t剩余串\n");

f=E1();

if(f==0)return;

if(ch=='#')

{

printf("accept\n");

p=0;

x=d[p];

while(x!

='#'){

printf("%c",x);

p=p+1;

x=d[p];/*输出推导式*/

}

}

else{

printf("error\n");

printf("回车返回\n");

getchar();getchar();

return;

}

printf("\n");

printf("回车返回\n");

getchar();

getchar();

}

intE1()

{

intf,t;

printf("E→TG\t");

flag=1;

input();

input1();

f=T();

if(f==0)return(0);

t=G();

if(t==0)return(0);

elsereturn

(1);

}

intE()

{

intf,t;

printf("E→TG\t");

e[0]='E';

e[1]='=';

e[2]='>';

e[3]='T';

e[4]='G';

e[5]='#';

output();

flag=1;

input();

input1();

f=T();

if(f==0)return(0);

t=G();

if(t==0)return(0);

elsereturn

(1);

}

intT()

{

intf,t;

printf("T→FS\t");

e[0]='T';

e[1]='=';

e[2]='>';

e[3]='F';

e[4]='S';

e[5]='#';

output();

flag=1;

input();

input1();

f=F();

if(f==0)return(0);

t=S();

if(t==0)return(0);

elsereturn

(1);

}

intG()

{

intf;

if(ch=='+'){

b[i1]=ch;

printf("G→+TG\t");

e[0]='G';

e[1]='=';

e[2]='>';

e[3]='+';

e[4]='T';

e[5]='G';

e[6]='#';

output();

flag=0;

input();

input1();

ch=a[++i1];

f=T();

if(f==0)return(0);

G();

return

(1);

}

printf("G→^\t");

e[0]='G';

e[1]='=';

e[2]='>';

e[3]='^';

e[4]='#';

output();

flag=1;

input();

input1();

return

(1);

}

intS()

{

intf,t;

if(ch=='*'){

b[i1]=ch;

printf("S→*FS\t");

e[0]='S';e[1]='=';e[2]='>';e[3]='*';e[4]='F';e[5]='S';e[6]='#';

output();

flag=0;

input();input1();

ch=a[++i1];

f=F();

if(f==0)

return(0);

t=S();

if(t==0)

return(0);

else

return

(1);

}

printf("S→^\t");

e[0]='S';e[1]='=';e[2]='>';e[3]='^';e[4]='#';

output();

flag=1;

a[i1]=ch;

input();input1();

return

(1);

}

intF()

{

intf;

if(ch=='('){

b[i1]=ch;

printf("F→(E)\t");

e[0]='F';e[1]='=';e[2]='>';e[3]='(';e[4]='E';e[5]=')';e[6]='#';

output();

flag=0;

input();input1();

ch=a[++i1];

f=E();

if(f==0)

return(0);

if(ch==')')

{b[i1]=ch;

printf("F→(E)\t");

flag=0;

input();input1();

ch=a[++i1];

}

else{

printf("error\n");

return(0);

}

}

elseif(ch=='i')

{b[i1]=ch;

printf("F→i\t");

e[0]='F';e[1]='=';e[2]='>';e[3]='i';e[4]='#';

output();

flag=0;

input();input1();

ch=a[++i1];

}

else{

printf("error\n");

return(0);

}

return

(1);

}

voidinput()

{

intj=0;

for(;j<=i1-flag;j++)

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

printf("\t\t");

printf("%c\t\t",ch);

}

voidinput1(){

intj;

for(j=i1+1-flag;j

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

printf("\n");

}

voidoutput(){

intm,k,j,q;

inti=0;m=0;k=0;q=0;

i=n;

d[n]='=';d[n+1]='>';d[n+2]='#';n=n+2;i=n;

i=i-2;

while(d[i]!

='>'&&i!

=0)i=i-1;

i=i+1;

while(d[i]!

=e[0])i=i+1;

q=i;

m=q;

k=q;

while(d[m]!

='>')

m=m-1;

m=m+1;

while(m!

=q){

d[n]=d[m];m=m+1;n=n+1;

}

d[n]='#';

for(j=3;e[j]!

='#';j++){

d[n]=e[j];

n=n+1;

}

k=k+1;

while(d[k]!

='='){

d[n]=d[k];

n=n+1;

k=k+1;

}

d[n]='#';

}

实验截图:

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

当前位置:首页 > 解决方案 > 学习计划

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

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