?? analysentendlg.cpp
字號:
// AnalySentenDlg.cpp : implementation file
//
#include "stdafx.h"
#include "AnalySenten.h"
#include "AnalySentenDlg.h"
#include <vector>
#include <cassert>
#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()
/////////////////////////////////////////////////////////////////////////////
// CAnalySentenDlg dialog
CAnalySentenDlg::CAnalySentenDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAnalySentenDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAnalySentenDlg)
m_sentence = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CAnalySentenDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAnalySentenDlg)
DDX_Control(pDX, IDC_LIST2, m_anaList);
DDX_Text(pDX, IDC_SENTEN, m_sentence);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAnalySentenDlg, CDialog)
//{{AFX_MSG_MAP(CAnalySentenDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_ANA_OK, OnAnaOk)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAnalySentenDlg message handlers
BOOL CAnalySentenDlg::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
DWORD dwSytle=::GetWindowLong(m_anaList.m_hWnd,GWL_STYLE);
SetWindowLong(m_anaList.m_hWnd,GWL_STYLE,dwSytle|LVS_REPORT);
DWORD ExStyle=m_anaList.GetExtendedStyle();
m_anaList.SetExtendedStyle(ExStyle|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
//網(wǎng)格類型的列表框
m_anaList.InsertColumn(0,"步驟",LVCFMT_CENTER,45);
m_anaList.InsertColumn(1,"狀態(tài)棧",LVCFMT_CENTER,75);
m_anaList.InsertColumn(2,"符號棧",LVCFMT_CENTER,75);
m_anaList.InsertColumn(3,"輸入串",LVCFMT_CENTER,60);
m_anaList.InsertColumn(4,"ACTION",LVCFMT_LEFT,90);
m_anaList.InsertColumn(5,"GOTO",LVCFMT_LEFT,80);
return TRUE; // return TRUE unless you set the focus to a control
}
void CAnalySentenDlg::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 CAnalySentenDlg::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 CAnalySentenDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CAnalySentenDlg::OnAnaOk()
{
UpdateData(TRUE);
//LR lr;
// TODO: Add your control notification handler code here
int k,loop=0;//循環(huán)變量
int i=0;
nPos=0; //定位變量
step=1;
char cbuffer;
char Vp;//非終結(jié)符號
bool bContinue,bError,bnFind;
bContinue=true;
bError=false;
bnFind=true;
m_anaList.DeleteAllItems();//滯空列表的內(nèi)容
//1首先讀入字符輸入串 壓狀態(tài)為0 符號為#
vStatus.push_back(0);//狀態(tài)從0開始
vSymbol.push_back('#');//符號從#開始
if (m_sentence.IsEmpty()||(!IsSmlChar(m_sentence)))//循環(huán)查看句子的內(nèi)容 是否有非小寫字母 有則報錯
{
MessageBox("輸入的句子錯誤,請改正!", "錯誤", MB_OK | MB_ICONSTOP);
return;
}
// while(bnFind)
// {
// bnFind=jihe.Find(m_sentence.GetAt(loop));
// loop++;
// }
// if(!bnFind)
// {
// MessageBox("輸入的句子不全由文法的終結(jié)符號組成,請改正!", "錯誤", MB_OK | MB_ICONSTOP);
// return ;
// }
//增加第一行
m_temp=m_sentence+"#";//句子放入臨時變量
while((bContinue) && (!bError))
{
assert(nPos < m_sentence.GetLength()); //異常捕捉
assert(vStatus.size() == vSymbol.size());
for(int j=0;j<vStatus.size();j++)
{
t1.Format("%d",vStatus[j]);//將vector放入字符串
}
for(j=0;j<vSymbol.size();j++)//
{
t2.Format("%c",vSymbol[j]);//將VECTOR放入字符串
}
cbuffer=m_sentence.GetAt(nPos);
//2利用action(status,symbol)返回一個字符串 判斷類型:R(規(guī)約)/S(移進)/acc(接受)/錯誤
// m_act=lr.GetAction(vStatus.back(),cbuffer);
//3這里用選擇語句來判斷是S/R 判斷規(guī)約還是移進 1個函數(shù) GoDir()返回int 向哪走 并用來壓棧
switch(m_act.GetAt(0))
{
case 'S':
m_buffer.Format("%d",step);
m_anaList.InsertItem(nPos,m_buffer);//輸入步驟
m_anaList.SetItemText(nPos,1,t1);//輸入狀態(tài)棧
m_anaList.SetItemText(nPos,2,t2);//輸入符號串
m_anaList.SetItemText(nPos,3,m_temp.Mid(nPos,m_sentence.GetLength()-1));//輸入串
m_anaList.SetItemText(nPos,4,m_act);//ACTION
m_anaList.SetItemText(nPos,5,m_goto);//GOTO
vSymbol.push_back(cbuffer);//將當前符號壓棧
//5移進的話就將symbol的下一個字符壓棧 符號串取后面N-1位 將Sn的n狀態(tài)壓棧
vStatus.push_back(GoDir(m_act));//將ACT返回的值的第2個元素--狀態(tài) 壓棧
m_temp=m_temp.Mid(nPos,m_sentence.GetLength()-1);//把當前字符從輸入串中刪去
nPos++;
break;
case 'R':
//6規(guī)約的話輸入串不變,先彈出STATUS和SYMBOL的棧頂,因為goto(status,BIGsymbol)是N,語法分析器把
//BIGsymbol和N分別壓入棧
m_buffer=GoDir(m_act);
r_temp=atoi(m_buffer); //得到產(chǎn)生式的序號
// Vp=lr.GetLeftSymbol(r_temp);//得到非終結(jié)符號
// r_dir=lr.GetGoTo(vStatus.back(),cbuffer);
m_buffer.Format("%d",nPos);m_anaList.InsertItem(nPos,m_buffer);
m_anaList.SetItemText(nPos,1,t1);
m_anaList.SetItemText(nPos,2,t2);
m_anaList.SetItemText(nPos,3,m_temp);//輸入串不變
m_anaList.SetItemText(nPos,4,m_act);//ACTION
m_buffer.Format("%d",r_dir);
m_anaList.SetItemText(nPos,5,m_buffer);//GOTO
// for(k=0;k<lr.GetRightNum(r_temp);k++)//產(chǎn)生式右邊的字符個數(shù)
// {
// vSymbol.pop_back();
// vStatus.pop_back();
// }//將規(guī)約產(chǎn)生式右邊的字符彈棧
vSymbol.push_back(Vp);
vStatus.push_back(r_dir);
//取到產(chǎn)生式對應(yīng)的左邊的非終結(jié)符并壓棧
//彈棧后,壓DIR(goto的內(nèi)容)到狀態(tài)棧
break;
//4如果不是R/S 就判斷是否ACC 是的話表示完成。
case 'a'://直到acc退出循環(huán)
if (m_sentence.GetAt(nPos) == '#')
{
m_buffer.Format("%d",step);
m_anaList.InsertItem(nPos,m_buffer);//輸入步驟
m_anaList.SetItemText(nPos,1,t1);//輸入狀態(tài)棧
m_anaList.SetItemText(nPos,2,t2);//輸入符號串
m_anaList.SetItemText(nPos,3,m_temp.Mid(nPos,m_temp.GetLength()-1));//輸入串
m_anaList.SetItemText(nPos,4,m_act);//ACTION
m_anaList.SetItemText(nPos,5,m_goto);//GOTO
bContinue=false;//正確輸出
}
else
bError=true;
break;
//錯誤,句子不是文法G的句子
default:
bError=true;//停止循環(huán)和記錄錯誤
}
step++;//步驟自加
}//7循環(huán)
if (!bError)
{
MessageBox("語句分析正確!", "結(jié)果提示", MB_OK | MB_ICONSTOP);
}
else
{
MessageBox("分析失敗,所給句子不是文法能產(chǎn)生的句子", "結(jié)果提示", MB_OK | MB_ICONSTOP);
}
}
bool CAnalySentenDlg::IsSmlChar(CString inputStr)//判斷字符串是否全為小寫字母
{
for(int i=0;i<inputStr.GetLength();i++ )
{
char oneChar=inputStr.GetAt(i);
if((oneChar<'a')||(oneChar>'z'))
return false;
}
return true;
}
int CAnalySentenDlg::GoDir(CString godir)//取action得到的字符后面的狀態(tài)或產(chǎn)生式序號
{
CString buffer1=godir.Mid(1,godir.GetLength()-1);
int buffer2;
buffer2=atoi(buffer1);
return buffer2;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -