《C-编译器设计报告》由会员分享,可在线阅读,更多相关《C-编译器设计报告(15页珍藏版)》请在金锄头文库上搜索。
1、课程实验报告(全日制硕士研究生)课程名称程序语言与编译实验名称C-编译器设计专业、班级计算机科学与技术4班学生张洁坤学号 S07102114同组学生及学号无指导教师杨晓波目录第1章实验介绍及整体框架31.1实验目的:31.2实验环境:31.3 C-编译器的整体框架3第2章词法分析42.1词法分析包括两个类:42.2 C关键字表:523标识符词法:5第3章语法分析63.1 Class CParser: 63.2 Grammar: 63.3基本树形结构:73.4支持的语句及运算:8第4章建立符号表94.1辅助类:94.2主要的类,建立符号表:9第5章类型检测10第6章代码生成n6.1 PCode:
2、 116.2 80X86 ASM: 12第7章总结13参考文献15第1章实验介绍及整体框架1.1实验目的:通过实验,加深对所学的关于程序语言与编译的理论知识的理解,增强对所 学知识的综合应用能力。通过本实验,进一步明确编译各阶段之间的关系,掌握 词法分析、语法分析、语义分析等实现技术及其实现,熟悉符号表的管理及其在 编译过程中的作用,掌握错误处理机制及其应用。1.2实验环:硬件:主机:586以上,配有鼠标, 内存:256MB以上 显示器:VGA或以上 硬盘空间:500MB以上软件:Microsoft Visual C+ 6.01. 3 C-编译器的整体框架结柬第2章词法分析2.1词法分析包括两
3、个类:(1) Class CTokenizer:从一个字符串中(这个把一个文件看作是一个字符 串,MFC中CFile-CString)分离出一个一个token,配上简单的类型通过 NextToken ()返回:define TT_EOL n* #defme TT_EOF -1 #dcfinc TT.INTEGER -2 #define TT.REAL-3#define TT_WORD-4#define TT_STRING #define TT.CHAR丫(2) Class CScaner:得到具体的的token类型,定义TokenType如下:enum TokenType/ reserved
4、Keyword_AUTO, .DOUBLE, _INT, _STRUCT,.BREAK, .ELSE, .LONG, SWITCH,_CASE, _ENUM, .REGISTER, _TYPEDEF,_CHAR, .EXTERN, _RETURN, .UNION,.CONST, .FLOAT, .SHORT, .UNSIGNED,.CONTINUE, _FOR, _SIGNED, _VOID,.DEFAULT, 一GOTO, 一S1ZEOK .VOLATILE,_DO, JF, 一STATIC,.WHILE,_READ, _WRITE, _PRINTF,/ operationsASSIGN,
5、PLUS, MINUS, TIMES, DIV, MOD,BITWISE_AND, BITWISE_OR, BITWISE_NOT, LOGICAL_NOT, LT, GT,/ intcrpunctionsLPARAN, RPARAN, LBRACE,RBRACE,LSQUARE, RSQUARE,COMMA, DOT, SEMI, COLON,/ complex operationsEQ/* = */, NEQ/* != */, PLUS一PLUS/* + */,MINUS_MINUS/* - */,PLUS.ASSIGNZ* += MINUS_ASSIGN/* -= */,TIMES_AS
6、SIGN/* *= */, DIV_ASSIGN/* /= */, NGT/* = */, LOGICAL一AND/* & */,LOGICAL一0R7* | */, / others_EOF,_ID, _NUM, .STRING, .CHARACTER, _LABEL, .ERROR, _NONE);CScaner 通过一个 CMapdeclaration_list2. declaration_list-declaration_list declaration | declaration3. declaration-var_declaration | fun_declaration4. va
7、r_declaration-type_specifier ID(, ); | type_specifier ID NUM (,5. type一specifier-inr | void | char, actually this step is in declarationlist()6. fun_declaration-type_specifier ID 丫 params compound_stmt7. params-param_list | void | empty, void is thought as empty8. param_list-param_list , param | par
8、am9. param-type_specifier ID | type_specifier ID10. compound_stmt-丫 loal一declarations statementist 丁 | expression一stmt11. Iocal_declarations-local_declarations var_declaration | var_declaration12. read ( var13. write ( expression)14. printf 7STRING15. expression_stmt-expression f |16. expression-var
9、expression | Iogic1_expression17. Iogic1_expression-logic1_expression |P Iogic2_expression | Iogic2_expression18. logic2_expression- Iogic2_expression & simpleexpression 丨 simpleexpression19. simple一expression-additive_expression relop additiveexpression | additive一expression20. relop- = | | = | = |
10、 !=21. additive_expression - additive_expression addop term | term22. addop- + |23. term-term mulop Iogic3_expression | Iogic3_expression24. mulop- | / | %25. logic3_expression- P Iogic3_expression | factor26. factor-( expression )、| var | call | NUM27. var-ID | ID expression 28. call-ID ( args )29.
11、 args-args_list | empty30. argsist-args_list , expression | expression31. sub_compoundstmt-ID| call | expression_stmt32. if_stmt-、r 丫 expression 丁 compound_stmt| if 丫 expression 丁 compound_stmt else compound_stmt33. while_stmt-while expression compound_stmt34. for_stmt-for 丫 var expression V expression V var expression ) compound_stmt35. goto_stmt-goto ID;36. break_stmt-break37. continue_stmt- continue f38. return_stmt-return | return