ImageVerifierCode 换一换
格式:DOCX , 页数:46 ,大小:61.85KB ,
资源ID:4890706      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/4890706.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(编译器的语法分析.docx)为本站会员(b****3)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

编译器的语法分析.docx

1、编译器的语法分析佛山科学技术学院 实 验 报 告课程名称 编译原理 实验项目 编译器的语法分析 专业班级 计算机1班 姓 名 学 号 指导教师 黄营 成 绩 日 期 2014/6/7 一、实验目的; 掌握PL语言编译器的语法分析程序设计与LL(1)文法应用的实现方法。二、实验内容;采用递归下降的方法来设计PL/0编译器,证明PL/0语言属于LL(1)文法。然后结合语法图编写(递归下降)语法分析程序的一般方法,具体方面有:(1)用合适的替换将语法约化成尽可能少的单个图;(2)将每一个图按下面的规则(3)-(7)翻译成一个过程说明;(3)顺序图对应复合语句:对应:begin T(S1); T(S2

2、); .; T(Sn) end(4)选择: 对应:case语句或者条件语句: case ch of if ch in L1 then T(S1) else L1: T(S1); if ch in L2 then T(S2) else L2: T(S2); 或 . if ch in Ln then T(Sn) elseLn: T(Sn); error 其中LiFIRST(Si),ch为当前输入符号。(下同)(5)循环: 对应:while ch in L do T(S)(6)表示另一个图A的图:对应:过程调用A。(7)表示终结符的单元图:对应:if ch = x then read(ch) els

3、e error相关过程有:block(), constdeclaration(), vardeclaration(), statement(), condition(), expression(), term(), factor()等。并画出它们之间依赖关系图,并在此基础上实现程序的编制。并适当进行语义分析的相关检查:(1)是否存在标识符先引用未声明的情况;(2)是否存在己声明的标识符的错误引用;(3)是否存在一般标识符的多重声明。三、实验步骤; 1. 将实验一“词法分析”的输出结果,作为表达式语法分析器的输入,进行语法解析,对于语法正确的表达式,报告“语法正确”;并指出该语句属于哪一条文法。

4、 对于语法错误的表达式,报告“语法错误”, 指出错误原因。 2. 把语法分析器设计成一个独立一遍的过程。 3. 语法分析器的编写方法采用递归子程序法。四、程序代码;set.h:#ifndef SET_H#define SET_Htypedef struct snode int elem; struct snode* next; snode, *symset;symset phi, declbegsys, statbegsys, facbegsys, relset;symset createset(int data, ./* SYM_NULL */);void destroyset(symset

5、 s);symset uniteset(symset s1, symset s2);int inset(int elem, symset s);void showset(symset s);#endif/ EOF set.hpl0.h:#include #define NRW 11 / number of reserved words#define TXMAX 500 / length of identifier table#define MAXNUMLEN 14 / maximum number of digits in numbers#define NSYM 10 / maximum nu

6、mber of symbols in array ssym and csym#define MAXIDLEN 10 / length of identifiers#define MAXADDRESS 32767 / maximum address#define MAXLEVEL 32 / maximum depth of nesting block#define CXMAX 500 / size of code array#define MAXSYM 30 / maximum number of symbols #define STACKSIZE 1000 / maximum storagee

7、num symtype SYM_NULL, / 0 SYM_IDENTIFIER, / 1 SYM_NUMBER, / 2 SYM_PLUS, / 3 + SYM_MINUS, / 4 - SYM_TIMES, / 5 * SYM_SLASH, / 6 / SYM_ODD, / 7 odd SYM_EQU, / 8 = SYM_NEQ, / 9 - SYM_LES, / 10 SYM_LEQ, / 11 SYM_GEQ, / 13 = SYM_LPAREN, / 14 ( SYM_RPAREN, / 15 ) SYM_COMMA, / 16 , SYM_SEMICOLON, / 17 ; SY

8、M_PERIOD, / 18 . SYM_BECOMES, / 19 := SYM_BEGIN, / 20 begin SYM_END, / 21 end SYM_IF, / 22 if SYM_THEN, / 23 then SYM_WHILE, / 24 while SYM_DO, / 25 do SYM_CALL, / 26 call SYM_CONST, / 27 const SYM_VAR, / 28 var SYM_PROCEDURE / 29 procedure;enum idtype ID_CONSTANT, ID_VARIABLE, ID_PROCEDURE;enum opc

9、ode LIT, OPR, LOD, STO, CAL, INT, JMP, JPC;enum oprcode OPR_RET, OPR_NEG, OPR_ADD, OPR_MIN, OPR_MUL, OPR_DIV, OPR_ODD, OPR_EQU, OPR_NEQ, OPR_LES, OPR_LEQ, OPR_GTR, OPR_GEQ;typedef struct int f; / function code int l; / level int a; / displacement address instruction;/char* err_msg =/* 0 */ ,/* 1 */

10、Found := when expecting =.,/* 2 */ There must be a number to follow =.,/* 3 */ There must be an = to follow the identifier.,/* 4 */ There must be an identifier to follow const, var, or procedure.,/* 5 */ Missing , or ;.,/* 6 */ Incorrect procedure name.,/* 7 */ Statement expected.,/* 8 */ Follow the

11、 statement is an incorrect symbol.,/* 9 */ . expected.,/* 10 */ ; expected.,/* 11 */ Undeclared identifier.,/* 12 */ Illegal assignment.,/* 13 */ := expected.,/* 14 */ There must be an identifier to follow the call.,/* 15 */ A constant or variable can not be called.,/* 16 */ then expected.,/* 17 */

