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

📄 wordanalysisdlg.cpp

📁 用c++语言编写的词法分析器源代码
💻 CPP
字号:
// WordAnalysisDlg.cpp : implementation file
//

#include "stdafx.h"
#include "WordAnalysis.h"
#include "WordAnalysisDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CWordAnalysisDlg dialog
typedef struct tagRESERVE
{
	CString strReverse;
	int code;
}Reserve;

Reserve res[65];

CString keyWords[]={"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","main"};

CString symbols[]={"+","+=","-","-=","*","*=","/","/=","{","}","(",")","&","&&",
		"|","||","%","#","!","!=","/*","*/",";","<","<=",">",">=","=","==",",","_","'"};

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

void CWordAnalysisDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWordAnalysisDlg)
	DDX_Control(pDX, IDC_EDIT1, m_strFile);
	DDX_Text(pDX, IDC_EDIT1, m_strText);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWordAnalysisDlg, CDialog)
	//{{AFX_MSG_MAP(CWordAnalysisDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_KEYDOWN()
	ON_BN_CLICKED(IDC_OPEN, OnOpen)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWordAnalysisDlg message handlers

BOOL CWordAnalysisDlg::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
	Init();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CWordAnalysisDlg::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData();
	this->Analysis(this->m_strText);
	char* lpResult=new char[sizeof(this->m_strResult)];
	lpResult=this->m_strResult.GetBuffer(sizeof(this->m_strResult));
	this->m_strResult.ReleaseBuffer();
	CFileDialog fg(FALSE,NULL,"Result.txt",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,".txt",NULL);
	if(fg.DoModal()==IDOK)
	{
		this->m_strFileName=fg.GetPathName();
	}
	else
	{
		return;
	}
	CStdioFile file;
	CFileException e;
	try
	{
		if(file.Open(this->m_strFileName.GetBuffer(0),CFile::modeCreate | CFile::modeWrite,&e))
		{
			file.WriteString(lpResult);
			file.Close();
			CString str;
			str.Format("分析结果已成功保存到  %s  中",m_strFileName);
			AfxMessageBox(str);
			::ShellExecute(NULL,"open",this->m_strFileName,NULL,NULL,SW_SHOWNORMAL);
		}
		else
		{
			e.ReportError();
		}
	}
	catch(CFileException* fe)
	{
		fe->ReportError();
		fe->Delete();
	}

	
	//	CDialog::OnOK();
}

void CWordAnalysisDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}

BOOL CWordAnalysisDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class

	return CDialog::PreTranslateMessage(pMsg);
}

void CWordAnalysisDlg::OnOpen() 
{
	// TODO: Add your control notification handler code here
	this->m_strText="";
	UpdateData(TRUE);
	static char BASED_CODE szFilter[] = "C Header Files (*.h)|*.h|C Files (*.c)|*.c|Data Files (*.txt)|*.txt||";
	CFileDialog fg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter,NULL);
	fg.m_ofn.lpstrTitle="请选择你要打开的文件";
	if(fg.DoModal()==IDOK)
	{
		CStdioFile cf;
		CFileException e;
		try
		{
			if(cf.Open(fg.GetPathName().GetBuffer(0),CFile::modeRead,&e))
			{
				CString  strTemp;
				while(cf.ReadString(strTemp))
				{
					this->m_strText+=strTemp;
					this->m_strText+="\r\n";
				}	
				UpdateData(FALSE);
			
				cf.Close();
			}
			else
			{
				e.ReportError();
			}
		}
		catch(CFileException* ce)
		{
			ce->ReportError();
			ce->Delete();
		}
	}
	else
	{
		return;
	}
}

void CWordAnalysisDlg::Init()
{
	for(int i=0;i<33;i++)
	{
		res[i].strReverse=keyWords[i];
		res[i].code=i+1;
	}
	for(i=0;i<32;i++)
	{
		res[i+33].strReverse=symbols[i];
		res[i+33].code=i+34;
	}
}

void CWordAnalysisDlg::Analysis(CString str)
{
	char arr[255]="";
	float nIndex=0;
	char ch=' ';
	unsigned int i=0;
	while(str.GetLength()>nIndex)
	{
		ch=str.GetAt(nIndex++);
		if(ch==' '||ch=='\t')
		{
			;
		}
		else if(ch=='\n')
		{
			;
		}
		else if(isdigit(ch))
		{
			arr[i++]=ch;
			if(str.GetLength()>nIndex)
			{
				ch=str.GetAt(nIndex++);
				while(isdigit(ch))
				{
					arr[i++]=ch;
					if(str.GetLength()>nIndex)
						ch=str.GetAt(nIndex++);
					else
						ch='\0';
				}
				nIndex--;
			}
			this->m_strResult+=arr;
			if(m_bNote)
				this->m_strResult+="     注释";
			else if(m_bString)
				this->m_strResult+="     引号内容";
			else
				this->m_strResult+="     数字";
			this->m_strResult+="\n";
		}
		else if(isalpha(ch)||ch=='_')
		{
			arr[i++]=ch;
			if(str.GetLength()>nIndex)
			{
				ch=str.GetAt(nIndex++);
				while(isdigit(ch)||isalpha(ch)||ch=='_')
				{
					arr[i++]=ch;
					if(str.GetLength()>nIndex)
					{
						ch=str.GetAt(nIndex++);
					}
					else
						ch='\0';
				}
				nIndex--;
			}
			int code=FindReserve(arr);
			this->m_strResult+=arr;
			if(m_bNote)
				this->m_strResult+="     注释";
			else if(m_bString)
				this->m_strResult+="     引号内容";
			else
			{
				CString strcode;
				if(code!=-1)
					strcode.Format("%5d",code);
				else
					strcode="     用户自定义字符串";
			
				this->m_strResult+=strcode;
			}
			this->m_strResult+="\n";
		}
		else if(ch=='"')
		{
			arr[i++]=ch;
			this->m_strResult+=arr;
			if(m_bNote)
				this->m_strResult+="     注释";
			else
			{
				this->m_strResult+="   66";
				m_bString=!m_bString;
			}
			this->m_strResult+="\n";
		}
		else
		{
			arr[i++]=ch;
			if(str.GetLength()>nIndex)
				ch=str.GetAt(nIndex++);
			else
				ch='\0';
			arr[i++]=ch;
			int code=FindReserve(arr);
			CString strcode;
			if(code!=-1)
			{
				strcode.Format("%5d",code);
				if(strcmp(arr,"/*")==0)
					m_bNote=true;
				if(strcmp(arr,"*/")==0)
					m_bNote=false;
			}
			else
			{
				arr[1]='\0';
				code=FindReserve(arr);
				if(code!=-1)
				{
					strcode.Format("%5d",code);
				}
				nIndex--;
			}
			this->m_strResult+=arr;
			if(m_bNote&&strcmp(arr,"/*")!=0)
				this->m_strResult+="     注释";
			else if(m_bString)
				this->m_strResult+="     引号内容";
			else
				this->m_strResult+=strcode;
			this->m_strResult+="\n";
		}
		i=0;
		for(int j=0;j<255;j++)
		{
			arr[j]='\0';
		}
	}
}

int CWordAnalysisDlg::FindReserve(char* cstr)
{
	CString str;
	str.Format("%s",cstr);
	for(int i=0;i<65;i++)
	{
		if(str==res[i].strReverse)
			return res[i].code;
	}
	return -1;
}

⌨️ 快捷键说明

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