编译原理课程设计.docx

上传人:b****5 文档编号:6822133 上传时间:2023-01-10 格式:DOCX 页数:16 大小:87.20KB
下载 相关 举报
编译原理课程设计.docx_第1页
第1页 / 共16页
编译原理课程设计.docx_第2页
第2页 / 共16页
编译原理课程设计.docx_第3页
第3页 / 共16页
编译原理课程设计.docx_第4页
第4页 / 共16页
编译原理课程设计.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

编译原理课程设计.docx

《编译原理课程设计.docx》由会员分享,可在线阅读,更多相关《编译原理课程设计.docx(16页珍藏版)》请在冰豆网上搜索。

编译原理课程设计.docx

编译原理课程设计

合肥工业大学

计算机与信息学院

课程设计

编译原理课程设计

专业班级:

学号:

姓名:

 

引言

 

本题的题目要求为:

对给定的正规式r1、r2,已知它们的NFA分别为M1、M2(其状态转换矩阵及初态、终态信息分别保存在指定文件中)。

构造一程序,由此程序构造正规式r1r2(或运算)的NFA(将其状态转换矩阵及初态、终态信息保存在指定文件中)。

程序实现前,需要两个文件存放正规式r1和r2,连接运算成功后,需要将新的正规式存放在新的文件中.故本实验共有三个文件:

nfa1.txt、nfa2.txt、nfa.txt,其中nfa1.txt、nfa2.txt为实验运行前建立的,nfa.txt是运行程序后程序建立的,存放的是连接后的正规式。

本课程设计用C++编写,用到了文件的输入输出流,连接运算并不复杂,故程序并不复杂。

 

一.概述

1.1设计内容

构造正规式r1r2(或运算)的NFA的程序实现。

1.2设计要求

对给定的正规式r1、r2,已知它们的NFA分别为M1、M2(其状态转换矩阵及初态、终态信息分别保存在指定文件中)。

构造一程序,由此程序构造正规式r1r2(或运算)的NFA(将其状态转换矩阵及初态、终态信息保存在指定文件中)。

二.设计的基本原理

2.1正规式与有限自动机的等价性

(1)对任何FAM,都存在一个正规式r,使得L(r)=L(M).

(2)对任何正规式r,都存在一个FAM,使得L(M)=L(r).

2.2状态转换图的合并

三.程序设计

3.1总体方案设计

由于此题目较为简单,所以总体上有三个函数,分别是OnFile1(),OnFile2()和OnRun(),OnRun()函数完成r1、r2的或运算,而OnFile1()和OnFile2()函数完成对OnRun(()函数的调用。

3.2各模块设计

本程序仅有一个函数,就是OnRun()。

其中有几个功能模块:

1)文件的读与写,即将nfa1、nfa2写入输入文件流,将nfa与输出文件流关联;

2)正规式的或运算与输出(含有对原正规式的输出);

3)关闭文件输入输出流.

 

四.程序测试

1.r1的NFA为M1,其状态转换矩阵及初态、终态信息为(&表示空串):

5-9999(初态)

6-9999(终态)

5a5#

5b5#

5&1#

1a3#

1b4#

3a2#

4b2#

2&6#

6a6#

6b6@

状态转换图:

2.r2的NFA为M2,其状态转换矩阵及初态、终态信息为:

7-9999(初态)

10-9999(终态)

7c8#

7d9#

8c10#

8d9#

9c8#

9d10#

10c10#

10d10@

状态转换图:

3.程序执行后,得正规式r1r2的NFA,其状态转换矩阵及初态、终态信息为:

x-9999

z-9999

x&5#

5a5#

5b5#

5&1#

1a3#

1b4#

3a2#

4b2#

2&6#

6a6#

6b6#

6&y#

y&7#

7c8#

7d9#

8c10#

8d9#

9c8#

9d10#

10c10#

10d10#

10&z@

实验截图如下:

 

根据以上信息,可知OnRun()实现的功能与正规式的或运算相同.

五.总结

通过状态转换图的或(合并、闭包)运算,可实现正规式与有限自动机的转换,以上程序可以实现或运算,同理,可以实现闭包和连接运算,所以正规式与有限自动机的转换这一过程可以用程序来实现。