12、; or end expected.,/* 18 */ do expected.,/* 19 */ Incorrect symbol.,/* 20 */ Relative operators expected.,/* 21 */ Procedure identifier can not be in an expression.,/* 22 */ Missing ).,/* 23 */ The symbol can not be followed by a factor.,/* 24 */ The symbol can not be as the beginning of an expressi

13、on.,/* 25 */ The number is too great.,/* 26 */ Redeclared identifier., / added by yzhang 02-02-28/* 27 */ ,/* 28 */ ,/* 29 */ ,/* 30 */ ,/* 31 */ ,/* 32 */ There are too many levels.;/char ch; / last character readint sym; / last symbol readchar idMAXIDLEN + 1; / last identifier readint num; / last

14、number readint cc; / character countint ll; / line lengthint kk;int err;int cx; / index of current instruction to be generated.int level = 0;int tx = 0;char line80;instruction codeCXMAX;char* wordNRW + 1 = , /* place holder */ begin, call, const, do, end,if, odd, procedure, then, var, while;int wsym

15、NRW + 1 = SYM_NULL, SYM_BEGIN, SYM_CALL, SYM_CONST, SYM_DO, SYM_END, SYM_IF, SYM_ODD, SYM_PROCEDURE, SYM_THEN, SYM_VAR, SYM_WHILE;int ssymNSYM + 1 = SYM_NULL, SYM_PLUS, SYM_MINUS, SYM_TIMES, SYM_SLASH, SYM_LPAREN, SYM_RPAREN, SYM_EQU, SYM_COMMA, SYM_PERIOD, SYM_SEMICOLON;char csymNSYM + 1 = , +, -,

16、*, /, (, ), =, , ., ;#define MAXINS 8char* mnemonicMAXINS = LIT, OPR, LOD, STO, CAL, INT, JMP, JPC;typedef struct char nameMAXIDLEN + 1; int kind; int value; comtab;comtab tableTXMAX;typedef struct char nameMAXIDLEN + 1; int kind; short level; short address; mask;FILE* infile, *outfile;/ EOF PL0.hse

17、t.c:#include #include #include #include set.hsymset uniteset(symset s1, symset s2) symset s; snode* p; s = p = (snode*) malloc(sizeof(snode); / added by yzhang 02-02-28 s1 = s1-next; s2 = s2-next; / end add while (s1 & s2) p-next = (snode*) malloc(sizeof(snode); p = p-next; if (s1-elem elem) p-elem

18、= s1-elem; s1 = s1-next; else p-elem = s2-elem; s2 = s2-next; if ( s2 ) s1 = s2; /added by yzhang 02-02-28 while (s1) p-next = (snode*) malloc(sizeof(snode); p = p-next; p-elem = s1-elem; s1 = s1-next; /* deleted by yzhang 02-02-28 while (s2) p-next = (snode*) malloc(sizeof(snode); p = p-next; p-ele

19、m = s2-elem; s2 = s2-next; */ p-next = NULL; return s; / unitesetvoid setinsert(symset s, int elem) snode* p = s; snode* q; while (p-next & p-next-elem next; q = (snode*) malloc(sizeof(snode); q-elem = elem; q-next = p-next; p-next = q; / setinsertsymset createset(int elem, ./* SYM_NULL */) va_list

20、list; symset s; s = (snode*) malloc(sizeof(snode); s-next = NULL; va_start(list, elem); while (elem) setinsert(s, elem); elem = va_arg(list, int); va_end(list); return s; / createsetvoid destroyset(symset s) snode* p; while (s) p = s; s = s-next; free(p); / destroysetint inset(int elem, symset s) s

21、= s-next; while (s & s-elem next; if (s & s-elem = elem) return 1; else return 0; / inset/added by yzhang 02-02-28void showset(symset s) s = s-next; while (s ) printf(%d, s-elem); s = s-next; printf(n); / showset/ EOF set.cpl0.c: / pl0 compiler source code#include #include #include #include #include

22、 set.h#include pl0.h/ print error message.void error(int n) int i; printf( ); for (i = 1; i = cc - 1; i+) printf( ); fprintf(outfile, ); fprintf(outfile, n); printf(n); fprintf(outfile, Error %3d: %sn, n, err_msgn); printf(Error %3d: %sn, n, err_msgn); err+; / error/void getch(void) if (cc = ll) if

23、(feof(infile) printf(nPROGRAM INCOMPLETEn); exit(1); ll = cc = 0; fprintf(outfile, %5d , cx); printf(%5d , cx); while ( (!feof(infile) / added & modified by alex 01-02-09 & (ch = getc(infile) != n) fprintf(outfile, %c, ch); printf(%c, ch); line+ll = ch; / while fprintf(outfile, n); printf(n); line+l

24、l = ; ch = line+cc; / getch/ gets a symbol from input stream.void getsym(void) int i, k; char aMAXIDLEN + 1; while (ch = | ch = t) / modified by yzhang 02-03-12,add some white space getch(); if (isalpha(ch) / symbol is a reserved word or an identifier. k = 0; do if (k MAXNUMLEN) error(25); / The number is too great. else if (ch = :) getch(); if (ch = =) sym = SYM_BECOMES; / := getch(); else sym = SYM_NULL; / illegal? else if (ch = ) getch(); if (ch = =) sym = SYM_GEQ; / = getch(); else sym = SYM_GTR; / else if (ch = ) getch();

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

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