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

📄 l1dlg.cpp

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

#include "stdafx.h"
#include "L1.h"
#include "L1Dlg.h"
#include"stack.h"
#include "string.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()

/////////////////////////////////////////////////////////////////////////////
// CL1Dlg dialog

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CL1Dlg message handlers

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

void CL1Dlg::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
	
}

void CL1Dlg::Onfenxiwenfa() 
{


	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);
    int nItem;
    _variant_t vm;
    	stack s;
	int i, ii, j=0, k=0, m=0, a, b;
	char L1[]={'E' ,'S' ,'T' ,'W' ,'F'};
	char L2[]={'i' ,'+' ,'*' ,'(' ,')' ,'#'};
	char *LL[][6]={{"TS" ,"NULL"  ,"NULL"  ,"TS"  ,"NULL" ,"NULL"}, 
	{"NULL" ,"+TS" ,"NULL"  ,"NULL"  ,"" ,""} ,
	{"FW" ,"NULL"  ,"NULL"  ,"FW"  ,"NULL" ,"NULL"} ,
	{"NULL" ,""  ,"*FW" ,"NULL"  ,"" ,""} ,
	{"i"  ,"NULL"  ,"NULL"  ,"(E)" ,"NULL","NULL"  }};
	char *LLO[][6]={{"E→TS" ,"NULL"     ,"NULL"    ,"E→TS"  ,"NULL"    ,"NULL"  } ,
	{"NULL"    ,"S→+TS" ,"NULL"     ,"NULL"     ,"S→ε" ,"S→ε" },
	{"T→FW"   ,"NULL"     ,"NULL"     ,"T→FW"  ,"NULL"    ,"NULL"   },
	{"NULL"    ,"W→ε"  ,"W→*FW" ,"NULL"    ,"W→ε" ,"W→ε" },
	{ "F→i"  ,"NULL"     ,"NULL"     ,"F→(E)" ,"NULL"    ,"NULL"   }};
//	char word[]={'i' ,'+' ,'i' ,'*' ,'i','#','\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';	
	//if(word[0]=='i')
	//AfxMessageBox("aaaaaaaaaaaa");
//	else
	//	AfxMessageBox("bbbbbbbbb");

	
	
	
	
	
	
	CString vm0,vm1,vm2,vm3;

   //vm = char * (m);
    //vm = cstring(word[2]);
    //	vm = char* (m);
	vm0.Format("%d",m);
	vm1.Format("%c",s.st);
	vm2.Format("%c",word);
   // m_test= s.st;
//	UpdateData(false);
   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)s.st ,NULL,0,0,0);
   m_userlist.SetItem(nItem,2,1,(_bstr_t)word,NULL,0,0,0);
   m++;
	// TODO: Add your control notification handler code here
	
	do{
			if(s.st[s.top-1]=='#') {
			k=1;
		}
		else if(s.st[s.top-1]==word[j]){
			while(s.st[s.top-1]==word[j]){
				s.pop();
				j++;
			}
			//cout<<setw(5)<<m<<setw(15)<<s.st<<setw(15)<<(word+j)<<endl;
		
		
            vm0.Format("%d",m);
			vm1.Format("%c",s.st);
			vm2.Format("%s",(word+j));
            //m_test= word+j;
        	//UpdateData(false);

            nItem=m_userlist.InsertItem(0xffff,(_bstr_t)vm0);//先插第一个项
			m_userlist.SetItem(nItem,1,1,(_bstr_t)s.st ,NULL,0,0,0);
			m_userlist.SetItem(nItem,2,1,(_bstr_t)vm2,NULL,0,0,0);
			
			
			m++;
		}
		else{
			for(i=0; i<=4; i++)
				if(L1[i]==s.st[s.top-1]) {a=i; break; }

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

			if(!strcmp(LLO[a][b],"NULL")){
				//cout<<"输入错误,无法识别"<<endl;
				
			
				//Messagebox("输入错误,无法识别");
				AfxMessageBox("输入错误,无法识别");
				k=1;
			}

			if(k!=1){
			s.pop ();
			s.push(LL[a][b]);
           // cout<<setw(5)<<m<<setw(15)<<s.st<<setw(15)<<(word+j)<<setw(15)<<LLO[a][b]<<endl;
			




			vm0.Format("%d",m);
			vm1.Format("%c",s.st);
			vm2.Format("%s",(word+j));
            vm3.Format("%s",LLO[a][b]);
            //m_test= s.st;
        	//UpdateData(false);

            nItem=m_userlist.InsertItem(0xffff,(_bstr_t)vm0);//先插第一个项
			m_userlist.SetItem(nItem,1,1,(_bstr_t)s.st ,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);


			m++;
			}
		} 

	}while(k!=1);
}

⌨️ 快捷键说明

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