编译原理课程设计____C语言编译器的实现_第1页
编译原理课程设计____C语言编译器的实现_第2页
编译原理课程设计____C语言编译器的实现_第3页
编译原理课程设计____C语言编译器的实现_第4页
编译原理课程设计____C语言编译器的实现_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、difficult, not hating; blueprints, not rhetoric; words and simple, but full of love, truth, passion, entrepreneurial passion. Take this opportunity, my inspirational quotes Li Dazhaos classic iron specialise in uplifting blend with the article gave everyone, with a view on how to do well under the n

2、ew situation of township and village leaders inspire and help, and also share with you. A good play to create iron shoulders strong arms, to assume the burden be passed; only specific qualities, will it be possible in specific areas to create a world. In my view, village chiefs in the new situation,

3、 we must first have five basic quality. One ambition is to pioneer. Who is handsome. People only high-minded can stimulate the flow of momentum to become brave and persistent, become quiet and powerful. Township and village party leaders, within their respective jurisdictions, political stability an

4、d economic and social development issues, decisions and orientation depends to a large extent because of your. Everybody in village chiefs and the positions, both the trust of the masses, the Organizations trust, more ability to you, fully affirmed the moral and other qualities. Therefore, we must a

5、lways maintain the historical mission and social responsibility, and our ideal tree rooted in the hard work of the soil, with indomitable spirit, indomitable courage to open up a new situation of villages and towns. To have a feeling of closeness to the second. The people and country. Our business f

6、oundation in the peoples blood in the people power people. Advanced decisions of the party cadres to the feelings of the people is an eternal theme. Fan Zhong-yan, called first, and enjoy comfort about others, Zheng Banqiaos also wrote a Ya Chai lay listening to the rustling of bamboo, the suspect i

7、s suffering. Cao County officials, little my, a total turn off. Old of feudal official even has so feelings, alone we is to serving for purposes of people servant does? we more should always hold with on people of deep feelings, heart Department masses of leading visited tea, care masses of emotions

8、 thoughts complained, always insisted put masses of interests put in first, consciously from itself do up, from small do up, from masses most care, and most directly, and most reality of interests problem grabbed, real do love for people by Department, and right for people by with, and Lee for peopl

9、e by conspiracy to. Three with the fame of mind. Non-indifferent not insist your dream, without serenity not go far. Ascension to fame is morality, is the sublimation of the soul. As Township and village, be sure to maintain a normal state of mind, establish a correct viewpoint on power, position an

10、d values and Outlook, as fame is light like water, depending . 50 km, also red and like hundreds of thousands of the KMT military combat, but he went on to collect a variety of newspapers and magazines, and then race against time to pore over. As long as the study of mind, it is not necessary to com

11、e to the library, workshops, into the villages, people learned farming techniques, problem-solving methods, participation in南华大学编译原理课程设计名:编译代生成器设计 专业 计算机科学与技术 学生姓名 熊浩斌 班 级 计算机01班 学 号 指导老师 陈星 实验地点 8栋 2-209 完成日期:2013.6.2一、课程设计的目的 编译原理课程兼有很强的理论性和实践性,是计算机专业的一门非常重要的专业基础课程,它在系统软件中占有十分重要的地位,是计算机专业学生的一门主修课。

12、为了让学生能够更好地掌握编译原理的基本理论和编译程序构造的基本方法和技巧,融会贯通本课程所学专业理论知识,提高他们的软件设计能力,特设定该课程的课程设计,通过设计一个简单的PASCAL语言(EL语言)的编译程序,提高学生设计程序的能力,加深对编译理论知识的理解与应用。二、课程设计的要求1、 明确课程设计任务,复习编译理论知识,查阅复印相关的编译资料。2、 按要求完成课程设计内容,课程设计报告要求文字和图表工整、思路清晰、算法正确。3、 写出完整的算法框架。4、 编写完整的编译程序。 三、课程设计的内容 课程设计是一项综合性实践环节,是对平时实验的一个补充,课程设计内容包括课程的主要理论知识,但

