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

📄 granalyserdlg.cpp

📁 使用vc编写的文法分析器课件
💻 CPP
字号:
// GrAnalyserDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GrAnalyser.h"
#include "GrAnalyserDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CGrAnalyserDlg dialog

CGrAnalyserDlg::CGrAnalyserDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGrAnalyserDlg::IDD, pParent)
{
	Vt[0]='i';Vt[1]='+';Vt[2]='*';Vt[3]='(';Vt[4]=')';Vt[5]='#';
	Vn[0]='E';Vn[1]='e';Vn[2]='T';Vn[3]='t';Vn[4]='F';
	for(int m=0;m<5;m++)
		for(int n=0;n<6;n++)
		{
			table[m][n].Frule ="^";
			table[m][n].Trule ="";
		}
	table[0][0].Trule =" -> TE'";table[0][0].Frule ="Te";
	table[0][3].Trule =" -> TE'";table[0][3].Frule ="Te";
	table[1][1].Trule =" -> +TE'";table[1][1].Frule ="+Te";
	table[1][4].Trule =" -> ε";table[1][4].Frule ="ε";
	table[1][5].Trule =" -> ε";table[1][5].Frule ="ε";
	table[2][0].Trule =" -> FT'";table[2][0].Frule ="Ft";
	table[2][3].Trule =" -> FT'";table[2][3].Frule ="Ft";
	table[3][1].Trule =" -> ε";table[3][1].Frule ="ε";
	table[3][2].Trule =" -> *FT'";table[3][2].Frule ="*Ft";
	table[3][4].Trule =" -> ε";table[3][4].Frule ="ε";
	table[3][5].Trule =" -> ε";table[3][5].Frule ="ε";
	table[4][0].Trule =" -> i";table[4][0].Frule ="i";
	table[4][3].Trule =" -> (E)";table[4][3].Frule ="(E)";
	charbuf="";
	//{{AFX_DATA_INIT(CGrAnalyserDlg)
	m_input = _T("");
	m_count1 = 0;
	m_count2 = 0;
	m_count3 = 0;
	m_info = _T("本版本仅限课内使用\015\012\015\012王亮\015\012计算机一班");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGrAnalyserDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGrAnalyserDlg)
	DDX_Control(pDX, IDC_LIST3, m_list3);
	DDX_Control(pDX, IDC_LIST2, m_list2);
	DDX_Control(pDX, IDC_LIST1, m_list1);
	DDX_Text(pDX, IDC_input, m_input);
	DDV_MaxChars(pDX, m_input, 35);
	DDX_Text(pDX, IDC_count1, m_count1);
	DDX_Text(pDX, IDC_count2, m_count2);
	DDX_Text(pDX, IDC_count3, m_count3);
	DDX_Text(pDX, IDC_info2, m_info);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGrAnalyserDlg, CDialog)
	//{{AFX_MSG_MAP(CGrAnalyserDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_analyse, Onanalyse)
	ON_BN_CLICKED(IDC_cleanit, Oncleanit)
	ON_BN_CLICKED(IDC_reedit, Onreedit)
	ON_BN_CLICKED(IDC_info1, Oninfo1)
	ON_BN_CLICKED(IDC_errora, Onerrora)
	ON_BN_CLICKED(IDC_errorb, Onerrorb)
	ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
	ON_LBN_SELCHANGE(IDC_LIST2, OnSelchangeList2)
	ON_LBN_SELCHANGE(IDC_LIST3, OnSelchangeList3)
	ON_BN_CLICKED(IDC_example, Onexample)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGrAnalyserDlg message handlers

BOOL CGrAnalyserDlg::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 CGrAnalyserDlg::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 CGrAnalyserDlg::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 CGrAnalyserDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
Lstack::Lstack(void)
{
	count=0;
	top=NULL;
}
Lstack::~Lstack()
{
	node *p;
	while(top!=NULL)
	{
		p=top;
		top=top->next ;
		delete p;
	}
}
void Lstack::PUSHstack (char newdata)
{
	node *p=new node;
	if(p==NULL)
	{
		AfxMessageBox("内存不足!");
		return;
	}
	p->data =newdata;
	p->next =NULL;
	if(top==NULL)
		top=p;
	else
	{
		p->next =top;
		top=p;
	}
	count++;
}
char Lstack::POPstack ()
{
	node * p;
	char q;
	if(top==NULL)
	{
		AfxMessageBox("栈已空!非法结束!");
		exit(1);
	}
	p=top;
	top=top->next ;
	q=p->data;
	delete p;
	count--;
	return q;
}





