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

📄 wordinterpretdlg.cpp

📁 词法分析器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// WordInterpretDlg.cpp : implementation file
//

#include "stdafx.h"
#include "WordInterpret.h"
#include "WordInterpretDlg.h"
#include "ShowDlgT.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()

/////////////////////////////////////////////////////////////////////////////
// CWordInterpretDlg dialog

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

void CWordInterpretDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWordInterpretDlg)
	
	DDX_Control(pDX, IDC_LIST1, m_ResultList);
	DDX_Text(pDX, IDC_EDIT1, m_Edit1);
	DDX_Text(pDX, IDC_EDIT2, m_Edit2);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWordInterpretDlg, CDialog)
	//{{AFX_MSG_MAP(CWordInterpretDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWordInterpretDlg message handlers

BOOL CWordInterpretDlg::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
	//CString strToken;
	//ptr=m_Edit1;
	

	//GetDlgItemText(IDC_EDIT1,ptr); 


	strcpy(ReserveTable[0],"auto");
	strcpy(ReserveTable[1],"continue");
	strcpy(ReserveTable[2],"enum");
	strcpy(ReserveTable[3],"if");
	strcpy(ReserveTable[4],"short");
	strcpy(ReserveTable[5],"switch");
	strcpy(ReserveTable[6],"volatile");
	strcpy(ReserveTable[7],"break");
	strcpy(ReserveTable[8],"default");
	strcpy(ReserveTable[9],"extern");
	strcpy(ReserveTable[10],"int");
	strcpy(ReserveTable[11],"signed");
	strcpy(ReserveTable[12],"typedef");
	strcpy(ReserveTable[13],"while");
	strcpy(ReserveTable[14],"case");
	strcpy(ReserveTable[15],"do");
	strcpy(ReserveTable[16],"long");
	strcpy(ReserveTable[17],"sizeof");
	strcpy(ReserveTable[18],"union");
	strcpy(ReserveTable[19],"char");
	strcpy(ReserveTable[20],"double");
	strcpy(ReserveTable[21],"for");
	strcpy(ReserveTable[22],"register");
	strcpy(ReserveTable[23],"static");
	strcpy(ReserveTable[24],"unsigned");
	strcpy(ReserveTable[25],"const");
	strcpy(ReserveTable[26],"else");
	strcpy(ReserveTable[27],"goto");
	strcpy(ReserveTable[28],"return");
	strcpy(ReserveTable[29],"struct");
	strcpy(ReserveTable[30],"void");
	strcpy(ReserveTable[31],"float");
	
	
	m_ResultList.InsertColumn(0,"单词",LVCFMT_LEFT,65,-1);
	m_ResultList.InsertColumn(1,"种别",LVCFMT_LEFT,65,-1);
	m_ResultList.InsertColumn(2,"属性",LVCFMT_LEFT,65,-1);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CWordInterpretDlg::OnButton3() 
{
	CDialog::OnOK();	
}

BOOL CWordInterpretDlg::IsLetter()
{
	if(ch>='a' && ch<='z' || ch>='A' && ch<='Z')
		return 1;
	else
		return 0;
	
}

BOOL CWordInterpretDlg::IsDigit()
{
	if(ch>='0' && ch<='9')
		return 1;
	else
		return 0;
}

void CWordInterpretDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here

