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

📄 词法分析dlg.cpp

📁 编译程序的实验~~ 很详细哈..需要的下来看看!
💻 CPP
字号:
// 词法分析Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "词法分析.h"
#include "词法分析Dlg.h"

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

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

struct Cifa
{
	int type;//1:保留字,2:标识符,3:常数,4:运算符,5:分隔符,6:字符串,7:宏定义和文件包含处理
	CString text;//单词
};
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()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

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

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
	DDX_Control(pDX, IDC_BUTTON1, m_cifafenxi);
	DDX_Text(pDX, IDC_EDIT1, m_CodeIn);
	DDX_Text(pDX, IDC_EDIT2, m_CodeOut);
	//}}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_BUTTON1, cifafenxi)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

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

void CMyDlg::OnOK() 
{
	// TODO: Add extra validation here
}
int keyword(CString str)//保留字检查,值为1
{
	if(str=="main")
		return 1;
	else if(str=="auto")
		return 1;
	else if(str=="break")
		return 1;
	else if(str=="case")
		return 1;
	else if(str=="char")
		return 1;
	else if(str=="const")
		return 1;
	else if(str=="continute")
		return 1;
	else if(str=="default")
		return 1;
	else if(str=="do")
		return 1;
	else if(str=="double")
		return 1;
	else if(str=="else")
		return 1;
	else if(str=="enum")
		return 1;
	else if(str=="extern")
		return 1;
	else if(str=="float")
		return 1;
	else if(str=="for")
		return 1;
	else if(str=="goto")
		return 1;
	else if(str=="if")
		return 1;
	else if(str=="int")
		return 1;
	else if(str=="long")
		return 1;
	else if(str=="register")
		return 1;
	else if(str=="return")
		return 1;
	else if(str=="short")
		return 1;
	else if(str=="signed")
		return 1;
	else if(str=="sizof")
		return 1;
	else if(str=="static")
		return 1;
	else if(str=="struct")
		return 1;
	else if(str=="switch")
		return 1;
	else if(str=="typedef")
		return 1;
	else if(str=="union")
		return 1;
	else if(str=="void")
		return 1;
	else if(str=="colatile")
		return 1;
	else if(str=="while")
		return 1;
	else 
		return 0;
}

int identifier(CString str)//标识符检查,值为2
{
	if((str.GetAt(0)<=90&&str.GetAt(0)>=65)||(str.GetAt(0)<=122&&str.GetAt(0)>=97)||str.GetAt(0)=='_'){
		for(int i=1;i<str.GetLength();i++){
			if((str.GetAt(i)<=90&&str.GetAt(i)>=65)||(str.GetAt(i)<=122&&str.GetAt(i)>=97)||str.GetAt(i)=='_'||(str.GetAt(i)<=57&&str.GetAt(i)>=48))
				;
			else
				return 0;
		}
		return 1;
	}
	else
		return 0;
}

int number(CString str)//常数检查,值为3
{
	for(int i=0;i<str.GetLength();i++){
		if(str.GetAt(i)<=57&&str.GetAt(i)>=48)
			;
		else
			return 0;
	}
	return 3;
}