CString Lstack::PRIstack()
{
	CString view;
	node *p;
	p=top;
	while(p!=NULL)
	{
		if(p->data =='e')
			view="E'" + view;
		else if(p->data =='t')
			view="T'" + view;
		else
			view=p->data + view;
		p=p->next ;
	}
	return view;
}

chart CGrAnalyserDlg::rulecheck(CString X, CString a)
{
	int x=0,y=0;
	chart error;
	error.Frule ="^^";
	while(x<5)
	{
		if(Vn[x]!=X)
			x++;
		else
			break;
	}
	while(y<6)
	{
		if(Vt[y]!=a)
			y++;
		else
			break;
	}
	if((x==5)||(y==6))
		return error;
	return table[x][y];
}

void CGrAnalyserDlg::Onanalyse() 
{
	Lstack st;
	int i=0,j,k;
	bool z;
	CString cmpbufa,cmpbufX;
	CString charbuf;
	chart rulebuf;
	UpdateData(true);
	if(m_input=="")
	{
		MessageBox("请先输入<符号串>!","错误");
		return;
	}
	charbuf=m_input;
	charbuf=charbuf+"#";
	st.PUSHstack ('#');
	st.PUSHstack ('E');
	cmpbufa=charbuf.GetAt (i);
	while(1)
	{
		m_list1.AddString (st.PRIstack());
		m_list2.AddString (charbuf.Mid (i));
		cmpbufX=st.POPstack ();
		z=false;
		for(j=0;j<6;j++)
			if(cmpbufX==Vt[j])
			{
				z=true;
				break;
			}
		if(z)
			if(cmpbufX==cmpbufa)
				if(cmpbufX=='#')
				{
					m_list3.AddString ("接受");
					break;
				}
				else
				{
					m_list3.AddString (cmpbufa + "匹配");
					cmpbufa=charbuf.GetAt (++i);
				}
			else
			{
				m_list3.AddString ("第B类错误!");
				break;
			}
		else
		{
			rulebuf=rulecheck(cmpbufX,cmpbufa);
			if(rulebuf.Frule =="^^")
			{
				m_list3.AddString ("无效输入!");
				MessageBox("输入了错误的符号串!","错误");
				break;
			}
			else if(rulebuf.Frule =="^")
			{
				m_list3.AddString ("第A类错误!");
				break;
			}
			else if(rulebuf.Frule =="ε")
				if(cmpbufX=='e')
					m_list3.AddString ("E'"+rulebuf.Trule);
				else if(cmpbufX=='t')
					m_list3.AddString("T'"+rulebuf.Trule);
				else
					m_list3.AddString (cmpbufX + rulebuf.Trule);
			else
			{
				k=0;
				while(rulebuf.Frule.Mid (k)!="")
				{k++;}
				while(k>0)
				{st.PUSHstack (rulebuf.Frule.GetAt (--k));}
				if(cmpbufX=='e')
					m_list3.AddString ("E'" + rulebuf.Trule);
				else if(cmpbufX=='t')
					m_list3.AddString("T'" + rulebuf.Trule);
				else
					m_list3.AddString (cmpbufX + rulebuf.Trule);
			}
		}
	}	
}

void CGrAnalyserDlg::Oncleanit() 
{
	m_list1.ResetContent();
	m_list2.ResetContent();
	m_list3.ResetContent();
	m_count1=0;
	m_count2=0;
	m_count3=0;
	UpdateData(false);
	
}

void CGrAnalyserDlg::Onreedit() 
{
	m_input="";
	UpdateData(false);	
}

void CGrAnalyserDlg::Oninfo1() 
{
	MessageBox("E->TE'\015E'->+TE'|ε\015T->FT'\015T'->*FT'|ε\015F->i|(E)","LL(1)文法");	
}

void CGrAnalyserDlg::Onerrora() 
{
	MessageBox("不合文法,无法匹配!","第A类错误");	
}

void CGrAnalyserDlg::Onerrorb() 
{
	MessageBox("无法匹配的意外错误!","第B类错误");	
}

void CGrAnalyserDlg::OnSelchangeList1() 
{
	m_count1=1+m_list1.GetCurSel();
	UpdateData(false);
}

void CGrAnalyserDlg::OnSelchangeList2() 
{
	m_count2=1+m_list2.GetCurSel();
	UpdateData(false);	
}

void CGrAnalyserDlg::OnSelchangeList3() 
{
	m_count3=1+m_list3.GetCurSel();
	UpdateData(false);	
}

void CGrAnalyserDlg::Onexample() 
{
	m_input="i+i*i";
	UpdateData(false);
}

⌨️ 快捷键说明

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