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

📄 lrdlg.cpp

📁 这是一个lr文法分析器
💻 CPP
字号:
// lrDlg.cpp : implementation file
//

#include "stdafx.h"
#include "lr.h"
#include "lrDlg.h"
#include "stack.h"
#include "cc.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()

/////////////////////////////////////////////////////////////////////////////
// CLrDlg dialog

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

void CLrDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLrDlg)
	DDX_Control(pDX, IDC_LIST1, m_userlist);
	DDX_Text(pDX, IDC_EDIT2, m_juzi);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLrDlg, CDialog)
	//{{AFX_MSG_MAP(CLrDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON2, Onfenxi)
	ON_BN_CLICKED(IDC_BUTTON1, Onopen)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLrDlg message handlers

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

void CLrDlg::Onfenxi() 
{
	m_userlist.InsertColumn(0,"步骤",LVCFMT_LEFT,40);
	m_userlist.InsertColumn(1,"状态",LVCFMT_LEFT,100);
	m_userlist.InsertColumn(2,"符号栈",LVCFMT_LEFT,100);
	m_userlist.InsertColumn(3,"输入串",LVCFMT_LEFT,100);
    


	//初始化列表的项目






		char R1[]={'a','b','c','d','#','E','A','B'};
	char R2[]={'0','1','2','3','4','5','6','7','8','9','a','b'};
	char *LR[][12]={{"s2","s3","NULL","NULL","NULL","1","NULL","NULL"},
	{"NULL","NULL","NULL","NULL","acc","NULL","NULL","NULL"},
	{"NULL","NULL","s4","sa","NULL","NULL","6","NULL"},
	{"NULL","NULL","s5","sb","NULL","NULL","NULL","7"},
	{"NULL","NULL","s4","sa","NULL","NULL","8","NULL"},
	{"NULL","NULL","s5","sb","NULL","NULL","NULL","9"},
	{"r1","r1","r1","r1","r1","NULL","NULL","NULL"},
	{"r2","r2","r2","r2","r2","NULL","NULL","NULL"},
	{"r3","r3","r3","r3","r3","NULL","NULL","NULL"},
	{"r5","r5","r5","r5","r5","NULL","NULL","NULL"},
	{"r4","r4","r4","r4","r4","NULL","NULL","NULL"},
	{"r6","r6","r6","r6","r6","NULL","NULL","NULL"}};
	char *R[]={"S→E","E→aA","E→bB","A→cA","A→d","B→cB","B→d"};
	char *RT[]={"E","aA","bB","cA","d","cB","d"};
	char *RH[]={"S","E","E","A","A","B","B"};
	//char word[50];
	int nItem;
	int i, j=0, k=0, a, b, mm, no=0;
	stack s;

    // char word[]={'a' ,'c' ,'c' ,'c' ,'d','#','\0'};

		char word[50];
    int index;
	this->UpdateData(true);//获得输入的字符串
	for(index=0;index<m_juzi.GetLength();index++)
	{
		
		word[index] = m_juzi.GetAt(index);
	}
	word[index]='\0';





     CString vm0,vm1,vm2,vm3;
	 vm0.Format("%d",no);
	vm1.Format("%s",s.c.con);
	vm2.Format("%s",s.c.st);
    vm3.Format("%s",(word+j));


    m_userlist.SetItemState(m_nCurrentSel,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);
   nItem=m_userlist.InsertItem(0xffff,(_bstr_t)vm0);//先插第一个项
   m_userlist.SetItem(nItem,1,1,(_bstr_t)vm1 ,NULL,0,0,0);
   m_userlist.SetItem(nItem,2,1,(_bstr_t)vm2,NULL,0,0,0);
   m_userlist.SetItem(nItem,3,1,(_bstr_t)vm3,NULL,0,0,0);  

	 //cout<<setw(5)<<no<<setw(10)<<s.c.con<<setw(15)<<s.c.st<<setw(15)<<(word+j)<<endl;


   
   
   
   
   do{
		for(i=0; i<=11; i++)
			if(R2[i]==s.c.con[s.topcon-1]) {a=i; break;}

		for(i=0; i<=7; i++)
			if(R1[i]==word[j]) {b=i; break;}

		if(i>7) {AfxMessageBox("输出错误,分析中止!"); break;}

		if(*(LR[a][b])=='s'){
			s.pushst(word,  j);
			s.pushcon(LR[a][b], 1);
			j++;
			no++;
			//cout<<setw(5)<<no<<setw(10)<<s.c.con<<setw(15)<<s.c.st<<setw(15)<<(word+j)<<endl;
		vm0.Format("%d",no);
		vm1.Format("%s",s.c.con);
		vm2.Format("%s",s.c.st);
		vm3.Format("%s",(word+j));


         m_userlist.SetItemState(m_nCurrentSel,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);
         nItem=m_userlist.InsertItem(0xffff,(_bstr_t)vm0);//先插第一个项
         m_userlist.SetItem(nItem,1,1,(_bstr_t)vm1 ,NULL,0,0,0);
         m_userlist.SetItem(nItem,2,1,(_bstr_t)vm2,NULL,0,0,0);
         m_userlist.SetItem(nItem,3,1,(_bstr_t)vm3,NULL,0,0,0); 

		}
		else if(*(LR[a][b])=='r'){
			mm=strlen(RT[*(LR[a][b]+1)-48]);
			for(i=0; i<=mm-1; i++)
				s.pop();

			s.pushst(RH[*(LR[a][b]+1)-48], 0);

            for(i=0; i<=11; i++)
				if(R2[i]==s.c.con[s.topcon-1]) {a=i; break;}

		    for(i=0; i<=7; i++)
			    if(R1[i]==s.c.st[s.topst-1]) {b=i; break;}

			s.pushcon(LR[a][b], 0);

			no++;
			//cout<<setw(5)<<no<<setw(10)<<s.c.con<<setw(15)<<s.c.st<<setw(15)<<(word+j)<<endl;
		
			vm0.Format("%d",no);
			vm1.Format("%s",s.c.con);
			vm2.Format("%s",s.c.st);
			vm3.Format("%s",(word+j));


			m_userlist.SetItemState(m_nCurrentSel,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);
			nItem=m_userlist.InsertItem(0xffff,(_bstr_t)vm0);//先插第一个项
			m_userlist.SetItem(nItem,1,1,(_bstr_t)vm1 ,NULL,0,0,0);
			m_userlist.SetItem(nItem,2,1,(_bstr_t)vm2,NULL,0,0,0);
			m_userlist.SetItem(nItem,3,1,(_bstr_t)vm3,NULL,0,0,0); 
		
		}
		else if(*(LR[a][b])=='a'){
			AfxMessageBox("分析成功!");
			k=1;
		}
		else if(*(LR[a][b])=='N'){
			AfxMessageBox("输出错误,分析中止!");
			k=1;
		}
		
	}while(k!=1);








	// TODO: Add your control notification handler code here
	
}

void CLrDlg::Onopen() 
{




	CFileDialog fd(true);
	fd.m_ofn.lpstrTitle="请选择你要打开的文件";  //标题
	fd.m_ofn.lpstrInitialDir="e:\\";            //初始目录
	if(fd.DoModal())
	{
		m_strFileName=fd.GetPathName();
		
		CStdioFile sf;
		CString str;
		if(sf.Open(m_strFileName.GetBuffer(0),CFile::modeRead))
		{
			CString  strTemp;
			while(sf.ReadString(strTemp))
			{
				str+=strTemp;
				str+="\r\n";
			}	
			SetDlgItemText(IDC_EDIT1,str);
			
			sf.Close();
		}



	
	
	// TODO: Add your control notification handler code here
	
}
}

⌨️ 快捷键说明

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