实验一 C++源代码单词扫描程序(词法分析)软件文档

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

C++源代码单词扫描程序(词法分析)软件文档

一、需求分析

1.程序功能:

(1)C++源代码扫描程序识别C++记号。C++语言包含了几种类型的记号:标识符,关键字,数(包括整数、浮点数),字符串、注释、特殊符号(分界符)和运算符号等。

(2)打开一个C++源文件,打印出所有以上的记号。

2.界面要求:Windows界面。

二、概要设计

1.关键词(60个):

"include","iostream","iostream.h","cstring","stdio","stdio.h","ma th.h","int","double","float","char","long","short","enum","struct ","typedef","string","class","true","false","if","else","switch", "case","default","break","continue","return","break","continue","

protected","do","while","for","goto","Loop","cin","cout","printf"

,"sizeof","try","catch","throw","typedef","virtual","public","pri vate","const","static","signed","unsigned","union","volatile","vo id","bool","namespace","define","this","main","template"

2.运算符(28个):

"+","","*","/","%","<",">","==",">=","<=","!=","!","&&","||","~", "|","^","&","=","?:","++","","--",".","+=","-=","*=","/="

3.特殊符号(13个):

"->","(",")","[","]","{","}",",",";","'" , ,"<<",">>"

4.注释符号(3个):"//","/*","*/"

5.程序流程图:

1)主流程图:

2)词法分析模块流程图:

三、详细设计

void CCScanerDlg::csearch(char *word) {

CString St;

int m, line = 0, csi = 0;

int value = 0, value2 = 0;

for (m = 1; m <= 60; m++)

if (strcmp(word, keyword[m]) == 0) {

value = 1;

break;

}

if(value==1) display(word,_T("关键字"));

else display(word,_T("标识符"));

}

void CCScanerDlg::OnEnChangeMfceditbrowse1()

{

UpdateData(true);

int count;

char ch;

FILE *fin;

char temp[100];

char target[3] = "'";//target[0]="'"

CString St;

char *filename=(LPSTR)(LPCTSTR)m_Filename;//获取文件路径if ((fin = fopen(filename, "r")) == NULL)//文件为空return;

ch = fgetc(fin);

while (ch != EOF)//未到文件尾

{

/*对文件包含、宏定义进行处理*/

if (ch == '#')

{

int i=0;

Count++;

St.Format("%c",ch);

m_list.InsertItem(Count,St); St.Format("%s",_T("特殊符号")); m_list.SetItemText(Count,1,St); ch = fgetc(fin);

while (ch != '<')//库文件名

{

temp[i++]=ch;

ch = fgetc(fin);

}

temp[i]='\0';

display(temp,_T("关键字")); Count++;

St.Format("%c",ch);

m_list.InsertItem(Count,St); St.Format("%s",_T("特殊符号")); m_list.SetItemText(Count,1,St); i=0;

ch = fgetc(fin);

while (ch != '>')

{

temp[i++]=ch;

ch = fgetc(fin);

}

temp[i]='\0';//字符串结束标志display(temp,_T("关键字")); Count++;

St.Format("%c",ch);

m_list.InsertItem(Count,St); St.Format("%s","特殊符号");

m_list.SetItemText(Count,1,St);

}

/*对空格符、水平制表符、回车进行处理*/

else if ((ch == ' ') || (ch == '\t')||ch == '\n') {

;

}

/*对单引号内的字符常量进行处理*/

else if (ch == target[0])

{

temp[0] = ch;

ch = fgetc(fin);

temp[1] = ch;

ch = fgetc(fin);

if (ch != target[0])

{

temp[2] = ch;

ch = fgetc(fin);

temp[3] = ch;

temp[4] = '\0';

}

else

{

temp[2] = ch;

temp[3] = '\0';

}

display(temp,_T("字符"));

}

相关文档
最新文档