1、 了解高级语言单词的分类,了解状态图以及如何表示并识别单词规则,掌握状态图到识别程序的编程。四、设计环境该课程设计包括的硬件和软件条件如下:4.1.硬件(1)Intel Core Duo CPU P8700(2)内存4G4.2.软件(1)Window 7 32位操作系统(2)Microsoft Visual Studio c#开发平台4.3.编程语言 C#语言五、需求分析5.1.源程序的预处理:源程序中,存在许多编辑用的符号,他们对程序逻辑功能无任何影响。例如:回车,换行,多余空白符,注释行等。在词法分析之前,首先要先剔除掉这些符号,使得词法分析更为简单。5.2.单词符号的识别并判断单词的合法
2、性:将每个单词符号进行不同类别的划分。单词符号可以划分成5中。 (1)标识符:用户自己定义的名字,常量名,变量名和过程名。(2)常数:各种类型的常数。(3) 保留字(关键字):如if、else、while、int、float等。(4) 运算符:如+、-、*、=等。(5)界符:如逗号、分号、括号等。5.3.将所有合法的单词符号转化为便于计算机处理的二元组形式:(单词分类号,单词自身值);以图形化界面显示出来。5.4.可选择性地将结果保存到文件中。六、概要设计6.1数据类型6.1.1.单词的分类:本词法分析器演示的是C语言的一个子集,故字符集如下:(1)标识符:以字母开头的字母数字串。(2)整数或
3、浮点型(3)保留字:auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for, goto, if, int, long, register, return, short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile, while(4)运算符:+、-、*、/、%、=、!=、=、=、+、-、!、&、|; ( ) : ; “ # , 6.1.2.单词二元组
4、:(单词分类号,单词自身值) 表6.1单词分类表单词分类号标识符1常数2保留字3界符4运算符5 6.2词法分析器的结构图6.1 词法分析器的结构 6.3部分单词的正规式与状态转换图图6.2部分单词的状态转换图 6.4功能模块表6.2 词法分析器的C#程序过程或函数功能表过程或函数名简要功能说明GetTokens()分析源程序得到单个单词并大致区分其类型,并生成二元组GetInput( string get )输入源程序NoWs( string aCopy, int cp )去除源程序中的空格符GetTokenType最终确定标识符和保留字OutPut输出二元组到指定的.txt文件中GetRes
5、ult输出二元组七、详细设计7.1总体流程图 图7.1程序总体流程图7.2主要分支选择算法介绍 7.2.1case0情况算法 图7.2 部分分支流程图八、编程调试8.1主要源代码/逻辑操作类与函数using System;using System.Collections.Generic;using System.Text;using System.IO;namespace TokenAnalysis struct TokenNode public string type; public string token; class TokenAnalysis int cPosition = 0; i
6、nt lPosition = 0; int ignorWs = 0; int recordWs = 1; FileInfo output; string source; string kwList = auto, breakcasecharconstcontinuedefaultdodoubleelse,enumexternfloatforgotoifintlongregisterreturnshortsignedsizeofstaticstructswitchtypedefunionunsignedvoidvolatilewhileprintfscanfendmain; int Error
7、= -2; int Normal = -1; int statue = -1; bool isComment = false; List resultTokenNodes = new List(); public List GetTokens() TokenNode node2Add = new TokenNode(); int cp = 0;/current position int lp = 0;/last position string aCopy = source; int length = aCopy.Length; aCopy = aCopy.Trim(); while( cp a
8、Copy.Length ) /ID or KeyWords node2Add = this.GetToken( ignorWs, aCopy, ref cp, ref lp ); if( node2Add.type = cmtLeft ) isComment = true;cmtRight isComment = false; if( !isComment & node2Add.type != resultTokenNodes.Add( node2Add ); return resultTokenNodes; public TokenNode GetToken() if( cPosition
9、source.Length ) TokenNode token = this.GetToken( ignorWs, source, ref cPosition, ref lPosition ); return token; else TokenNode token = new TokenNode(); token.token = Error; private TokenNode GetToken( int type, string aCopy, ref int cp, ref int lp ) string tempToken; string tempType; if( type = igno
10、rWs ) if( Char.IsLetter( aCopy cp ) ) cp+; aCopy.Length & ( Char.IsLetterOrDigit( aCopy cp ) | aCopy cp = _ ) ) tempToken = aCopy.Substring( lp, cp - lp ); tempType = 1 or 3 tempType = GetTokenType( tempToken, tempType ); token.token = tempToken; token.type = tempType; lp = cp = NoWs( aCopy, cp ); /
11、Num else if( Char.IsDigit( aCopy cp ) ) ( ( Char.IsDigit( aCopy cp ) ) | ( aCopy cp = . & Char.IsDigit( aCopy cp + 1 ) ) ) ) 2 /Border else if( aCopy cp = ( | aCopy cp = ) |,4 /Operation and other if( cp */! statue = Error;| default: tempToken = tempToken.Replace( );5 public void GetInput( string ge
12、t ) /source = for( int i = 0; i length; i+ ) /* cmt */ source = get; private int NoWs( string aCopy, int cp ) while( Char.IsWhiteSpace( aCopy cp ) ) return cp; private string GetTokenType( string tk2anls, string iniType ) if( iniType = foreach( string kw in kwList ) if( tk2anls = kw ) return 31 if(
13、tk2anls = /*/ return iniType;UnknownTpye public void OutPut( string fileName ) FileInfo outFile = new FileInfo( fileName ); StreamWriter fw = outFile.AppendText(); foreach( TokenNode tkNode in resultTokenNodes ) string show = string.Format( 0,-251,-40, tkNode.token, tkNode.type ); System.Console.Wri
14、teLine( show ); fw.WriteLine( show ); fw.Close(); public void OutPut() FileInfo outFile = new FileInfo( result.txtstring GetResult() res = new List res = ta.GetResult(); lstResult.Items.Clear(); lstResult.Items.AddRange(res.ToArray(); private void btnExit_Click(object sender, EventArgs e) Applicatio
15、n.Exit(); private void 保存SToolStripButton_Click(object sender, EventArgs e) saveFileDialog1.Title = 保存分析结果 saveFileDialog1.Filter = 文本文件|*.txt if( ( saveFileDialog1.ShowDialog() ) = DialogResult.OK ) if( saveFileDialog1.FileName != null ) FileInfo outFile = new FileInfo( saveFileDialog1.FileName );
16、foreach( string tkString in lstResult.Items ) fw.WriteLine( tkString ); private void btnsave_Click(object sender, EventArgs e) if (saveFileDialog1.ShowDialog() = DialogResult.OK) if (saveFileDialog1.FileName != null) FileInfo outFile = new FileInfo(saveFileDialog1.FileName); foreach (string tkString
17、 in lstResult.Items) fw.WriteLine(tkString);8.2调试 调试过程中存在保存不了输出结果的问题。最后调试发现是由于绝对路径与相对路径的区分。最后上网搜索解决方法,通过调用C#中提供的文件保存接口来解决保存的问题九、测试1.运行的主界面图9.1 运行的主界面2.输入想要分析的c语言源程序图9.2 输入源程序3.点击分析的按钮,显示分析后的结果,以二元组形式展示。图9.3 分析结果4.点击保存按钮,弹出选择保存文件的路径和名称图9.4 保存过程5.保存后的.txt文档图9.5 保存后的文档十、结束语通过这次的课程设计,我学会了很多。让我学会了如何设计、编制并调试词法分析程序,加深对词法分析原理的理解;熟悉了构造词法分析器的手工方式的相关原理,也对状态图以及如何表示并识别单词规则有了更进一步的了解。并且在掌握状态图对识别程序的编程上有了更深层次的进步。除