《编译原理教程》习题解析与上机指导(胡元义) 第十一章 小型编译程序

上传人:E**** 文档编号:89349157 上传时间:2019-05-23 格式:PPT 页数:247 大小:930KB
返回 下载 相关 举报
《编译原理教程》习题解析与上机指导(胡元义) 第十一章  小型编译程序_第1页
第1页 / 共247页
《编译原理教程》习题解析与上机指导(胡元义) 第十一章  小型编译程序_第2页
第2页 / 共247页
《编译原理教程》习题解析与上机指导(胡元义) 第十一章  小型编译程序_第3页
第3页 / 共247页
《编译原理教程》习题解析与上机指导(胡元义) 第十一章  小型编译程序_第4页
第4页 / 共247页
《编译原理教程》习题解析与上机指导(胡元义) 第十一章  小型编译程序_第5页
第5页 / 共247页
点击查看更多>>
资源描述

《《编译原理教程》习题解析与上机指导(胡元义) 第十一章 小型编译程序》由会员分享,可在线阅读,更多相关《《编译原理教程》习题解析与上机指导(胡元义) 第十一章 小型编译程序(247页珍藏版)》请在金锄头文库上搜索。

1、第十一章 小型编译程序,11.1 高级语言到四元式的编译程序PAS 11.2 四元式到汇编语言程序的编译程序COMPILER,11.1 高级语言到四元式的编译程序PAS,/*/ /* pas.c */ /* 高级语言到四元式 */ /*/,#include tdio.h #include string.h #define ACC -2 /*/ #define sy_if 0 #define sy_then 1 #define sy_else 2,#define sy_while 3 #define sy_begin 4 #define sy_do 5 #define sy_end 6 #def

2、ine a 7 #define semicolon 8 #define e 9,#define jinghao 10 #define S 11 #define L 12 #define tempsy 15 #define EA 18 /*E and*/ #define EO 19 /*E or*/,#define plus 34 #define times 36 #define becomes 38 #define op_and 39 #define op_or 40 #define op_not 41 #define rop 42 #define lparent 4 #define rpar

3、ent 49 #define ident 56 #define intconst 57,/*/ char ch=0 ; /*当前字符*/ int count=0; static char spelling10= ; /*存放识别的字*/ static char line81= ; /*一行字符缓冲区*/ char *pline ; /*字符缓冲区指针*/,static char ntab110010; struct ntab int tc; int fc; ntab2200; int label=0; /*存放临时变量的表的定义 */,struct rwords char sp10; int

4、sy; ; /*存放文件的结构*/ struct rwords reswords10= if,sy_if, do,sy_do, else,sy_else, while,sy_while, then,sy_then,begin,sy_begin, end,sy_end, and,op_and, or,op_or, not,op_not; struct aa int sy1; int pos; buf1000; /*词法分析结果缓冲区*/,n ;/*当前字符*/ n1 ;/*当前表达式中的字符*/ E ;/*非终结符*/ sstack100 ;/*符号栈*/ ibuf100, stack1000;

5、 struct aa oth;,struct fourexp char op10; struct aa arg1; struct aa arg2; int result; fexp200; /*四元式的结构*/ int ssp=0 ; /*指向sstack100*/,structaa *pbuf=buf ; /*指向词法分析缓冲区*/ int nlength=0; int lnum=0 ; /*源程序长度*/ int tt1=0; FILE *cfile; FILE *mfile;,/*/ int newt=0; /*临时变量*/ int nxq=100; /*nxq指向下一个形成的四元式的地

6、址*/ int lr; int lr1; int sp=0;,/* 状态栈定义*/ int stack1100; int sp1=0; /*状态栈1的定义*/ int num=0; struct ll int nxq1; int tc1; int fc1; labelmark10;,int labeltemp10; int pointmark=-1,pointtemp=-1; int sign=0; /*sign1,表达式为赋值语句;sign2,表达式为布尔表达式。*/ /*/ static int action1913= /*0*/ 2,-1,-1,3,4,-1,-1,5,-1,-1,-1,

7、1,-1, /*1*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,ACC,-1,-1, /*2*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,6,-1,-1,-1,/*3*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,7,-1,-1,-1, /*4*/ 2,-1,-1,3,4,-1,-1,5,-1,-1,-1,9,8, /*5*/ -1,-1,104,-1,-1,-1,104,-1,104,-1,104,-1,-1, /*6*/ -1,10,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /*7*/ -1,-1,-1,-1,-1,1

