编译原理实验1.docx
- 文档编号:17870802
- 上传时间:2023-08-04
- 格式:DOCX
- 页数:19
- 大小:69.95KB
编译原理实验1.docx
《编译原理实验1.docx》由会员分享,可在线阅读,更多相关《编译原理实验1.docx(19页珍藏版)》请在冰点文库上搜索。
编译原理实验1
实验报告
学生姓名:
学号:
指导教师:
实验地点:
实验室610实验时间:
2010年12月17日
1、实验室名称:
实验室610
2、实验项目名称:
编译原理分析器设计
3、实验学时:
8
四、实验目的:
在学习《编译原理》课程过程中,结合各章节的构造编译程序的基本理论,总共用8个学时完成实验。
要求用C或C++语言描述及上机调试,实现一个小编译程序的一部分(包括词法分析,语法分析子程序),使学生将理论与实际应用结合起来,受到软件设计等开发过程的全面训练,从而全面的掌握编译原理课程内容,提高学生软件开发的能力。
五、实验内容:
(1)设计词法分析器
设计各单词的状态转换图,并为不同的单词设计种别码。
将词法分析器设计成供语法分析器调用的子程序。
功能包括:
a.具备预处理功能。
将不翻译的注释等符号先滤掉,只保留要翻译的符号串,即要求设计一个供词法分析调用的预处理子程序;
b.能够拼出语言中的各个单词;
c.将拼出的标识符填入符号表;
d.返回(种别码,属性值)。
(2)语法分析
要求用SLR分析法,实现对表达式、各种说明语句、控制语句进行语法分析。
若语法正确,则用语法制导翻译法进行语义翻译:
对说明语句,要求将说明的各符号记录到相应符号表中;对可执行语句,应产生出四元式中间代码并填写到三地址码表中;
若语法错误,要求指出出错性质和出错位置(行号)。
出错处理应设计成一个出错处理子程序。
六、实验器材(设备、元器件):
计算机、VC++6.0
七、实验步骤:
词法分析器:
#include
#include
#include
#include
#include
charprog[80]={'\0'},
token[8];
charch;
intsyn,n,sum,m,p;
char*rwtab[8]={"begin","if","then","while","int","else","void","return"};
voidscaner(){
m=0;
sum=0;
for(n=0;n<8;n++)
token[n]='\0';
ch=prog[p++];
while(ch=='')
ch=prog[p++];
if(isalpha(ch)){
while(isalpha(ch)||isdigit(ch)){
token[m++]=ch;
ch=prog[p++];}
token[m++]='\0';
ch=prog[p--];
syn=10;
for(n=0;n<6;n++)
if(strcmp(token,rwtab[n])==0){
syn=n+1;
break;}}
else
if(isdigit(ch)){
while(isdigit(ch)){
sum=sum*10+ch-'0';
ch=prog[p++];}
ch=prog[p--];
syn=11;}
else
switch(ch){
case'<':
cout< m=0;token[m++]=ch;ch=prog[p++]; if(ch=='>'){ syn=21; token[m++]=ch;} elseif(ch=='='){ syn=22; token[m++]=ch;} else{ syn=20; ch=prog[p--];} break; case'>': cout< m=0;token[m++]=ch;ch=prog[p++]; if(ch=='='){ syn=24; token[m++]=ch;} else{ syn=23; ch=prog[p--];} break; case': ': cout< m=0;token[m++]=ch;ch=prog[p++]; if(ch=='='){ syn=18; token[m++]=ch;} else{ syn=17; ch=prog[p--];} break; case'+': syn=13;token[0]=ch;cout< case'-': syn=14;token[0]=ch;cout< case'*': syn=15;token[0]=ch;cout< case'/': syn=16;token[0]=ch;cout< case'=': syn=25;token[0]=ch;cout< case';': syn=26;token[0]=ch;cout< case'(': syn=27;token[0]=ch;cout< case')': syn=28;token[0]=ch;cout< case'#': syn=0;token[0]=ch;cout< default: syn=-1;}} intmain(){ p=0; printf("\npleaseinputstring: \n"); do{ch=getchar(); prog[p++]=ch; }while(ch! ='#'); p=0; do{ scaner(); switch(syn){ case11: printf("(%d,%d)\n",syn,sum);break; case-1: printf("\nERROR;\n");break; default: printf("(%d,%s)\n",syn,token); } }while(syn! =0); cout<<""< return0; } 语法分析器: #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; } 八、实验数据及结果分析: 九、实验结论: 语法分析器的任务是识别和处理比单词更大的语法单位,如: 程序设计语言中的表达式、各种说明和语句乃至全部源程序,指出其中的语法错误;必要时,可生成内部形式,便于下一阶段处理。 词法分析器对输入的每个单词或符号进行分析归类,主要分析每个词的词性如属于关键词,运算符,变量名等等 十、总结及心得体会: 通过此次实验,让我了解到如何设计、编写并调试词法与语法分析部分的相关程序,并加深对词法分析与语法分析原理的理解;熟悉了构造词法分析及语法分析程序的手工方式的相关原理,使用某种高级语言(例如C++语言)直接编写此法分析程序。 另外,通过本次实验,也让我重新熟悉了C++语言的相关内容,加深了对C++语言知识的深化和用途的理解。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 编译 原理 实验
![提示](https://static.bingdoc.com/images/bang_tan.gif)