13、由于编译的知识量较复杂而且综合性较强,因而对一个完整的编译程序不适合平时实验。通过课程设计可以达到综合设计编译程序的目的。本课程的课程设计要求学生编写一个完整的编译程序,包括词法分析器、语法分析器以及实现对简单程序设计语言中的逻辑运算表达式、算术运算表达式、赋值语句、IF语句、While语句以及dowhile语句进行编译,并生成中间代码和直接生汇编指令的代码生成器。四、总体设计方案及详细设计总体设计方案:1.总体模块主程序词法分析程序语法分析程序中间代码生成程序2.表2.1 各种单词符号对应的种别码单词符号种别码 单词符号种别码bgin1:17If2:=18Then320wile421do52

14、3lettet(letter|digit)*10=24dight dight*11=25+13;2614(27*15)28/16#0详细设计: 4.1界面导入设计 (1)一共三个选项:choice 1-cifafenxi choice 2-yufafenxi choice 3-zhongjiandaima (2)界面演示 图一 图二 图三 4.2词法分析程序置初值调用扫描子程序输出单词二元组输入串结束结束否是(1)流程图设计(2)具体功能的具体设计1、cifafenxi( ) 首先设置progn来接收输入的语句,以#来结束; 调用扫描子程序 scaner1( ),每一次得到一个类型码; 用sw

