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

📄 ajaxparserdlg.cpp

📁 词法分析的程序。核心函数是一个状态切换的函数 CAjaxParserDlg::Route。状态切换函数解根据一个 DFA 来对输入的文本进行分析。也就是说
💻 CPP
字号:
// AjaxParserDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AjaxParser.h"
#include "AjaxParserDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

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

// this is comment
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()

/////////////////////////////////////////////////////////////////////////////
// CAjaxParserDlg dialog

CAjaxParserDlg::CAjaxParserDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAjaxParserDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAjaxParserDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	double a = 1.21e1;
}

void CAjaxParserDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAjaxParserDlg)
	DDX_Control(pDX, IDC_EDIT1, m_edit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAjaxParserDlg, CDialog)
	//{{AFX_MSG_MAP(CAjaxParserDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_PARSE, OnParse)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAjaxParserDlg message handlers

BOOL CAjaxParserDlg::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
	CStateChangeRule rule;
	rule.nCurState = 0;
	rule.nNextState = 1;
	rule.route[0].byStart = '_';
	rule.route[0].byEnd = '_';
	rule.route[1].byStart = 'A';
	rule.route[1].byEnd = 'Z';
	rule.route[2].byStart = 'a';
	rule.route[2].byEnd = 'z';
	m_ruleArr.Add(rule);
	rule.Clear();

	rule.nCurState = 0;
	rule.nNextState = 0;
	rule.route[0].byStart = '\r';
	rule.route[0].byEnd = '\r';
	rule.route[1].byStart = '\n';
	rule.route[1].byEnd = '\n';
	rule.route[2].byStart = ' ';
	rule.route[2].byEnd = ' ';
	rule.route[3].byStart = '\t';
	rule.route[3].byEnd = '\t';
	m_ruleArr.Add(rule);
	rule.Clear();

	rule.nCurState = 1;
	rule.nNextState = 1;
	rule.route[0].byStart = '_';
	rule.route[0].byEnd = '_';
	rule.route[1].byStart = 'A';
	rule.route[1].byEnd = 'Z';
	rule.route[2].byStart = 'a';
	rule.route[2].byEnd = 'z';
	rule.route[3].byStart = '0';
	rule.route[3].byEnd = '9';
	m_ruleArr.Add(rule);
	rule.Clear();

	rule.nCurState = 0;
	rule.nNextState = 2;
	rule.route[0].byStart = '+';
	rule.route[0].byEnd = '+';
	rule.route[1].byStart = '-';
	rule.route[1].byEnd = '-';
	m_ruleArr.Add(rule);
	rule.Clear();

	rule.nCurState = 0;
	rule.nNextState = 3;
	rule.route[0].byStart = '0';
	rule.route[0].byEnd = '9';
	m_ruleArr.Add(rule);
	rule.Clear();

	rule.nCurState = 2;
	rule.nNextState = 3;
	rule.route[0].byStart = '0';
	rule.route[0].byEnd = '9';
	m_ruleArr.Add(rule);
	rule.Clear();

	rule.nCurState = 2;
	rule.nNextState = 6;
	rule.route[0].byStart = '.';
	rule.route[0].byEnd = '.';
	m_ruleArr.Add(rule);
	rule.Clear();

	rule.nCurState = 0;
	rule.nNextState = 6;
	rule.route[0].byStart = '.';
	rule.route[0].byEnd = '.';
	m_ruleArr.Add(rule);
	rule.Clear();

	rule.nCurState = 3;
	rule.nNextState = 3;
	rule.route[0].byStart = '0';
	rule.route[0].byEnd = '9';
	m_ruleArr.Add(rule);
	rule.Clear();

	rule.nCurState = 3;
	rule.nNextState = 4;
	rule.route[0].byStart = '.';
	rule.route[0].byEnd = '.';
	m_ruleArr.Add(rule);
	rule.Clear();

	rule.nCurState = 4;
	rule.nNextState = 5;
	rule.route[0].byStart = '0';
	rule.route[0].byEnd = '9';
	m_ruleArr.Add(rule);
	rule.Clear();

	rule.nCurState = 6;
	rule.nNextState = 7;
	rule.route[0].byStart = '0';
	rule.route[0].byEnd = '9';
	m_ruleArr.Add(rule);
	rule.Clear();

	rule.nCurState = 7;
	rule.nNextState = 7;
	rule.route[0].byStart = '0';
	rule.route[0].byEnd = '9';
	m_ruleArr.Add(rule);
	rule.Clear();

	rule.nCurState = 5;
	rule.nNextState = 5;
	rule.route[0].byStart = '0';
	rule.route[0].byEnd = '9';
	m_ruleArr.Add(rule);
	rule.Clear();

	DFA_STATE state;

	// 0
	state.nFlag = STATE_START;
	m_stateArr.Add(state);

	// 1
	state.nFlag = STATE_END;
	m_stateArr.Add(state);

	// 2
	state.nFlag = 0;
	m_stateArr.Add(state);

	// 3
	state.nFlag = STATE_END;
	m_stateArr.Add(state);

	// 4
	state.nFlag = STATE_END;
	m_stateArr.Add(state);

	// 5
	state.nFlag = STATE_END;
	m_stateArr.Add(state);

	// 6
	state.nFlag = 0;
	m_stateArr.Add(state);

	// 7
	state.nFlag = STATE_END;
	m_stateArr.Add(state);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CAjaxParserDlg::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 CAjaxParserDlg::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 CAjaxParserDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

