⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scanner1dlg.cpp

📁 自己做的一个简单的C词法分析器
💻 CPP
字号:
// scanner1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "scanner1.h"
#include "scanner1Dlg.h"
#include "scan.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CScanner1Dlg dialog

CScanner1Dlg::CScanner1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CScanner1Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CScanner1Dlg)
	m_input = _T("");
	m_output = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CScanner1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CScanner1Dlg)
	DDX_Text(pDX, IDC_EDIT1, m_input);
	DDX_Text(pDX, IDC_EDIT2, m_output);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CScanner1Dlg, CDialog)
	//{{AFX_MSG_MAP(CScanner1Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnOpen)
	ON_BN_CLICKED(IDC_BUTTON2, OnScan)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CScanner1Dlg message handlers

BOOL CScanner1Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CScanner1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CScanner1Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

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

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CScanner1Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CScanner1Dlg::OnOpen() 
{
	// TODO: Add your control notification handler code here


	static char BASED_CODE szFilter[] = "C_file(*.C)|*.C|CPP_file(*.CPP)|*.CPP|txt_file(*.TXT)|*.TXT|all_file(*.*)|*.*|我的测试文件(*.fch)|*.fch||";
	CFileDialog file(TRUE,NULL,NULL,OFN_HIDEREADONLY ,szFilter);
	if (file.DoModal()==IDOK)
	{
		//CString path=file.GetPathName( );	
			CString Filename;
		Filename=file.GetPathName( );
		CFile myfile(Filename,CFile::modeRead);
		DWORD dwLength = myfile.GetLength();
		char *buff=new char[dwLength+1];
		memset(buff,0,dwLength+1);
		myfile.Read(buff,dwLength);
		myfile.Close();
		m_input.Format("%s",buff);
		UpdateData(FALSE);	
	}
}

void CScanner1Dlg::OnScan() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	//文本框里没有内容
	if (m_input.IsEmpty())
	{
		AfxMessageBox("请输入源代码或载入源码文件!");
		return;
	}
	//获取当前文本框
	CEdit* tempEdit=(CEdit*)GetDlgItem(IDC_EDIT1);
	int nLineCount=tempEdit->GetLineCount();	//得到行数
	char buf[100];
	memset(buf,0,100);
	CString strText=_T("");
	int linelength=0;
	scan ana;		//创建sacn类的对象,执行扫描任务
	m_output=_T("");
	m_output+="                     词法分析结果                       \r\n";
	m_output+="\r\n";
	m_output+="输出格式:二元式(行,列) 单词\r\n";
	m_output+="\r\n";
	CString temp=_T("");
	//扫描每一行进行行处理
	for (int i=0; i < nLineCount; i++)
	{
		//得到一行信息
		tempEdit->GetLine(i,buf,tempEdit->LineLength(tempEdit->LineIndex(i)));
		strText.Format("%s",buf);
		//得到的一行信息传入scan进行错误。
 		temp+=ana.Analyze(strText,i,tempEdit->LineLength(tempEdit->LineIndex(i)));
		strText=_T("");
		memset(buf,0,100);
	}
	m_output+=temp;
	//将Token写入文件
	StoreToken(ana.strtoken);
	//将Error写入文件
	StoreError(temp);
	//将符号表写入文件
	ana.AddWordTable();
	StoreTable(ana.addwordtable);
	//提示错误个数
	m_output+="\r\n共扫描出";
	char buffer[20]="";
	//错误个数的打印
	m_output+=_itoa(ana.errnum, buffer, 10 );
	m_output+="个错误!\r\n";
	m_output+="\r\n";
	UpdateData(FALSE);
}
void CScanner1Dlg::StoreToken(CString s)
{
	char write[1000];
	memset(write,0,1000);
	strcat(write,s);
	CFile file("ftoken.txt",CFile::modeCreate|CFile::modeWrite );
	file.Write(write,1000);
	file.Close();
}

//写错误文件
void CScanner1Dlg::StoreError(CString s)
{
	char write[1000];
	memset(write,0,1000);
	strcat(write,s);
	CFile file("ferror.txt",CFile::modeCreate|CFile::modeWrite );
	file.Write(write,1000);
	file.Close();
}

//写字符表
void CScanner1Dlg::StoreTable(CString s)
{
	char write[1000];
	memset(write,0,1000);
	strcat(write,s);
	CFile file("ftable.txt",CFile::modeCreate|CFile::modeWrite );
	file.Write(write,1000);
	file.Close();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -