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

📄 c++词法分析器1dlg.cpp

📁 用C++编写的编译原理词法分析器读磁盘文件进行词法分析
💻 CPP
字号:
// C++词法分析器1Dlg.cpp : 实现文件
//

#include "stdafx.h"
#include "C++词法分析器1.h"
#include "C++词法分析器1Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

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

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

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

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

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CC词法分析器1Dlg 对话框




CC词法分析器1Dlg::CC词法分析器1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CC词法分析器1Dlg::IDD, pParent)
	, stredit1(_T(""))
	, stredit2(_T(""))
	, stredit3(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CC词法分析器1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);

	
	DDX_Control(pDX, IDC_LIST1, listcontrolll);
}

BEGIN_MESSAGE_MAP(CC词法分析器1Dlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON1, &CC词法分析器1Dlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDOK, &CC词法分析器1Dlg::OnBnClickedOk)
END_MESSAGE_MAP()


// CC词法分析器1Dlg 消息处理程序

BOOL CC词法分析器1Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	
	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	/*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);
		}
	}*/

	listcontrolll.InsertColumn(0,"");
	listcontrolll.InsertColumn(1,"");
	listcontrolll.SetColumnWidth(0,300);
	listcontrolll.SetColumnWidth(1,90);
	listcontrolll.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT );
    return TRUE;
	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码

	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

void CC词法分析器1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CC词法分析器1Dlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作矩形中居中
		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;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标显示。
//
HCURSOR CC词法分析器1Dlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CC词法分析器1Dlg::OnBnClickedButton1()
{
	listcontrolll.DeleteAllItems();
	CString aa=_T("All Files(*.*)|*.*|cpp(*.cpp)|*.cpp|h(*.h)|*.h|txt(*.txt)|*.txt||");
	CFileDialog dialog(true,NULL,NULL,OFN_OVERWRITEPROMPT,aa,this);
	if(dialog.DoModal()==IDOK)
	{
		
		
		stredit1=dialog.GetPathName();  //
        CC词法分析器1Dlg::deal();
	}// TODO: 在此添加控件通知处理程序代码
}

void CC词法分析器1Dlg::deal(void)
{
	char last=' ';
	char save[500];
	int len=sizeof(save);
	int readed=0;
	int ptr=0;
	int statu=0;
	CString buffer;

	CFile fp1;
	if(!fp1.Open(stredit1,CFile::modeRead))
	{
		MessageBox("Can not open");
		return;
	}
	readed=fp1.Read(save,len);
	while(readed>0)
	{
		while(ptr<readed)
		{
			char current=save[ptr];
			switch(statu)
			{
			case 0:
				if((current>='a'&&current<='z')||(current>='A'&&current<='Z')||current=='_')
				{
					last=current;
					buffer.AppendChar(current);
					statu=1;
				}
				else if(current>='0'&&current<='9')
				{
					last=current;
					buffer.AppendChar(current);
					statu=2;
				}
                else if(current=='\''||current=='\"')
				{
					last=current;
					buffer.AppendChar(current);
					statu=3;
				}
				else if((current>='!'&&current<='/')||(current>=':'&&current<='@')
					||(current>='['&&current<='^')||(current>='{'&&current<='~'))
				{
					last=current;
					buffer.AppendChar(current);
					statu=4;
				}
				break;
			case 1:
				if((current>='a'&&current<='z')||(current>='A'&&current<='Z')
					||current=='_'||(current>='0'&&current<='9'))
				{
					last=current;
					buffer.AppendChar(current);
				}
				else
				{
					if(isreserved(buffer))
						show(buffer,5);
					else show(buffer,1);
					buffer="";
					ptr--;
					statu=0;
				}
				break;
			case 2:
				if(current>='0'&&current<='9')
				{
					last=current;
					buffer.AppendChar(current);
				}
				else
				{
					show(buffer,2);
					buffer="";
					ptr--;
					statu=0;
				}
				break;
			case 3:
				if((current!='\''&&current!='\"')||(last=='\\'))
				{
					if(current=='\''||current=='\"')
					{
						ptr--;
						statu=0;
					}
					else
					{
					last=current;
					buffer.AppendChar(current);
					}
				}
				else
				{
					buffer.AppendChar(current);
					show(buffer,3);
					buffer="";
					statu=0;
				}
				break;
			case 4:
				if((current>='!'&&current<='/')||(current>=':'&&current<='@')
					||(current>='['&&current<='^')||(current>='{'&&current<='~'))
				{
					buffer.AppendChar(current);
					if(last=='/'&&current=='*')
					{
						buffer.Delete(buffer.GetLength()-2,2);
						if(buffer!="")
						  show(buffer,4);
					    buffer="/*";
						statu=6;
					}
				    else if(last=='/'&&current=='/')
					{
						buffer.Delete(buffer.GetLength()-2,2);
						if(buffer!="")
						   show(buffer,4);
					    buffer="//";
						statu=7;
					}
					else if(current=='\''||current=='\"')
					{
						ptr--;
						buffer.Delete(buffer.GetLength()-1,1);
						show(buffer,4);
                        buffer="";
						statu=0;
					}

					last=current;
				}
				else
				{
					show(buffer,4);
					buffer="";
					ptr--;
					statu=0;
				}
				break;
			case 6:
				if(!(last=='*'&&current=='/'))
				{
					last=current;
					buffer.AppendChar(current);
				}
				else
				{
					buffer.AppendChar(current);
					show(buffer,6);
					buffer="";
					statu=0;
				}
                break;
			case 7:
				if(current!='\n')
				{
					last=current;
					buffer.AppendChar(current);
				}
				else
				{
					show(buffer,7);
					buffer="";
					statu=0;
				}
				break;
			default:
				MessageBox("error statu");
			}
			ptr++;
		}
		ptr=0;
		readed=fp1.Read(save,len);
	}
	fp1.Close();
	file.Close();
}






