软件代码开发技术实验报告.docx
《软件代码开发技术实验报告.docx》由会员分享,可在线阅读,更多相关《软件代码开发技术实验报告.docx(16页珍藏版)》请在冰豆网上搜索。
软件代码开发技术实验报告
实验报告
专业
课程名称软件代码开发技术
学号
姓名
辅导教师成绩
实验日期
实验时间
4学时
1实验名称:
词法分析器的设计与实现
2、实验目的
(1)掌握C语言单词符号的划分、正规式、状态转换图及词法分析器的实现。
(2)掌握词法分析程序的作用。
3、实验要求
(1)对任给的一个C语言源程序,能够滤掉空格、回车换行符、tab键及注释。
(2)识别各类单词符号,如关键字、标识符、运算符、常数、界符,结果以二元式形式输出,并构造符号表。
(3)输出有词法错误的单词及所在行号。
(在此阶段只能识别有限的词法错误)
4、实验原理
根据扫描到的单词符号的第一个字符的种类,分别转到相应的程序进行处理。
这些程序的功能就是识别以相应字符开头的各类单词符号。
5、实验步骤
(1)根据C语言各类单词的正规式,构造能识别各类单词的状态转换图。
(2)根据状态转换图,构造识别各类单词的词法分析器。
6、状态转换图及词法分析程序
#include
#include
#include
#include
#defineNULL0
FILE*fp;
charcbuffer;
char*key[8]={"if","else","for","while","int","return","break","continue"};
char*border[6]={",",";","{","}","(",")"};
char*arithmetic[5]={"+","-","*","/","="};
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);
return0;
case2:
for(i=0;i<=5;i++)
if(strcmp(border[i],searchchar)==0)return(i+1);
return(0);
case3:
for(i=0;i<=4;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;iif(strcmp(consts[i],searchchar)==0)return(i+1);
consts[i]=(char*)malloc(sizeof(searchchar));
strcpy(consts[i],searchchar);
constnum++;
return(i+1);
case6:
for(i=0;iif(strcmp(label[i],searchchar)==0)return(i+1);
label[i]=(char*)malloc(sizeof(searchchar));
strcpy(label[i],searchchar);
labelnum++;
return(i+1);
}
}
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);
else
{
atype=search(alphatp,6);
printf("(%s,6,%d)\n",alphatp,atype);
}
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);
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);
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);
gotoout;
}
else
{
othertp[1]='\0';
printf("(%s,4,%d)\n",othertp,otype);
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);
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("11.txt","r"))==NULL)
printf("OpenFileError!
");
else
{
printf("输出格式为:
(单词,类别,位置)\n");
cbuffer=fgetc(fp);
while(cbuffer!
=EOF)
{
if(isalpha(cbuffer))
cbuffer=alphaprocess(cbuffer);
else
if(isdigit(cbuffer))
cbuffer=digitprocess(cbuffer);
elsecbuffer=otherprocess(cbuffer);
}
printf("AnysisEnd\n");
getchar();
}
}
7、测试及结果
8、心得
通过这次软件代码开发技术的实验,让我对C++语言有了更深的理解和认识。
对单词符号的划分、正规式、状态转换图及词法分析器有了进一步的理解。
词法分析是编译的第一个阶段,他的主要任务是从左至右逐个字符的对源程序进行扫描,产生一个个单词序列,用以语法分析。
正规式用以描述单词符号的工具。
辅导教师成绩
实验日期
实验时间
4学时
1实验名称语法分析器的设计与实现
2、实验目的
掌握自上而下语法分析方法、自下而上语法分析方法
3、实验要求
(1)实验内容:
四选一
1设计及实现能够识别表达式的预测分析程序。
文法如下:
G[E]:
E->E+T|T
T->T*F|F
F->(E)|i
2设计及实现能够识别表达式的LR分析程序。
文法如下:
G[E]:
E->E+T|T
T->T*F|F
F->(E)|i
③设计及实现能够识别表达式的算符优先分析程序。
文法如下:
G[E]:
E->E+T|T
T->T*F|F
F->P↑F|P
P->(E)|i
④设计及实现计算表达式的计算器。
表达式中可包含+、-、*、/、(、)运算符。
(2)实验要求:
对已给的一个二元式形式表达式,能够检查有无语法错误。
并指定出错位置。
将表达式的语法树输出(或将语法分析过程输出)。
4、实验原理
根据自上而下和自下而上的语法分析思想实现语法分析程序。
5、实验步骤
(1)根据文法构造语法分析表。
(2)编写总控程序实现语法分析。
6、状态转换图及语法分析程序
#include
#include
usingnamespacestd;
charVn[]={'E','e','T','t','F'};
charVt[]={'i','+','*','(',')','#'};
intLENVt=sizeof(Vt);
voidshowstack(stackst)
{inti,j;charch[100];j=st.size();
for(i=0;i{ch[i]=st.top();st.pop();
}
for(i=j-1;i>=0;i--)
{cout<}
}
intfind(charc,chararray[],intn)
{inti;intflag=0;
for(i=0;i{if(c==array[i])
flag=1;
}
returnflag;
}
intlocation(charc,chararray[])
{inti;for(i=0;c!
=array[i];i++);
returni;
}
voiderror()
{cout<<"出错!
"<voidanalyse(charVn[],charVt[],stringM[5][6],stringstr)
{inti,j,p,q,h,flag=1;chara,X;
stackst;
st.push('#');
st.push(Vn[0]);
j=0;
h=1;
a=str[j];
cout<<"步骤"<<"分析栈"<<"剩余输入串"<<"所用产生式"<while(flag==1)
{cout<cout<<"";
for(i=j;iX=st.top();
if(find(X,Vt,LENVt)==1)
if(X==a)
if(X!
='#')
{
cout<<""<st.pop();
a=str[++j];
}
else
{cout<<""<<"接受!
"<flag=0;}
else
{error();break;}
else
{
p=location(X,Vn);
q=location(a,Vt);
stringS1("NULL"),S2("null");
if(M[p][q]==S1||M[p][q]==S2)
{error();break;}
else
{
stringstr0=M[p][q];
cout<<""<"<st.pop();
if(str0!
="$")
for(i=str0.size()-1;i>=0;i--)st.push(str0[i]);
}
}
}
}
}
main()
{stringM[5][6]={"Te","NULL","NULL","Te",
"NULL","NULL",
"NULL","+Te","NULL","NULL","$","$","Ft","NULL","NULL","Ft",
"NULL","NULL","NULL","$","*Ft","NULL","$","$","i","NULL","NULL","(E)","NULL","NULL"};
stringstr;
interrflag,i;
cout<<"文法:
E->E+T|TT->T*F|FF->(E)|i"<cout<<"请输入分析串(以#结束):
"<do
{errflag=0;
cin>>str;
for(i=0;iif(!
find(str[i],Vt,LENVt))
{
cout<<"输入串中包含有非终结符"<"<}
}while(errflag==1);
analyse(Vn,Vt,M,str);
return0;
}
7、测试及结果
8、心得
通过这次的上机实验,我对语法分析器有了更深刻的印象。
我选的课题是:
设计及实现能够识别表达式的算符优先分析程序。
文法如下:
G[E]:
E->E+T|T
T->T*F|F
P->(E)|i
通过上网查找资料,找到了实现的方法。
对以后的学习有了很大的帮助。
希望可以学到更多关于软件代码开发的技术。