m_ResultList.DeleteAllItems();
j=0;
UpdateData(TRUE);
//m_Edit2=m_Edit1;
//UpdateData(FALSE);
i=0;
//char *ptr;
m_Edit1=m_Edit1+'_';
while(j<=(m_Edit1.GetLength()-1))
{
strToken=_T("");
ch=GetChar();
GetBC();
//m_Edit2=strToken;
//UpdateData(FALSE);
if(IsLetter())
{
	while(IsLetter()||IsDigit())
	{
		strToken=strToken+ch;//将ch连接到strToken之后
		ch=GetChar();
		
     }
	Retract();//回调一个字符位置;
	code=Reserve();//查找保留字表,若是则返回编码,若不是返回0;
	if(code==0)
	{//value=InsertId(strToken);//将strToken的标识符插入符号表,返回符号表指针;
	SHOWTABLECHAR(strToken,i);//列表框显示
	}
	else
	{code=code+1;
	SHOWTABLE(code,i);
	}
}
else if(IsDigit())
{
	while(IsDigit())
	{
	strToken=strToken+ch;
	
	 ch=GetChar();
	}
	Retract();
	//value=InsertConst(strToken);//插入常数表
	SHOWTABLEDIGIT(strToken,i);
}
else if(ch=='=')

{
ch=GetChar();
if(ch=='=')SHOWTABLE(56,i);
else {SHOWTABLE(66,i);	Retract();}
}

else if(ch=='+')
{
ch=GetChar();
if(ch=='+')SHOWTABLE(60,i);
else if(ch=='=')SHOWTABLE(58,i);
else {SHOWTABLE(50,i);	Retract();}
}

else if(ch=='*')
{
ch=GetChar();
if(ch=='*')SHOWTABLE(47,i);
else if(ch=='=')SHOWTABLE(59,i);

else {SHOWTABLE(44,i);	Retract();}
}
else if(ch==';')SHOWTABLE(78,i);
else if(ch=='(')SHOWTABLE(33,i);
else if(ch==')')SHOWTABLE(34,i);
else if(ch=='{')SHOWTABLE(88,i);
else if(ch=='}')SHOWTABLE(89,i);
else if(ch=='[')SHOWTABLE(35,i);
else if(ch==']')SHOWTABLE(36,i);
else if(ch=='.')SHOWTABLE(40,i);
else if(ch=='!')
{
ch=GetChar();
if(ch=='=')SHOWTABLE(94,i);

else {SHOWTABLE(41,i);	Retract();}
}
else if(ch=='~')SHOWTABLE(42,i);
else if(ch=='"')SHOWTABLE(105,i);

else if(ch=='-')
{
ch=GetChar();
if(ch=='-')SHOWTABLE(60,i);
else if(ch=='=')SHOWTABLE(70,i);
else if(ch=='>')SHOWTABLE(98,i);

else {SHOWTABLE(43,i);	Retract();}
}

else if(ch=='&')
{
ch=GetChar();
if(ch=='=')SHOWTABLE(72,i);
else if(ch=='&')SHOWTABLE(93,i);


else {SHOWTABLE(45,i);	Retract();}
}

else if(ch=='/')
{
ch=GetChar();
if(ch=='=')SHOWTABLE(73,i);
else {SHOWTABLE(46,i);	Retract();}
}

else if(ch=='%')SHOWTABLE(96,i);

else if(ch=='<')
{
ch=GetChar();
if(ch=='<')
{
ch=GetChar();
if(ch=='=')SHOWTABLE(81,i);
else {SHOWTABLE(80,i);	Retract();}
}
else if(ch=='=')SHOWTABLE(103,i);
else {SHOWTABLE(48,i);	Retract();}
}
else if(ch=='>')
{
ch=GetChar();
if(ch=='>')
{
ch=GetChar();
if(ch=='=')SHOWTABLE(82,i);
else {SHOWTABLE(83,i);	Retract();}
}
else if(ch=='=')SHOWTABLE(102,i);
else {SHOWTABLE(49,i);	Retract();}
}

else if(ch=='^')
{
ch=GetChar();
if(ch=='=')SHOWTABLE(74,i);
else {SHOWTABLE(51,i);	Retract();}
}
else if(ch=='|')
{
ch=GetChar();
if(ch=='=')SHOWTABLE(75,i);
else if(ch=='|')SHOWTABLE(92,i);
else {SHOWTABLE(52,i);	Retract();}
}
else if(ch=='?')SHOWTABLE(53,i);
else if(ch==':')SHOWTABLE(54,i);
else if(ch==',')SHOWTABLE(55,i);
else if(ch=='#')SHOWTABLE(90,i);
else SHOWTABLE(100,i);
i++;
}
return ;
}

char CWordInterpretDlg::GetChar()
{	
	while(m_Edit1[j]=='\n' || m_Edit1[j]=='\r' || m_Edit1[j]=='\t')
		j=j+1;

	return(m_Edit1[j++]);
	
}

void CWordInterpretDlg::GetBC()
{
	while(ch==' ')
		ch=GetChar();
	return;

}

int CWordInterpretDlg::Reserve()
{	int k=0;
	while(k<=31)
	{if(strToken==ReserveTable[k])return k++;
	k++;
	}
	return 0;
}

void CWordInterpretDlg::Retract()
{
j=j-1;
return;
}

void CWordInterpretDlg::SHOWTABLE(int nID,int i)
{
	int nCurItem;
	switch(nID)
	{
	case 1:
		{nCurItem=m_ResultList.InsertItem(i,"auto");
		m_ResultList.SetItemText(nCurItem,1,"auto");
		m_ResultList.SetItemText(nCurItem,2,"NULL");
		break;
		}
	case 2:
		{nCurItem=m_ResultList.InsertItem(i,"continue");
		m_ResultList.SetItemText(nCurItem,1,"continue");
		m_ResultList.SetItemText(nCurItem,2,"NULL");
		break;
		}
	case 3:
		{nCurItem=m_ResultList.InsertItem(i,"enum");
		m_ResultList.SetItemText(nCurItem,1,"enum");
		m_ResultList.SetItemText(nCurItem,2,"NULL");
		break;
		}
	case 4:
		{nCurItem=m_ResultList.InsertItem(i,"if");
		m_ResultList.SetItemText(nCurItem,1,"if");
		m_ResultList.SetItemText(nCurItem,2,"NULL");
		break;
		}
	case 5:
		{nCurItem=m_ResultList.InsertItem(i,"short");
		m_ResultList.SetItemText(nCurItem,1,"short");
		m_ResultList.SetItemText(nCurItem,2,"NULL");
		break;
		}
	case 6:
		{nCurItem=m_ResultList.InsertItem(i,"switch");
		m_ResultList.SetItemText(nCurItem,1,"switch");
		m_ResultList.SetItemText(nCurItem,2,"NULL");
		break;
		}
	case 7:
		{nCurItem=m_ResultList.InsertItem(i,"volatile");
		m_ResultList.SetItemText(nCurItem,1,"volatile");
		m_ResultList.SetItemText(nCurItem,2,"NULL");
		break;
		}
	case 8:
		{nCurItem=m_ResultList.InsertItem(i,"break");
		m_ResultList.SetItemText(nCurItem,1,"break");
		m_ResultList.SetItemText(nCurItem,2,"NULL");
		break;
		}
	case 9:
		{nCurItem=m_ResultList.InsertItem(i,"default");
		m_ResultList.SetItemText(nCurItem,1,"default");
		m_ResultList.SetItemText(nCurItem,2,"NULL");
		break;
		}
	case 10:

⌨️ 快捷键说明

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