void CC词法分析器1Dlg::show(CString b, int st)
{
	CString add;
	switch(st)
	{
	
	case 1:
	    add="标识符";
		break;
	case 2:
	    add="数字";
		break;
	case 3:
	    add="字符串";
		break;
	case 4:
	    add="特殊字符";
		break;
	case 5:
	    add="保留字";
		break;
	case 6:
	    add="注释";
		break;
	case 7:
	    add="注释";
		break;
	default:
        MessageBox("error statu2");
	}
	int count=listcontrolll.GetItemCount();
	if(st!=4)
	{
		listcontrolll.InsertItem(count,b);
	    listcontrolll.SetItemText(count,1,add);
		
      
	}
	else
	{
		int len=b.GetLength();
		for(int i=0;i<len;i++)
		{
			char *u=b.GetBuffer();
			CString k;
			k.AppendChar(u[i]);
            listcontrolll.InsertItem(count,k);
	        listcontrolll.SetItemText(count,1,add);
		
			count++;
            
		}
	}
}

bool CC词法分析器1Dlg::isreserved(CString buf)
{
       CString reserved[37] = {
	 "array", "case", "const","do", "else"," for", "goto","if","goto",
     "while","public","int","double","long","class","char","float","short","void",
	 "default","true","false","break","sizeof","return","protected","private",
	  "enum","bool","virtual","extern","switch","new","include","ifdefine","endif","define"};
	 for(int i=0;i<37;i++)
	 {
		 if(buf==reserved[i])
			 return true;
	 }
	return false;
}

void CC词法分析器1Dlg::OnBnClickedOk()
{
    /*CString cc=_T("All Files(*.*)|*.*|cpp(*.cpp)|*.cpp|h(*.h)|*.h|txt(*.txt)|*.txt||");
	CFileDialog dialog(true,NULL,NULL,OFN_OVERWRITEPROMPT,stredit2,this);
	if(dialog.DoModal()==IDOK)
	{
		stredit2=dialog.GetPathName();
        ::CreateDirectoryA(stredit2,NULL);
        
	}*/
	CString bb=_T("All Files(*.*)|*.*|cpp(*.cpp)|*.cpp|h(*.h)|*.h|txt(*.txt)|*.txt||");
	CFileDialog dialog1(true,NULL,NULL,OFN_OVERWRITEPROMPT,bb,this);
	if(dialog1.DoModal()==IDOK)
	{
		stredit3=dialog1.GetFileName();
        stredit2=dialog1.GetPathName();
        //file.Open(stredit2,CFile::modeCreate|CFile::modeReadWrite);
       
		
        CC词法分析器1Dlg::conserve();
	}
	// TODO: 在此添加控件通知处理程序代码
	//OnOK();
}

void CC词法分析器1Dlg::conserve(void)
{
        CString b;
	    CString add;
        CFile fp2;
        if(!fp2.Open(stredit3,CFile::modeCreate|CFile::modeReadWrite))
	
		{
		
			MessageBox("Can not open");
		
			return;
	
		}
		int i;
		for(i=0;i<listcontrolll.GetItemCount();i++)
		{
			CString aa=listcontrolll.GetItemText(i,0);
			CString bb=listcontrolll.GetItemText(i,1);
			aa.Append("-----------------");
			fp2.Write(aa.GetBuffer(),aa.GetLength());
			fp2.Write(bb.GetBuffer(),bb.GetLength());
			fp2.Write("\r\n",2);
		}


     
			
		
		fp2.Close();
		file.Close();
}

⌨️ 快捷键说明

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