8、1,-1,-1,-1,-1,-1,-1,-1, /*8*/ -1,-1,-1,-1,-1,-1,12,-1,-1,-1,-1,-1,-1, /*9*/ -1,-1,-1,-1,-1,-1,105,-1,13,-1,-1,-1,-1, /*10*/ 2,-1,-1,3,4,-1,-1,5,-1,-1,-1,14,-1, /*11*/ 2,-1,-1,3,4,-1,-1,5,-1,-1,-1,15,-1,/*12*/ -1,-1,103,-1,-1,-1,103,-1,103,-1,103,-1,-1, /*13*/ 2,-1,-1,3,4,-1,-1,5,-1,-1,-1,9,16, /*14*

9、/ -1,-1,17,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /*15*/ -1,-1,102,-1,-1,-1,102,-1,102,-1,102,-1,-1, /*16*/ -1,-1,-1,-1,-1,-1,106,-1,-1,-1,-1,-1,-1, /*17*/ 2,-1,-1,3,4,-1,-1,5,-1,-1,-1,18,-1, /*18*/ -1,-1,101,-1,-1,-1,101,-1,101,-1,101,-1,-1;,static int action1107= /*0*/ 3,-1,-1,2,-1,-1,1, /*1*/ -1,4,5,-1,-

10、1,ACC,-1, /*2*/ 3,-1,-1,2,-1,-1,6, /*3*/ 104,104,104,104,104,104,-1, /*4*/ 3,-1,-1,2,-1,-1,7, /*5*/ 3,-1,-1,2,-1,-1,8, /*6*/ -1,4,5,-1,9,-1,-1, /*7*/ 101,101,5,101,101,101,-1, /*8*/ 102,102,102,102,102,102,-1, /*9*/ 103,103,103,103,103,103,-1;,static int action21611= /*0*/ 1,-1,4,-1,5,-1,-1,-1,13,7,

11、8, /*1*/ -1,2,-1,101,-1,101,101,101,-1,-1,-1, /*2*/ 3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /*3*/ -1,-1,-1,102,-1,102,102,102,-1,-1,-1, /*4*/ 1,-1,4,-1,5,-1,-1,-1,11,7,8,/*5*/ 1,-1,4,-1,5,-1,-1,-1,6,7,8, /*6*/ -1,-1,-1,104,-1,9,10,104,-1,-1,-1, /*7*/ 1,-1,4,-1,5,-1,-1,-1,14,7,8, /*8*/ 1,-1,4,-1,5,-1,-1,-1,

12、15,7,8, /*9*/ 105,-1,105,-1,105,-1,-1,105,-1,-1,-1, /*10*/ 107,-1,107,-1,107,-1,-1,107,-1,-1,-1,/*11*/ -1,-1,-1,12,-1,9,10,-1,-1,-1,-1, /*12*/ -1,103,-1,103,-1,103,103,103,-1,-1,-1, /*13*/ -1,-1,-1,-1,-1,9,10,ACC,-1,-1,-1, /*14*/ -1,-1,-1,106,-1,9,10,106,-1,-1,-1, /*15*/ -1,-1,-1,108,-1,9,10,108,-1,

13、-1,-1; /*从文件读一行到缓冲区*/,readline( ) char ch1; pline=line; ch1=getc(cfile); while (ch1!= n) *pline=ch1; pline+; ch1=getc(cfile); ,*pline=0; pline=line; /*从缓冲区读一个字符*/ readch( ) if (ch= 0) readline( ); lnum+; ,ch=*pline; pline+; /*标志符和关键字的识别*/ find(char spel ) int ss1=0; int ii=0; while(ss1=0)&(iinlength

14、) ,if (!strcmp(spel,ntab1ii) ss1=1; ii+; if (ss1=1) return ii-1; else return -1; ,identifier( ) int iii=0,j,k; int ss=0; k=0; do spellingk=ch; k+; readch( ); while(ch=a),pline-; spellingk= 0; while(ss=0) ,/*关键字匹配*/ if(ss=1) bufcount.sy1=reswordsiii-1.sy; else bufcount.sy1=ident; j=find(spelling); if

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号