通过本程序的编写,使我更深入了解了正规式与有限自动机的等价性,以及相互转换的过程,极大地锻炼了我的动手能力。

 

附录:

程序实现代码:

//编译原理Dlg.cpp:

implementationfile

//

#include"stdafx.h"

#include"编译原理.h"

#include"编译原理Dlg.h"

#ifdef_DEBUG

#definenewDEBUG_NEW

#undefTHIS_FILE

staticcharTHIS_FILE[]=__FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

//CAboutDlgdialogusedforAppAbout

classCAboutDlg:

publicCDialog

{

public:

CAboutDlg();

//DialogData

//{{AFX_DATA(CAboutDlg)

enum{IDD=IDD_ABOUTBOX};

//}}AFX_DATA

//ClassWizardgeneratedvirtualfunctionoverrides

//{{AFX_VIRTUAL(CAboutDlg)

protected:

virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport

//}}AFX_VIRTUAL

//Implementation

protected:

//{{AFX_MSG(CAboutDlg)

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

CAboutDlg:

:

CAboutDlg():

CDialog(CAboutDlg:

:

IDD)

{

//{{AFX_DATA_INIT(CAboutDlg)

//}}AFX_DATA_INIT

}

voidCAboutDlg:

:

DoDataExchange(CDataExchange*pDX)

{

CDialog:

:

DoDataExchange(pDX);

//{{AFX_DATA_MAP(CAboutDlg)

//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)

//{{AFX_MSG_MAP(CAboutDlg)

//Nomessagehandlers

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

//CMyDlgdialog

CMyDlg:

:

CMyDlg(CWnd*pParent/*=NULL*/)

:

CDialog(CMyDlg:

:

IDD,pParent)

{

//{{AFX_DATA_INIT(CMyDlg)

//NOTE:

theClassWizardwilladdmemberinitializationhere

//}}AFX_DATA_INIT

//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32

m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

voidCMyDlg:

:

DoDataExchange(CDataExchange*pDX)

{

CDialog:

:

DoDataExchange(pDX);

//{{AFX_DATA_MAP(CMyDlg)

DDX_Control(pDX,IDC_SHOWFILE2,m_showfile2);

DDX_Control(pDX,IDC_SHOWFILE1,m_showfile1);

DDX_Control(pDX,IDC_RUN,m_run);

DDX_Control(pDX,IDC_FILE2,m_file2);

DDX_Control(pDX,IDC_FILE,m_file);

DDX_Control(pDX,IDC_FILE1,m_file1);

//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CMyDlg,CDialog)

//{{AFX_MSG_MAP(CMyDlg)

ON_WM_SYSCOMMAND()

ON_WM_PAINT()

ON_WM_QUERYDRAGICON()

ON_BN_CLICKED(IDC_RUN,OnRun)

ON_BN_CLICKED(IDC_FILE1,OnFile1)

ON_BN_CLICKED(IDC_FILE2,OnFile2)

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

//CMyDlgmessagehandlers

BOOLCMyDlg:

:

OnInitDialog()

{

CDialog:

:

OnInitDialog();

//Add"About..."menuitemtosystemmenu.

//IDM_ABOUTBOXmustbeinthesystemcommandrange.

ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);

ASSERT(IDM_ABOUTBOX<0xF000);

CMenu*pSysMenu=GetSystemMenu(FALSE);

if(pSysMenu!

=NULL)

{

CStringstrAboutMenu;

strAboutMenu.LoadString(IDS_ABOUTBOX);

if(!

strAboutMenu.IsEmpty())

{

pSysMenu->AppendMenu(MF_SEPARATOR);

pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);

}

}

//Settheiconforthisdialog.Theframeworkdoesthisautomatically

//whentheapplication'smainwindowisnotadialog

SetIcon(m_hIcon,TRUE);//Setbigicon

SetIcon(m_hIcon,FALSE);//Setsmallicon

//TODO:

Addextrainitializationhere

m_file1.EnableWindow(true);

m_file2.EnableWindow(false);

m_run.EnableWindow(false);

number=0;

returnTRUE;//returnTRUEunlessyousetthefocustoacontrol

}

voidCMyDlg:

:

OnSysCommand(UINTnID,LPARAMlParam)

{

if((nID&0xFFF0)==IDM_ABOUTBOX)

{

CAboutDlgdlgAbout;

dlgAbout.DoModal();

}

else

{

CDialog:

:

OnSysCommand(nID,lParam);

}

}

//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow

//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,

//thisisautomaticallydoneforyoubytheframework.

voidCMyDlg:

:

OnPaint()

{

if(IsIconic())

{

CPaintDCdc(this);//devicecontextforpainting

SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);

//Centericoninclientrectangle

intcxIcon=GetSystemMetrics(SM_CXICON);

intcyIcon=GetSystemMetrics(SM_CYICON);

CRectrect;

GetClientRect(&rect);

intx=(rect.Width()-cxIcon+1)/2;

inty=(rect.Height()-cyIcon+1)/2;

//Drawtheicon

dc.DrawIcon(x,y,m_hIcon);

}

else

{

CDialog:

:

OnPaint();

}

}

//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags

//theminimizedwindow.

HCURSORCMyDlg:

:

OnQueryDragIcon()

{

return(HCURSOR)m_hIcon;

}

voidCMyDlg:

:

OnRun()

{

//TODO:

Addyourcontrolnotificationhandlercodehere

inthead,rear,i=0,a,b;

charmiddle,tail;

ifstreaminfile1("nfa1.txt",ios:

:

in);

if(!

infile1)

{

cerr<<"openerrornfa1!

"<

exit

(1);

}

ifstreaminfile2("nfa2.txt",ios:

:

in);

if(!

infile1)

{

cerr<<"openerrornfa2!

"<

exit

(1);

}

ofstreamoutfile("nfa.txt",ios:

:

out);

if(!

outfile)

{

cerr<<"openerrornfa!

"<

exit

(1);

}

outfile<<'x'<<'\t'<<"-9999"<

outfile<<'z'<<'\t'<<"-9999"<

infile1>>head;

a=number;

outfile<<'x'<<'\t'<<'&'<<'\t'<

infile1>>head;

infile1>>head;

infile1>>head;

do{

inttemp=number;

infile1>>head>>middle>>rear>>tail;

if(head==rear)

{

outfile<

}

else

{

number++;

outfile<

}

}while(tail!

='@');

outfile<

infile2>>head;

a=number;

infile2>>head;

infile2>>head;

infile2>>head;

outfile<<'x'<<'\t'<<'&'<<'\t'<

inttemp;

do{

temp=number;

infile2>>head>>middle>>rear>>tail;

if(tail=='@')

{

if(head==rear)

{

outfile<

outfile<

}

else

{

number++;

outfile<

outfile<

}

break;

}

else

{

if(head==rear)

{

outfile<

}

else

{

number++;

outfile<

}

}

}while

(1);

outfile.close();

infile2.close();

infile1.close();

CFilefile;

file.Open("nfa.txt",CFile:

:

modeReadWrite);

file.Read(show.GetBuffer(file.GetLength()),file.GetLength());

file.Close();

GetDlgItem(IDC_FILE)->SetWindowText(show);

m_file1.EnableWindow(true);

m_file2.EnableWindow(false);

m_run.EnableWindow(false);

}

voidCMyDlg:

:

OnFile1()

{

//TODO:

Addyourcontrolnotificationhandlercodehere

CStringstr;

CFilef;

f.Open("nfa1.txt",CFile:

:

modeReadWrite);

f.Read(str.GetBuffer(f.GetLength()),f.GetLength());

f.Close();

GetDlgItem(IDC_SHOWFILE1)->SetWindowText(str);

m_file1.EnableWindow(false);

m_file2.EnableWindow(true);

m_run.EnableWindow(false);

}

voidCMyDlg:

:

OnFile2()

{

//TODO:

Addyourcontrolnotificationhandlercodehere

CStringstr;

CFilef;

f.Open("nfa2.txt",CFile:

:

modeReadWrite);

f.Read(str.GetBuffer(f.GetLength()),f.GetLength());

f.Close();

GetDlgItem(IDC_SHOWFILE2)->SetWindowText(str);

m_file1.EnableWindow(false);

m_file2.EnableWindow(false);

m_run.EnableWindow(true);

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 法律文书 > 调解书

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

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