15、itch判别相应输出; 直到syn1=0为止。2、扫描子程序scaner1( )-扫描输入的语句 首先设置3个变量:token1用来存放构成单词符号的字符串;sum1用来存放整型单词;syn1用来存放单词符号的类型码。 有关scaner1()中关键点解析: while(ch= )|(ch=n) ch=progp+; ;忽略空格 if(ch=a)|(ch=A) while(ch=a)|(ch=A)|(ch=0)&(ch=9) tokenm+=ch; ch=progp+; ;判别标识符 for(n=0;n=0)&(ch=0)&(ch32=3334=35=36!=37 (2)程序结构描述 (3) 程

16、序的功能描述从文件中读入表达式,输出其四元式的结果序列 是否为main?调用scanner是否为(?调用scanner是否为)?调用scanner调用语句块分析函数staBlock出错处理 递归下降示意图 (4)详细功能描述void scanner(); /扫描void lrparser(); void staBlock(int *nChain); /语句块void staString(int *nChain); /语句串void sta(int *nChain); /语句void fuzhi(); /赋值语句void tiaojian(int *nChain); /条件语句void xunh

17、uan(); /循环语句char* E(); /Expresiion表达式char* T(); /Term项char* F(); /Factor因子char *newTemp(); /自动生成临时变量void backpatch(int p,int t); /回填int merge(int p1,int p2); /合并p1和p2void emit(char *res,char *num1,char *op,char *num2); /生成四元式void emit(char *res,char *num1,char *op,char *num2) 该函数的功能是生成一个三地址语句送到四式表中

18、char *newTemp()该函数的功能是会动一个新的临时变量,临时变量名产生的 顺序是T1,T2,T3,.int merge(int p1,int p2)该函数的功能是将以P1,P2为链首的两条链合并成一条链,返回时的函数值作为合并后的链首。void backpatch(int p,int t)该函数的功能是把P所链接的每个四元式的第四区段(result段)都回 填t。void fuzhi()该函数的功能是对赋值语句进行分析。void tiaojian(int *nChain)该函数的功能是对条件语句进行分析。void xunhuan()该函数的功能是对循环语句进行分析。(4) 结果演示

19、图一 简单语句生成四元式 图二 if语句的四元式生成 图三 循环语句四元式生成(5)汇编生成if(strcmp(fourComi.opera,=)=0) printf(Move AX,%1sn,fourComi.arg1); printf(Move %5s,Axn,fourComi.result);if(strcmp(fourComi.opera,+)=0) printf(Mov AX,%1sn,fourComi.arg1); printf(ADD Ax,%1sn,fourComi.arg2);printf(Mov %1s,Axn,fourComi.result);if(strcmp(four

20、Comi.opera,-)=0) printf(Mov AX,%1sn,fourComi.arg1); printf(SUB Ax,%1sn,fourComi.arg2); printf(Mov %1s,Axn,fourComi.result);if(strcmp(fourComi.opera,*)=0) printf(Mov AL,%1sn,fourComi.arg1); printf(MUL %1sn,fourComi.arg2); printf(Mov %1s,Axn,fourComi.result);if(strcmp(fourComi.opera,/)=0) printf(Mov A

21、X,%1sn,fourComi.arg1); printf(DIv %1sn,fourComi.arg2); printf(Mov %1s,ALn,fourComi.result);if(strcmp(fourComi.opera,goto)=0) printf(jmp L%1sn,i); 结果演示五、课程设计的体会与总结 经过一个星期的编译原理课程设计,本人在陈宏建老师的指导下,顺利完成该课程设 计。通过该课程设计,收获颇多。 词法分析的基本任务是从字符串表示的源程序中识别出具有独立意义的单词符号,其基本思想是根据扫描到单词符号的第一个字符的种类,拼出相应的单词符号。通过本试验的完成,更加加

22、深了对词法分析原理的理解。 通过本次试验,了解了语法分析的运行过程,主程序大致流程为:“置初值”调用scaner函数读下一个单词符号调用IrParse结束。递归下降分析的大致流程为:“先判断是否为begin”不是则“出错处理”,若是则“调用scaner函数”调用语句串分析函数“判断是否为end”不是则“出错处理”,若是则调用scaner函数“判断syn=0&kk=0是否成立”成立则说明分析成功打印出来。不成立则“出错处理”。 一、对实验原理有更深的理解通过该课程设计,掌握了什么是编译程序,编译程序工作的基本过程及其各阶段的基本任务,熟悉了编译程序总流程框图,了解了编译程序的生成过程、构造工具及

23、其相关的技术对课本上的知识有了更深的理解,课本上的知识师机械的,表面的。通过把该算法的内容,算法的执行顺序在计算机上实现,把原来以为很深奥的书本知识变的更为简单,对实验原理有更深的理解。二、对该理论在实践中的应用有深刻的理解通过把该算法的内容,算法的执行顺序在计算机上实现,知道和理解了该理论在计算机中是怎样执行的,对该理论在实践中的应用有深刻的理解。三、激发了学习的积极性通过该课程设计,全面系统的理解了编译原理程序构造的一般原理和基本实现方法。把死板的课本知识变得生动有趣,激发了学习的积极性。把学过的计算机编译原理的知识强化,能够把课堂上学的知识通过自己设计的程序表示出来,加深了对理论知识的理

24、解。以前对与计算机操在这次课程设计中,我就是按照实验指导的思想来完成。加深了理解文件系统的内部功能及内部实现,培养实践动手能力和程序开发能力的目的。 附录-程序清单#include#include#include#includeusing namespace std;#define MAX 100char inputstream50; /存储输入句子int temp1=0; /数组下标int right1; /判断输出信息int m2=0,sum2=0;/sum用于计算运算符的个数 /m用于标记输入表达式中字符的个数 char JG=A;char strMAX;/用于存输入表达式int tok

25、ene=0;/左括号的标志char prog180,token18,ch1;int syn1,p1,m1,n1,sum1;char *rwtab16=begin,if,then,while,do,end;int r1 ;char prog80; /存放所有输入字符 char token8; /存放词组 char ch; /单个字符 int syn,p,m,n,i; /syn:种别编码 double sum; int count; int isSignal; /是否带正负号(0不带,1负号,2正号)int isError;int isDecimal; /是否是小数 double decimal;

26、 /小数 int isExp; /是否是指数 int index; /指数幂 int isNegative; /是否带负号 double temp; int temp2;int repeat; /是否连续出现+,-int nextq;int kk; /临时变量的标号int ntc,nfc,nnc,nnb,nna;char *rwtab9=main,int,float,double,char,if,else,do,while; structchar result10; /字符串(字符数组)char arg110;char opera10;char arg210;fourCom20; /结构体数组

27、 cifafenxi();yufafenxi();zhongjiandaima();scaner1();void e();void e1();void t();void t1();void f();void lrparser(); void staBlock(int *nChain); /语句块void staString(int *nChain); /语句串void sta(int *nChain); /语句void fuzhi(); /赋值语句void tiaojian(int *nChain); /条件语句void xunhuan(); /循环语句char* E(); /Expresii

28、on表达式char* T(); /Term项char* F(); /Factor因子char *newTemp(); /自动生成临时变量void backpatch(int p,int t); /回填int merge(int p1,int p2); /合并p1和p2void emit(char *res,char *num1,char *op,char *num2); /生成四元式void scanner(); /扫描void lrparser()int nChain;nfc=ntc=1;nextq=1;if(syn=1) /mainscanner();if(syn=26) /(scanne

29、r();if(syn=27) /)scanner();staBlock(&nChain);elseprintf(缺少右括号n);else printf(缺少左括号n);elseprintf(缺少mainn);/ := void staBlock(int *nChain) /语句块if(syn=28) /scanner();staString(nChain);/backpatch(*nChain,nextq);if(syn=29) /scanner(); /读下一个elseprintf(缺少号n);elseprintf(缺少号n);/:=;void staString(int *nChain)

30、/语句串sta(nChain);backpatch(*nChain,nextq);while(syn=31) /;scanner();sta(nChain);/backpatch(*nChain,nextq-1);void sta(int *nChain) /语句if(syn=10)fuzhi();/*nChain=0;else if(syn=6) /iftiaojian(nChain);else if(syn=8) /doxunhuan();/-if()void tiaojian(int *nChain)char res10,num110,num210,op10;int nChainTemp

31、;/-if(syn=6) /ifscanner();/strcpy(num1,E();if(syn=26) /(scanner();strcpy(num1,E();if(syn=32) switch(syn)case 32:strcpy(op,);break;case 33:strcpy(op,=);break;case 34:strcpy(op,);break;case 35:strcpy(op,=);break;case 36:strcpy(op,=);break;case 37:strcpy(op,!=);break;default:printf(error);scanner();str

32、cpy(num2,E();strcat(num1,op);strcat(num1,num2);/nfc=nextq+1;ntc=nextq; /记住if语句位置emit(0,if,num1,goto); nfc=nextq; /if中表达式为假emit(0,goto);/第一个0已回填backpatch(ntc,nextq); /ntc链接的所有四元式都回填nextqif(syn=27) /)scanner();staBlock(&nChainTemp); /语句块*nChain=merge(nChainTemp,nfc);/:=do while void xunhuan()char res1

33、0,num110,num210,op10;int nChainTemp;if(syn=8) /donnc=nextq; /记住if语句位置,emit之后nextq就变了/emit(0,if,num1,goto); scanner();staBlock(&nChainTemp); /语句块if(syn=9) /whilescanner();if(syn=26) /(scanner();strcpy(num1,E();if(syn=32) switch(syn)case 32:strcpy(op,);break;case 33:strcpy(op,=);break;case 34:strcpy(o

34、p,);break;case 35:strcpy(op,=);break;case 36:strcpy(op,=);break;case 37:strcpy(op,!=);break;default:printf(error);scanner();strcpy(num2,E();strcat(num1,op);strcat(num1,num2);nnb=nextq;emit(0,if,num1,goto); backpatch(nnb,nnc);nna=nextq;emit(0,goto);backpatch(nna,nextq);if(syn=27) /)scanner();void fuz

35、hi() /赋值语句只有1个操作数char res10,num10; /num操作数if(syn=10) /字符串strcpy(res,token); /结果scanner();if(syn=21) /=scanner();strcpy(num,E();emit(res,num,=,);elseprintf(缺少=号n);char* E() /Expression表达式char *res,*num1,*op,*num2;res=(char *)malloc(10);num1=(char *)malloc(10);op=(char *)malloc(10);num2=(char *)malloc

36、(10);strcpy(num1,T();while(syn=22)|(syn=23) /+ -if(syn=22) /+strcpy(op,+);elsestrcpy(op,-);scanner();strcpy(num2,T();strcpy(res,newTemp();emit(res,num1,op,num2);strcpy(num1,res);return num1;char* T() /Term项char *res,*num1,*op,*num2;res=(char *)malloc(10);num1=(char *)malloc(10);op=(char *)malloc(10)

37、;num2=(char *)malloc(10);strcpy(num1,F();while(syn=24)|(syn=25) /* /if(syn=24) strcpy(op,*);elsestrcpy(op,/);scanner();strcpy(num2,F();strcpy(res,newTemp();emit(res,num1,op,num2);strcpy(num1,res);return num1;char* F() /Factor因子char *res;res=(char *)malloc(10);if(syn=10) /字符串strcpy(res,token);scanner();else if(syn=20) /二进制数itoa(int)sum,res,10); /整数转换为字符串scanner();else if(syn=26) /(scanner();res=E();if(syn=27) /)scanner();else isError=1;elseisError=1;return res;char *newTemp()char *p;char varTemp10;p=(char *)mallo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论