void CMyDlg::cifafenxi() 
{
	// TODO: Add your control notification handler code here
	GetDlgItemText(IDC_EDIT1,m_CodeIn);
	int sz=m_CodeIn.GetLength();
    CString str="";
    Cifa m_Cifa;
	m_Cifa.text.Empty();m_Cifa.type=NULL;
	int i=0,j=0,k=0,error=0;
	for(;i<sz&&m_CodeIn.GetAt(i)==' ';){
		i++;
	}
	m_CodeOut.Empty();
	m_CodeOut=m_CodeOut+"输出格式:二元式序列(类型,单词)\r\n\r\n";
	for(i;i<sz;i++){
		switch(m_CodeIn.GetAt(i)){
		//运算符,type值为4
		case '+':
			m_Cifa.text.Insert(m_Cifa.text.GetLength(),m_CodeIn.GetAt(i));
			m_Cifa.type=4;
			if(m_CodeIn.GetLength()>i+1&&(m_CodeIn.GetAt(i+1)=='+'||m_CodeIn.GetAt(i+1)=='=')){
				m_Cifa.text.Insert(m_Cifa.text.GetLength(),m_CodeIn.GetAt(i+1));
			    j=1;
			}
			break;
		case '-':
			m_Cifa.text.Insert(m_Cifa.text.GetLength(),m_CodeIn.GetAt(i));
			if(m_CodeIn.GetLength()>i+1&&(m_CodeIn.GetAt(i+1)=='-'||m_CodeIn.GetAt(i+1)=='='||m_CodeIn.GetAt(i+1)=='>')){
				m_Cifa.text.Insert(m_Cifa.text.GetLength(),m_CodeIn.GetAt(i+1));
				j=1;
			}
			m_Cifa.type=4;
			break;
		case '*':
		case '/':	
		case '%':	
		case '^':
		case '=':
		case '!':
			m_Cifa.text.Insert(m_Cifa.text.GetLength(),m_CodeIn.GetAt(i));
			if(m_CodeIn.GetLength()>i+1&&m_CodeIn.GetAt(i+1)=='='){
				m_Cifa.text.Insert(m_Cifa.text.GetLength(),m_CodeIn.GetAt(i+1));
				j=1;
			}
			m_Cifa.type=4;
			break;
		case '&':
		case '|':
			m_Cifa.text.Insert(m_Cifa.text.GetLength(),m_CodeIn.GetAt(i));
			if(m_CodeIn.GetLength()>i+1&&(m_CodeIn.GetAt(i+1)=='='||m_CodeIn.GetAt(i+1)==m_CodeIn.GetAt(i))){
				m_Cifa.text.Insert(m_Cifa.text.GetLength(),m_CodeIn.GetAt(i+1));
				j=1;
			}
			m_Cifa.type=4;
			break;
		case '<':
		case '>':
			m_Cifa.text.Insert(m_Cifa.text.GetLength(),m_CodeIn.GetAt(i));
			if(m_CodeIn.GetLength()>i+1&&m_CodeIn.GetAt(i+1)==m_CodeIn.GetAt(i)){
				m_Cifa.text.Insert(m_Cifa.text.GetLength(),m_CodeIn.GetAt(i+1));
				j=1;
				if(m_CodeIn.GetLength()>i+2&&m_CodeIn.GetAt(i+2)=='='){
					m_Cifa.text.Insert(m_Cifa.text.GetLength(),m_CodeIn.GetAt(i+2));
					j=2;
				}
			}else if(m_CodeIn.GetLength()>i+1&&m_CodeIn.GetAt(i+1)=='='){
				m_Cifa.text.Insert(m_Cifa.text.GetLength(),m_CodeIn.GetAt(i+1));
				j=1;
			}
			m_Cifa.type=4;
			break;
		case '~':
		case '?':
		case ':':
			m_Cifa.text.Insert(m_Cifa.text.GetLength(),m_CodeIn.GetAt(i));
			m_Cifa.type=4;
			break;
		//分隔符,type值为5
		case '(':
		case ')':
		case '[':
		case ']':
		case '{':
		case '}':
		case ',':
		case ';':
			m_Cifa.text.Insert(m_Cifa.text.GetLength(),m_CodeIn.GetAt(i));
			m_Cifa.type=5;
			break;
		//字符串,type值为6
		case '"':
			m_Cifa.text.Insert(m_Cifa.text.GetLength(),m_CodeIn.GetAt(i));
			i++;
			while(m_CodeIn.GetAt(i)!='"'){
				m_Cifa.text.Insert(m_Cifa.text.GetLength(),m_CodeIn.GetAt(i));
			    i++;
			}
			m_Cifa.text.Insert(m_Cifa.text.GetLength(),m_CodeIn.GetAt(i));
			m_Cifa.type=6;
			break;
		case '#'://宏定义和文件包含处理,type值为7
			while(m_CodeIn.GetAt(i)!='\r'&&m_CodeIn.GetAt(i)!='\n'&&m_CodeIn.GetAt(i)!='\r\n'){
				m_Cifa.text.Insert(m_Cifa.text.GetLength(),m_CodeIn.GetAt(i));
			    i++;
			}
			m_Cifa.type=7;
		//空格和回车,跳过不读
		case ' ':
		case '\r':
		case '\n':
		case '\r\n':
			break;
		default:
			str.Insert(str.GetLength(),m_CodeIn.GetAt(i));
			k=1;
		}
		if(k==0||i==m_CodeIn.GetLength()-1){
			if(str!=""){
				if(keyword(str)==1){//保留字检查,值为1
					m_CodeOut=m_CodeOut+"(1,\"";					
					m_CodeOut=m_CodeOut+str;
					m_CodeOut=m_CodeOut+"\")\r\n";
					str.Empty();
				}
				else if(identifier(str)){//标识符检查,值为2
					m_CodeOut=m_CodeOut+"(2,\"";
					m_CodeOut=m_CodeOut+str;
					m_CodeOut=m_CodeOut+"\")\r\n";
					str.Empty();
				}
				else if(number(str)){//常数检查,值为3
					m_CodeOut=m_CodeOut+"(3,\"";
					m_CodeOut=m_CodeOut+str;
					m_CodeOut=m_CodeOut+"\")\r\n";
					str.Empty();
				}
				else{
					m_CodeOut=m_CodeOut+"(0,\"";
					m_CodeOut=m_CodeOut+str;
					m_CodeOut=m_CodeOut+"\")";
					m_CodeOut=m_CodeOut+"错误,发现非法字符!\r\n";
					error++;
					str.Empty();
				}
			}
			if(!m_Cifa.text.IsEmpty()){
				str.Format("%d",m_Cifa.type);
				m_CodeOut=m_CodeOut+"("+str+",\"";
				str.Empty();
				m_CodeOut=m_CodeOut+m_Cifa.text;
				m_CodeOut=m_CodeOut+"\")\r\n";
				m_Cifa.text.Empty();
				m_Cifa.type=NULL;
			}
		}
		k=0;
		i=i+j;j=0;
	}
	str.Empty();
	str.Format("\r\n 发现 %d 个错误!\r\n",error);
	m_CodeOut=m_CodeOut+str;
	str.Empty();
	SetDlgItemText(IDC_EDIT2,m_CodeOut);
	m_CodeOut.Empty();
}

⌨️ 快捷键说明

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