📄 语法分析dlg.cpp
字号:
// 语法分析Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "语法分析.h"
#include "语法分析Dlg.h"
#include "DSHOW.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()
/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog
CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDlg)
m_enter = _T("");
m_show = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
step=1;
k=0;
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
DDX_Control(pDX, IDC_LIST_process, m_process);
DDX_Control(pDX, IDC_LIST_gr, m_gr);
DDX_Text(pDX, IDC_EDIT_enter, m_enter);
DDX_Text(pDX, IDC_EDIT1, m_show);
//}}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_BUTTON_start, OnBUTTONstart)
ON_BN_CLICKED(IDC_BUTTON_actor, OnBUTTONactor)
//}}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
m_gr.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
m_gr.InsertColumn(0,"句型语法");
m_gr.SetColumnWidth(0,151);
m_gr.InsertItem(0,"r1: Z::= A");
m_gr.InsertItem(1,"r2: Z::= B");
m_gr.InsertItem(2,"r3: A::= aAb");
m_gr.InsertItem(3,"r4: A::= c");
m_gr.InsertItem(4,"r5: B::= aBb");
m_gr.InsertItem(5,"r6: B::= b");
m_process.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
m_process.InsertColumn(0,"步骤");
m_process.InsertColumn(1,"状态栈");
m_process.InsertColumn(2,"符号栈");
m_process.InsertColumn(3,"输入串");
m_process.InsertColumn(4,"分析动作");
m_process.InsertColumn(5,"下一状态");
m_process.SetColumnWidth(0,30);
m_process.SetColumnWidth(1,60);
m_process.SetColumnWidth(2,100);
m_process.SetColumnWidth(3,100);
m_process.SetColumnWidth(4,60);
m_process.SetColumnWidth(5,100);
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::OnBUTTONstart()
{
m_process.DeleteAllItems();
UpdateData(true);
Enter=new char[strlen(m_enter)+2];
strcpy(Enter,m_enter);
strcat(Enter,"#");
step=1;
k=0;
j=0;
m_state=0;
r_state=0;
state[0]='0';
state[1]='\0';
r_mark=0;
mark[0]='#';
mark[1]='\0';
now_enter=Enter[j];
record_enter=Enter[j];
if(now_enter=='a')
{
actor="S1";
nextState="1";
m_state=1;
j++;
}
else if(now_enter=='b')
{
actor="S2";
nextState="2";
m_state=2;
j++;
}
else if(now_enter=='c')
{
actor="S3";
nextState="3";
m_state=3;
j++;
}
else
{
m_show="定义不合法";
UpdateData(false);
return;
}
str.Format("%d",step);
m_process.InsertItem(k,str);
m_process.SetItemText(k,1,"0");
m_process.SetItemText(k,2,mark);
m_process.SetItemText(k,3,Enter);
m_process.SetItemText(k,4,actor);
m_process.SetItemText(k,5,nextState);
k++;
//j++;
step++;
p=0;
int i=0;
int c=-1;
while(true)
{
c=actorStart();
if(c==0)
{
m_show="定义不合法";
UpdateData(false);
break;
}
else if(c==1||c==2)
{
str.Format("%d",step);
m_process.InsertItem(k,str);
m_process.SetItemText(k,1,state);
m_process.SetItemText(k,2,mark);
m_process.SetItemText(k,3,Enter);
m_process.SetItemText(k,4,actor);
m_process.SetItemText(k,5,nextState);
k++;
step++;
now_enter=Enter[j];
record_enter=Enter[j-1];
}
else if(c==4)
{
str.Format("%d",step);
m_process.InsertItem(k,str);
m_process.SetItemText(k,1,state);
m_process.SetItemText(k,2,mark);
m_process.SetItemText(k,3,Enter);
m_process.SetItemText(k,4,actor);
m_process.SetItemText(k,5,nextState);
k++;
step++;
m_show="合法定义";
UpdateData(false);
break;
}
}
}
int CMyDlg::actorStart()
{
//strcat(st)
// AfxMessageBox(state);
if(actor=="S1")
{
r_state++;
state[r_state]='1';
state[r_state+1]='\0';
r_mark++;
mark[r_mark]=record_enter;
mark[r_mark+1]='\0';
Enter[p]=' ';
p++;
if(now_enter=='a')
{
actor="S1";
j++;
nextState="1";
m_state=1;
return 2;
}
else if(now_enter=='b')
{
actor="S2";
j++;
nextState="2";
m_state=2;
return 2;
}
else if(now_enter=='c')
{
actor="S3";
j++;
nextState="3";
m_state=3;
return 2;
}
else
{
return 0;
}
}
else if(actor=="S2")
{
r_state++;
state[r_state]='2';
state[r_state+1]='\0';
r_mark++;
mark[r_mark]=record_enter;
mark[r_mark+1]='\0';
Enter[p]=' ';
p++;
if(now_enter=='a'||now_enter=='b'||now_enter=='c'||now_enter=='#')
{
actor="r6";
if(state[r_state-1]=='0')
{
nextState="5";
m_state=5;
return 1;
}
else if(state[r_state-1]=='1')
{
nextState="8";
m_state=8;
return 1;
}
else
{
return 0;
}
}
else
{
return 0;
}
}
else if(actor=="S3")
{
r_state++;
state[r_state]='3';
state[r_state+1]='\0';
r_mark++;
mark[r_mark]=record_enter;
mark[r_mark+1]='\0';
Enter[p]=' ';
p++;
if(now_enter=='a'||now_enter=='b'||now_enter=='c'||now_enter=='#')
{
actor="r4";
if(state[r_state-1]=='0')
{
nextState="4";
m_state=4;
return 1;
}
else if(state[r_state-1]=='1')
{
nextState="7";
m_state=7;
return 1;
}
else
{
return 0;
}
}
else
{
return 0;
}
}
else if(actor=="S9")
{
//AfxMessageBox(state);
r_state++;
state[r_state]='9';
state[r_state+1]='\0';
r_mark++;
mark[r_mark]=record_enter;
mark[r_mark+1]='\0';
Enter[p]=' ';
p++;
CString str;
// str.Format("%c",state[r_state-3]);
//AfxMessageBox(state);
if(now_enter=='a'||now_enter=='b'||now_enter=='c'||now_enter=='#')
{
actor="r3";
if(state[r_state-3]=='0')
{
nextState="4";
m_state=4;
return 1;
}
else if(state[r_state-3]=='1')
{
nextState="7";
m_state=7;
return 1;
}
else
{
return 0;
}
}
else
{
return 0;
}
}
else if(actor=="S10")
{
r_state++;
state[r_state]='+';
state[r_state+1]='\0';
r_mark++;
mark[r_mark]=record_enter;
mark[r_mark+1]='\0';
Enter[p]=' ';
p++;
if(now_enter=='a'||now_enter=='b'||now_enter=='c'||now_enter=='#')
{
actor="r3";
if(state[r_state-3]=='0')
{
nextState="5";
m_state=5;
return 1;
}
else if(state[r_state-3]=='1')
{
nextState="7";
m_state=7;
return 1;
}
else
{
return 0;
}
}
else
{
return 0;
}
}
else if(actor=="r1")
{
state[r_state]='\0';
strcat(state,nextState);
mark[r_mark]='Z';
int c=second();
return c;
}
else if(actor=="r2")
{
state[r_state]='\0';
strcat(state,nextState);
mark[r_mark]='Z';
int c=second();
return c;
}
else if(actor=="r3")
{
state[r_state-2]='\0';
r_state=r_state-2;
strcat(state,nextState);
mark[r_mark-2]='A';
r_mark=r_mark-2;
mark[r_mark-1]='\0';
int c=second();
return c;
}
else if(actor=="r4")
{
state[r_state]='\0';
strcat(state,nextState);
mark[r_mark]='A';
int c=second();
return c;
}
else if(actor=="r5")
{
state[r_state-2]='\0';
r_state=r_state-2;
strcat(state,nextState);
mark[r_mark-2]='B';
r_mark=r_mark-2;
mark[r_mark-1]='\0';
int c=second();
return c;
}
else if(actor=="r6")
{
state[r_state]='\0';
strcat(state,nextState);
mark[r_mark]='B';
int c=second();
return c;
}
return 0;
}
int CMyDlg::second()
{
if(m_state==4)
{
if(now_enter=='a'||now_enter=='b'||now_enter=='c'||now_enter=='#')
{
actor="r1";
if(state[r_state-1]=='0')
{
nextState="6";
m_state=6;
return 1;
}
else
{
return 0;
}
}
else
{
return 0;
}
}
else if(m_state==5)
{
if(now_enter=='a'||now_enter=='b'||now_enter=='c'||now_enter=='#')
{
actor="r2";
if(state[r_state-1]=='0')
{
nextState="6";
m_state=6;
return 1;
}
else
{
return 0;
}
}
else
{
return 0;
}
}
else if(m_state==6)
{
if(now_enter=='#')
{
actor="acc";
strcpy(state,"#Z");
strcpy(mark,"#");
return 4;//成功
}
else
{
return 0;
}
}
else if(m_state==7)
{
if(now_enter=='b')
{
actor="S9";
j++;
nextState="9";
m_state=9;
return 2;
}
else
{
return 0;
}
}
else if(m_state==8)
{
if(now_enter=='b')
{
actor="S10";
j++;
nextState="+";
m_state=10;
return 2;
}
else
{
return 0;
}
}
return 0;
}
void CMyDlg::OnBUTTONactor ()
{
CDSHOW show;
show.DoModal();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -