




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、人郭數大吟实验报告学院(系)名称: 计算机与通信工程学院姓名*学号*专业计算机科学与技术班级*实验项目实验一:词法分析课程名称编译原理课程代码0669036实验时间*实验地点软件实验室7-215批改意见成绩教师签字:实验内容:实现标准C语言词法分析器 实验目的:1 掌握程序设计语言词法分析的设计方法;2 .掌握DFA的设计与使用方法;3 .掌握正规式到有限自动机的构造方法; 实验要求:1 .单词种别编码要求基本字、运算符、界符:一符一种;标识符:统一为一种; 常量:按类型编码;2 .词法分析工作过程中建立符号表、常量表,并以文本文件形式输出;3 .词法分析的最后结果以文本文件形式输出;4 .完
2、成对所设计词法分析器的功能测试,并给出测试数据和实验结果;5.为增加程序可读性,请在程序中进行适当注释说明;6 .整理上机步骤,总结经验和体会;7 .认真完成并按时提交实验报告。【实验过程记录(源程序、测试用例、测试结果及心得体会等)】本次实验制作了一个简易的c语言词法分析器,gui部分用qt编写,LexicaI词法分析 类用标准C+编写,最后将分析结果写入txt文件。第25页共41页图形界面语法分析新建打开保存I ndinciudEhaKeyword1irrt2储Lu刑|5WentfiarnumliermjinNuirberward number1词法分(预留)语义分析PrflDefrie(
3、预留)pr intf ChclI worldnllI);/print or I dreturn 0;ndexKeywordIodi/tCiUhr*Ar代码编辑框分析结果现实OperatornunTibtrvdlutnumbervaluevalue|5numbervalue乍彳0 e E1乍Ee=2!jMkr-11E E e4(E E E erE-k-E1E2E3E4E5E61 e -=eEEEE+v E&”5e E E 1E_% 7I,JHhbeEEEE冷。TTTeEE- 11Tv-M3IIE E E eij15E经过简化后的DFAE6词法分析测试源代码:#i nclude int main(
4、)prin tf(hello worldn!);/pri nt hello world return 0;词法分析结果文件:source file :D:/programmi ng/qt/qtVSProject/Complier/Complier/hello.c*sourceCode :*#in clude int mai n() prin tf(hello worldn! !);/pri nt hello world return 0;* Identifier* * mainprintf*Number:*Keywords:*intreturn*Precomplied:*#in clude*s
5、ystemHeader:*Delimiters:*()()*Operators *Error:*Stri ngs *hello worldn!*Chars *Words:*sourcePrecompiledssystemHeadersourceKeywordssourcelde ntifiersourceDelimiters sourceDelimiters sourceDelimiters sourcelde ntifiersourceDelimiterssourcePreStri ngsourceDelimiters sourceDelimiters sourceKeywords sour
6、ceNumbersourceDelimiters sourceDelimiters关键代码:/*Lexical.h*/#pragma once#in clude #in clude namespace Complierclass Lexicalpublic :Lexical (void );Lexical (std : string data ); Lexical (void );public :void setEachLines (); void setEachWords (); void setSourceStack ();std :stringsourceCode ;std :vecto
7、rsourceStack ;std :vectorsourceWords ;std :vectorsourceL ines ;std :vectorsourcePreStri ngs ;std :vectorsourcePreChars ;std :vectorsystemHeaders ;std :vectorsourceKeywords ;std :vectorsourceOperators ;std :vectorsourcePrecompileds ;std :vectorsourceDelimiters ;std :vectorsourceNumber;std :vectorsour
8、ceIde ntifier;std :vectorsourceError ;std :vectorsourceErrorDescribeprivate :std :vector replaceStringCode;/替换字符串后的源代码std :vector removeAnnotationCode ; /除去注释后的源代码std :vector splitSpaceCode ; /按空格制表符拆分后的代码std :vector splitDelimitersCode;/按界符三元运算符拆分后的代码std :vector splitOperatorCode;/按运算符拆分后的代码std :ve
9、ctor replaceHeaderCode;/替换系统头文件后的代码std :vector replaceNumberCode;/替换数字后的代码std :vector replaceIdentifierCode;/替换数字后的代码std :vector replaceKeywordCode;/替换关键字后的代码std :vector replaceOperatorCode;/替换运算符后的代码std :vector replacePrecompiledsCode ; / 替换预编译指令后的代码std :vector replaceDelimitersCode; / 替换界符后的代码priv
10、ate :voidreplaceString (); /替换字符串voidremoveA nno tati on ()/除去注释voidsplitSpace (); /按空格制表符拆分voidsplitDelimiters();/按界符三元运算符拆分voidsplitOperator (); /按运算符拆分voidreplaceHeader (); /替换系统头文件voidreplaceKeyword (); / 替换关键字voidreplaceOperator ();/替换运算符voidreplacePrecompileds();/替换预编译指令voidreplaceDelimiters:)
11、;/替换界符voidreplaceNumber (); /替换数字voidreplaceIde ntifier:);/替换标示符voidlexical (); /词法分析;/*PreDefi ne.h*/#pragma once#in elude#in cludenamespace Complierclass PreDefi nepublic :PreDefine (void );PreDefine ( void );enum Keyword keyword_beg in= 0,keyword_auto = 0, keyword break= 1,46 /bool不是C吾言的关键字,表示特殊,
12、加进去了keyword_case:keyword_char:keyword_c onst keyword_c on ti nue keyword_default keyword_double keyword_do = keyword_else : keyword_e num:keyword_exter n keyword_float keyword_for=keyword_goto:keyword_if = keyword_ int=keywordong:keyword_register keyword_return keyword_short keyword_sig ned keyword_
13、sizeof keyword_static keyword_struct keyword_switch keyword_typedef keyword_u nion keyword_u nsig ned keyword_void : keyword_volatile keyword_while keywordn li ne keyword_restrict keyword_Bool : keyword_Complex keyword_Imagi nary keyword_Alig nas keyword_Alig nof keyword_Atomic keyword_Static keywor
14、d_assert keyword_Noretur n keyword_Thread keyword_local keyword_Ge neric keyword bool:2,3,/char=4,=5,=6,=7,/ double8,9,10, / enum=11,=12,/float13,14,15,16, /int17, /long=18,=19,=20, /short=21,=22,=23,=24, /struct=25,=26,=27,/union=28,29,=30,=31,=32,=33,34, =35,=36,=37,=38,=39,=40,=41,=42,=43,=44, =4
15、5,;enum Operatoroperator_beg in=47,/* defi ne operator 1st定义第一级运算符*/operator_pare nthesis=47,/圆括号()operator_subscript=48,/下标运算符operator_po in terOper=49,/ -指向结构体成员运算符operator_member=50,/ .结构体成员运算符/* defi ne operator 2nd定义第二级运算符*/operator, negati on=51,/逻辑非!operator_bitwiseNOT=52,/按位取反operator_ in cr
16、eme nt=53,/增量+operator_decreme nt=54,/减量-operator_ minus=55,/负号-operator_type=56,/强制类型转换()operator_po in ter=57,/指针运算符*operator_address=58,/地址运算符&/* defi ne operator 3rd定义第三级运算符*/operator_multiply=59,/乘法运算符*operator_divide=60,/除法运算符/operator_mod=61,/取余运算符%/* defi ne operator th定义第四级运算符*/operator_add
17、=62,/加法运算符operator_sub=63,/减法运算符/* defi ne operator th定义第五级运算符*/operator_leftShift=64,/ 左移运算符operator_rightShift=65,/ 右移运算符/* defi ne operator th定义第六级运算符*/operator_greater=66,/大于operator_greaterEqual=67,/大于等于operator_less=68,/小于operator_lessEqual=69,/小于等于/* defi ne operator th定义第七级运算符*/operator_equa
18、l=70,/等于operator_ no tEqual=71,/不等于/* defi ne operator th-13th定义第八级到第十三级运算符*/operator_bitwiseAND=72,&按位与运算符。operator_bitwiseXOR=73,A按位异或运算符。operator_bitwiseOR=74,|按位或运算符。operator_AND=75,/&逻辑与运算符。operator_OR=76,|逻辑或运算符。operator_c on diti on=77,/?:条件运算符。/* defi ne operator 14th定义第十四级运算符赋值运算符*/operator
19、 evaluati on=78,/ =operator_evaluation PLUS=79,/ +operator_evaluati on SUB=80,/ -operator_evaluati onMUL=81,/ *operator_evaluation DIV=82,/ /operator_evaluati onMOD=83,/ %operator_evaluati onADD=84,/ &operator_evaluati onXOR=85,/人operator_evaluati onOR=86,/ Ioperator_evaluati onlSF=87,/ /* defi ne o
20、perator 15th定义第十五级运算符*/operator_commaOper;=89/逗号运算符。enum Precompiledprecompiled_begi n=90,/* defi ne Precomiled定义预编译指令*/precompiled_sharp=90,/ #precompiled_doubleSharp=91,/ #precompiled_ in clude=92,/ #i ncludeprecompiled_defi ne=93,/ #defi neprecompiled_u ndef=94,/ #un defprecompiled_preIF=95,/ #if
21、precompiled_ifdef=96,/ #ifdefprecompiled_if ndef=97,/ #ifndefprecompiled_elif=98,/ #elifprecompiled_e ndif=99,/ #en difprecompiled_error=100,/ #errorprecompiledine=101,/ #li neprecompiled_pragma;=102/ #pragmaenum Delimitersdelimiters_begi n=103,/* define Delimiters定义界符*/delimiters_comma=103,/,逗号deli
22、miters_semico lon=104,/;分号delimiters_leftBrace=105,/左大括号delimiters_rightBrace=106,/右大括号delimiters_leftBracket=107,/左中括号delimiters_rightBracket=108,/右中括号delimiters_leftPare nthesis=109,/(左小括号delimiters_rightPare nthesis=110,/)右小括号delimiters_leftA ngle=111,/左尖括号delimiters rightA ngle=112/右尖括号;enum Ide
23、ntifieride ntifier_stri ng= 113;enum Numbern umber = 114;std :vector allValue ;std :vector keywordValue ;std :vector operatorValue ;std :vector precompiledValuestd :vector delimitersValue; /*PreDefi ne.cpp*/ #i ncludePreDefi ne.h#in cludeusing namespace Complier ; PreDefine : PreDefine (void ) / key
24、wordkeywordValue . push_back (auto );/0keywordValue . push_back (break); keywordValue . push_back (case); keywordValue . push_back (char); keywordValue . push_back (c on st); keywordValue . push_back (continue); keywordValue . push_back (default ); keywordValue . push_back (double); keywordValue . p
25、ush_back (do);keywordValue . push_back (else); keywordValue . push_back (enum); keywordValue . push_back (extern); keywordValue . push_back (float ); keywordValue . push_back (for); keywordValue . push_back (goto); keywordValue . push_back (if); keywordValue . push_back (int); keywordValue . push_ba
26、ck (long); keywordValue . push_back (register ); keywordValue . push_back (return); keywordValue . push back (short);keywordValuepush_back (signed);keywordValue.push_back (sizeof);keywordValuepush_back (static );keywordValuepush_back (struct );keywordValuepush_back (switch);keywordValuepush_back (ty
27、pedef);keywordValuepush_back (union);keywordValuepush_back (unsigned);keywordValuepush_back (void);keywordValuepush_back (volatile);keywordValuepush_back( while);keywordValuepush_back (inline);keywordValuepush_back (restrict);keywordValuepush_back (_Bool);keywordValuepush_back (_Complex);keywordValu
28、epush_back (_Imag in ary);keywordValuepush_back (_Alignas);keywordValuepush_back (_Alignof);keywordValuepush_back (_Atomic);keywordValuepush_back (_Static);keywordValuepush_back (_assert);keywordValuepush_back (_Noreturn);keywordValuepush_back(_Thread);keywordValuepush_back (_local);keywordValuepush
29、_back (_Generic);keywordValuepush_back (bool);/operatoroperatorValue.push_back ();operatorValue.push_back ();operatorValue.push_back (-);operatorValue.push_back (.);operatorValue.push_back (!);operatorValue.push_back ();operatorValue.push_back (+);operatorValue.push_back (-);operatorValue.push_back
30、(-);operatorValue.push_back ();operatorValue.push_back (*);operatorValue.push_back (&);operatorValue.push_back (*);operatorValue.push_back (/);operatorValue.push_back (%);operatorValue.push_back (+);operatorValue.push_back (-);operatorValue.push back ();operatorValuepush_back ();operatorValue .push_
31、back (=);operatorValue .push_back ();operatorValue .push_back (=);operatorValue .push_back (=);operatorValue .push_back (!=);operatorValue .push_back (&);operatorValue .push_back (A);operatorValue .push_back (|);operatorValue .push_back (&);operatorValue .push_back (|);operatorValue .push_back (?:);
32、operatorValue .push_back (=);operatorValue .push_back (+=);operatorValue .push_back (-=);operatorValue .push_back (*=);operatorValue .push_back (/=);operatorValue .push_back (%=);operatorValue .push_back (&=);operatorValue .push_back (a=);operatorValue .push_back (|=);operatorValue .push_back (=);op
33、eratorValue .push_back (,);/precomilpedprecompiledValue.push_back (#);precompiledValue.push_back (#);precompiledValue.push_back (#include);precompiledValue.push_back (#define);precompiledValue.push_back (#undef);precompiledValue.push_back (#if);precompiledValue.push_back (#ifdef);precompiledValue.pu
34、sh_back (#ifndef);precompiledValue.push_back (#elif);precompiledValue.push_back (#endif);precompiledValue.push_back (#error);precompiledValue.push_back (#line);precompiledValue.push_back (#pragma);/delimitersdelimitersValue.push_back (,);delimitersValue.push_back (;);delimitersValue.push_back ();del
35、imitersValue.push_back ();delimitersValue.push back ();delimitersValue delimitersValue delimitersValue delimitersValue delimitersValue.push_back();.push_back();.push_back ();.push_back ();for ( int i = 0; i keywordValue . size (); i +)allValue . push_back (keywordValue i );for ( int i = 0; i operato
36、rValue . size (); i +)allValue . push_back (operatorValue i );for (int i = 0; i precompiledValue . size (); i +)allValue . push_back (precompiledValue i );for ( int i = 0; i #i ncludevQtGui/QSta ndardltemModel#i ncludeGe neratedFiles/ui_ComplierForm.h#i ncludeLexical.hclass Mai nF orm : public QMai
37、nWin dowQ_OBJECTpublic :MainForm( void );MainForm(void );public slots :voidnewFile ();boolOpenFile ();boolSaveFile ();boolSaveAs();boolSaveAll ();boolUn do();bool Redo();void Lexical ();void Syntax ();void Semantic ();void About ();private :void acti on Save ();void setTablePrekeyword ();void setTab
38、lePreoperator ();void setTablePrePrecomiled();void setTablePreDelimiters();void setTableLines(Complier : Lexical * lex );void setTableWords (Complier : Lexical * lex );void setTableSourceStack (Complier : Lexical * lex ); void setTableKeyword (Complier : Lexical * lex );void setTableOperator (Compli
39、er : Lexical * lex ); void setTablePrecompiled (Complier : Lexical * lex );void setTableDelimiters (Complier : Lexical * lex ); void setTableNumber (Complier : Lexical * lex );void setTableError (Complier : Lexical * lex ); void setTableldentifier (Complier : Lexical * lex );private :Ui: Complier *
40、mainUI ;QStr ing filePath ;QString sourcePath ;Complier : Lexical * lex ;;/*Mai nForm.cpp*/#i ncludeMai nForm.h#i ncludeGe neratedFiles/Debug/moc_Mai nF orm.h#i ncludePreDefi ne.h#i ncludeLexical.h#i nclude#i nclude#i nclude#i nclude#i nclude#i nclude#i nclude#i ncludeusing namespace Complier ;Mai n
41、F orm:MainFo rm (void )mainUI = new Ui: Complier mainUI -setupUi (this );lex = new Complier : Lexical ();conn ect(ma in Ul- action_New , SIGNAL triggered (),this, SLOT newFile ();conn ect(ma in Ul- action_Ope n, SIGNAtriggered(),this,SLOT(Ope nFile();conn ect(ma in Ul- action_Save, SIGNAL triggered(
42、),this,SLOT(SaveFile();connect(mainUI- action_Save_As,SIGNALtriggered(),this,SLOT( SaveAs();conn ect(ma in Ul- action_Save_All , SIGNAL triggered(),this , SLOT(SaveAll ();conn ect(mai nUI-action_ Un do, SIGNAL triggered(),this,SLOT(U ndo();conn ect(ma in Ul- action_Redo, SIGNAL triggered(),this,SLOT
43、(Redo();connect(mainUI- action_Lexical,SIGNALtriggered(),this,SLOT( Lexical ();connect(mainUI- action_Grammar,SIGNALtriggered(),this,SLOT( Syntax ();conn ect (ma in Ul- action_Sema ntic , SIGNAL triggered (), this , SLOT(Sema ntic (); connect (mainUI- action_About , SIGNAL(triggered (), this , SLOT(
44、 About (); setTablePrekeyword ();setTablePreoperator (); setTablePrePrecomiled();setTablePreDelimiters();Mai nF orm: MainF orm(void )delete mainUI ;bool Mai nF orm: Ope nFile ()sourcePath = QFileDialog : getOpenFileName (this , tr (Open File ), . , tr (All Files(*.*);if (sourcePath . length () = 0)return false ;elseQFile * file = new QFile (sourcePath );if ( file -ope
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- mtbe考试试卷及答案
- 电网业务知识培训课件
- 电缆厂知识培训课件
- 电磁兼容原理课件接地
- 高端酒销售基础知识培训课件
- 新解读《GB-T 32151.11-2018温室气体排放核算与报告要求 第11部分- 煤炭生产企业》
- Ochracenomicin-B-生命科学试剂-MCE
- Acetohexamide-d11-生命科学试剂-MCE
- MLN-591-MLN2704-antibody-生命科学试剂-MCE
- 保育大赛考试试题题库及答案
- 2025年本科院校基建处招聘考试备考指南与模拟题
- 2025至2030中国电动车仪表行业发展分析及前景趋势与投资报告
- 小学教师个人专业发展三年规划
- 2023年度保安员考试检测卷【研优卷】附答案详解
- 2025云南昆明巫家坝建设发展有限责任公司及下属公司第三季度招聘23人笔试备考试题及答案解析
- 热电厂巡检工培训课件
- 2025年探伤工(二级)重点难点考试试卷
- (正式版)DB15∕T 385-2020 《行业用水定额》
- 村级财务业务知识培训课件
- 2025年农业面源污染治理农业面源污染治理技术手册报告
- 中国黄金知识培训课件
评论
0/150
提交评论