int InRoute(BYTE c, CHAR_RANGE *pRange)
{
	int i=0;
	while(i<ROUTE_BUF_LEN && pRange->byEnd!=0xff)
	{
		if(pRange[i].byEnd>=c && pRange[i].byStart<=c)
		{
			return 1;
		}

		i++;
	}

	return 0;
}

int CAjaxParserDlg::Route(int nCurState, BYTE c)
{
	int i, nSize;
	nSize = m_ruleArr.GetSize();

	CStateChangeRule rule;
	for(i=0; i<nSize; i++)
	{
		rule = m_ruleArr[i];
		if(rule.nCurState==nCurState
			&& InRoute(c, rule.route))
		{
			return rule.nNextState;
		}
	}

	if(nCurState==0)
	{
		Logln("start state, cannot recognize char");
	}
	// return to state 0 the start state
	return 0;
}

void CAjaxParserDlg::Accept(int nCurrent, int nNext, LPCTSTR pszLine, int nLen)
{
	char psz[256];
	if(nNext==0 && nCurrent!=0)
	{
		sprintf(psz, "Accept word(s:%d)[", nCurrent);
		Log(psz);
		strncpy(psz, pszLine, nLen);
		psz[nLen] = 0;
		Log(psz);

		if(!(m_stateArr[nCurrent].nFlag&STATE_END))
		{
			Logln("]unrecogized char, can't accept");
		}
		else
		{
			Logln("]");
		}
	}
}

void CAjaxParserDlg::OnParse() 
{
	char *pszLine = " void CAjaxParserDlg::OnParse() +12.3 12.3 -12.3 -.12 +.12 .12 +12. -12. 12. .023 .  +. -. + -";

	int i=0, nLen, nState=0, nNext=-1, nStart;
	nLen = strlen(pszLine);
	while(i<nLen)
	{
		if(nState==0)
		{
			nStart = i;
		}

		nNext = Route(nState, *(pszLine+i));
		Accept(nState, nNext, pszLine+nStart, i-nStart);

		if(!(nState!=0 && nNext==0))
			i++;
		nState = nNext;
	}
	Accept(nState, 0, pszLine+nStart, i-nStart);
}

void CAjaxParserDlg::Logln(LPCTSTR psz)
{
	m_edit.SetSel(-1,-1);
	m_edit.ReplaceSel(psz);
	m_edit.ReplaceSel("\r\n");
}

void CAjaxParserDlg::Log(LPCTSTR psz)
{
	m_edit.SetSel(-1,-1);
	m_edit.ReplaceSel(psz);
}

⌨️ 快捷键说明

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