编译原理实验报告Word文档格式.docx

上传人:b****5 文档编号:16471756 上传时间:2022-11-23 格式:DOCX 页数:21 大小:93.63KB
下载 相关 举报
编译原理实验报告Word文档格式.docx_第1页
第1页 / 共21页
编译原理实验报告Word文档格式.docx_第2页
第2页 / 共21页
编译原理实验报告Word文档格式.docx_第3页
第3页 / 共21页
编译原理实验报告Word文档格式.docx_第4页
第4页 / 共21页
编译原理实验报告Word文档格式.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

编译原理实验报告Word文档格式.docx

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

编译原理实验报告Word文档格式.docx

/'

//ch=='

:

'

'

;

('

)'

boolisJ(charch){

if(ch=='

.'

['

]'

<

>

{'

}'

#'

//

boolisBlank(charch){

'

\t'

intmain(){

stringsrc,ste,s;

charch0,ch,ch1[2];

charktt[48][20]={"

and"

"

begin"

const"

div"

do"

else"

end"

function"

if"

integer"

"

not"

or"

procedure"

program"

read"

real"

then"

type"

var"

while"

write"

标识符"

无符号数"

"

."

("

)"

["

]"

.."

++"

--"

+"

-"

*"

/"

="

"

{"

}"

#"

};

intpos=0;

FILE*fp;

fp=fopen("

in.txt"

r"

);

ch0=fgetc(fp);

while(ch0!

=EOF)

{

//if(ch0!

){src+=ch0;

src+=ch0;

}

src+='

cout<

src<

endl;

ch=src[pos++];

ste="

for(intj=0;

j<

47;

j++){cout<

ktt[j]<

词法分析:

\n"

while(ch!

charstr[20];

if(ch!

\n'

if(isDigit(ch))

{//判断常数

inti=0;

while(isDigit(ch)||ch=='

str[i++]=ch;

//i++;

str[i]='

\0'

ste=ste+"

|"

22"

str;

continue;

}

elseif(isLetter(ch))

{//判断字符

inti=0,j;

while(isLetter(ch)||isDigit(ch))

for(j=0;

21;

j++){//判断是否关键字

intt=strcmp(str,ktt[j]);

if(t==0){

stringstreamss;

ste+="

ss<

ste;

ss<

j;

ss>

break;

if(j==21){ste=ste+"

21"

//cout<

elseif(isP(ch)){///判断是否运算符

for(j=34;

38;

j++){

elseif(isJ(ch))//判断是否界符

{

while(isJ(ch))

for(j=23;

elseif(isBlank(ch))

ch;

else{

ste<

return0;

四.给定文件中的内容:

自己输入就行

五.运行结果:

实验二递归向下分析

用C语言设计、编制、调试一个典型的语法分析

程序,用于实现下列语法的分析

G[S]:

E→eBaA

A→a|bAcB

B→dEd|aC

C→e|dC

通过对G[S]问法的分析实现,加深对语法分析

的理解,进一步掌握常用的语法分析方法。

#include<

iostream.h>

charinputstream[50];

inttemp=0;

intright;

voide();

voida();

voidb();

voidc();

voidmain()

{

intright=1;

charstag;

while

(1){

if(stag=='

y'

)break;

请输入你要分析的字符串,以#结束(^为空字符):

cin>

inputstream;

e();

if(inputstream[temp]=='

right==1)

cout<

分析成功"

else

分析失败"

是否结束:

yorn:

stag;

voide()

e'

E->

eBaA"

temp++;

b();

if(inputstream[temp]=='

{

cout<

temp++;

a();

}

elseright=1;

elseright=0;

voida()

A->

a"

elseif(inputstream[temp]=='

b'

bAcB"

a();

c'

b();

voidb()

d'

B->

dEd"

e();

aC"

c();

voidc()

C->

e"

dC"

四.运行结果:

实验三LL

(1)文法分析

用C语言编写程序,用于实现对输入字符串

中内容进行LL

(1)文法分析。

通过编写程序实现对输入字符串中内容进行

语法分析,加深对语法分析的理解,并将其掌握。

#include<

stdio.h>

stdlib.h>

string.h>

dos.h>

charA[20];

charB[20];

charv1[20]={'

i'

charv2[20]={'

E'

G'

T'

S'

F'

intj=0,b=0,top=0,l;

typedefstructtype{

charorigin;

chararray[5];

intlength;

}type;

typee,t,g,gl,s,sl,f,fl;

typeC[10][10];

voidprint(){

inta;

for(a=0;

a<

top+1;

a++)

printf("

%c"

A[a]);

printf("

\t\t"

voidprint1(){

intj;

b;

j++)

for(j=b;

=1;

B[j]);

\t\t\t"

voidmain(){

intm,n,k=0,flag=0,finish=0;

charch,x;

typecha;

e.origin='

strcpy(e.array,"

TG"

e.length=2;

t.origin='

strcpy(t.array,"

FS"

t.length=2;

g.origin='

strcpy(g.array,"

+TG"

g.length=3;

gl.origin='

gl.array[0]='

g.length=1;

s.origin='

strcpy(s.array,"

*FS"

s.length=3;

sl.origin='

sl.array[0]='

sl.length=1;

f.origin='

strcpy(f.array,"

(E)"

f.length=3;

fl.origin='

fl.array[0]='

fl.length=1;

for(m=0;

m<

=4;

m++)

for(n=0;

n<

=5;

n++)

C[m][n].origin='

N'

C[0][0]=e;

C[0][3]=e;

C[1][1]=g;

C[1][4]=gl;

C[1][5]=gl;

C[2][0]=t;

C[2][3]=t;

C[3][1]=sl;

C[3][2]=s;

C[3][4]=C[3][5]=sl;

C[4][0]=fl;

C[4][3]=f;

提示:

本程序只能对由'

构成的以'

结束的字符串进行分析\n"

请输入要分析的字符串:

do{

scanf("

&

ch);

if((ch!

)&

(ch!

))

{

printf("

输入中有非法的字符\n"

exit

(1);

}

B[j]=ch;

j++;

}while(ch!

l=j;

ch=B[0];

A[top]='

A[++top]='

步骤\t\t分析栈\t\t剩余字符\t\t所用产生式\n"

do

x=A[top--];

printf("

%d"

k++);

for(j=0;

if(x==v1[j])

{

flag=1;

break;

}

if(flag==1)

if(x=='

{

finish=1;

printf("

acc!

getchar();

//?

?

exit

(1);

}

if(x==ch)

print();

print1();

%c匹配\n"

ch);

ch=B[++b];

flag=0;

else{

%c出错\n"

else{

for(j=0;

if(x==v2[j])

{

m=j;

break;

}

for(j=0;

if(ch==v1[j])

{

n=j;

break;

}

cha=C[m][n];

if(cha.origin!

print();

print1();

printf("

%c->

cha.origin);

for(j=0;

cha.length;

printf("

cha.array[j]);

for(j=(cha.length-1);

j>

=0;

j--)

A[++top]=cha.array[j];

if(A[top]=='

top--;

else

x);

exit

(1);

}while(finish==0);

四.实验结果

实验四逆波兰式

用C语言编写程序,用于实现对输入表达式

求值。

通过编写程序实现对输入表达式进行

求值得出结果,加深对逆波兰求解表达式过程理解,并将其掌握。

math.h>

#definemax100

charex[max];

//存储后缀表达式

voidtrans(){//将算术表达式转化为后缀表达式

charstr[max];

//存储原算术表达式的值

charstack[max];

//作为栈使用

charch;

intsum,i,j,t,top=0;

**********************************\n"

*输入一个求值表达式,以#号结束。

*\n"

算术表达式:

i=0;

//获取输入表达式,以#号结束

do{

i++;

scanf("

str[i]);

}while(str[i]!

i!

=max);

sum=i;

t=1;

i=1;

ch=str[i];

i++;

){

switch(ch){

case'

top++;

stack[top]=ch;

break;

while(stack[top]!

ex[t]=stack[top];

top--;

t++;

top--;

while(top!

=0&

stack[top]!

while(stack[top]=='

||stack[top]=='

default:

while(ch>

ch<

ex[t]=ch;

ch=str[i];

i--;

ex[t]='

ch=str[i];

while(top!

=0){

ex[t]=stack[top];

ex[t]='

\n\t原来的表达式:

for(j=1;

sum;

str[j]);

\n\t后缀表达式:

ex);

t;

ex[j]);

voidcompvalue(){//计算后缀表达式的值

floatstack[max],d;

intt=1,top=0;

ch=ex[t];

stack[top-1]=stack[top-1]+stack[top];

stack[top-1]=stack[top-1]-stack[top];

stack[top-1]=stack[top-1]*stack[top];

if(stack[top]!

=0)

stack[top-1]=stack[top-1]/stack[top];

else{

\n\t除零错误!

//exit(0);

d=0;

d=10*d+ch-'

//字符到数字的转化

ch=ex[t];

stack[top]=d;

printf("

\n\t计算结果:

%g\n"

stack[top]);

while

(1)

trans();

compvalue();

其中#号表示间隔符号,没有实际意义。

实验总结:

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

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

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

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