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

📄 analysentendlg.cpp

📁 编译原理语句分析 对PASCAL语言的语句分析
💻 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);
	//网格类型的列表框
	m_anaList.InsertColumn(0,"步骤",LVCFMT_CENTER,45);   
    m_anaList.InsertColumn(1,"状态栈",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;//循环变量
	int i=0;
	nPos=0; //定位变量
	step=1;  
	char cbuffer;
	char Vp;//非终结符号 

	bool bContinue,bError,bnFind;
	bContinue=true;
	bError=false;
	bnFind=true;

	m_anaList.DeleteAllItems();//滞空列表的内容
	
	//1首先读入字符输入串 压状态为0 符号为#
	vStatus.push_back(0);//状态从0开始
	vSymbol.push_back('#');//符号从#开始

	if (m_sentence.IsEmpty()||(!IsSmlChar(m_sentence)))//循环查看句子的内容 是否有非小写字母 有则报错
		{
			MessageBox("输入的句子错误,请改正!", "错误", MB_OK | MB_ICONSTOP);
			return;
		}
//	while(bnFind)
//	{
//		bnFind=jihe.Find(m_sentence.GetAt(loop));
//		loop++;
//	}
//	if(!bnFind)
//	{
//		MessageBox("输入的句子不全由文法的终结符号组成,请改正!", "错误", 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(规约)/S(移进)/acc(接受)/错误
//	    m_act=lr.GetAction(vStatus.back(),cbuffer);
		//3这里用选择语句来判断是S/R 判断规约还是移进 1个函数 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);//输入状态栈
			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状态压栈
			vStatus.push_back(GoDir(m_act));//将ACT返回的值的第2个元素--状态 压栈
			m_temp=m_temp.Mid(nPos,m_sentence.GetLength()-1);//把当前字符从输入串中删去
			nPos++;
			break;
		case 'R':
			//6规约的话输入串不变,先弹出STATUS和SYMBOL的栈顶,因为goto(status,BIGsymbol)是N,语法分析器把
			//BIGsymbol和N分别压入栈
			m_buffer=GoDir(m_act);
			r_temp=atoi(m_buffer);		//得到产生式的序号
//			Vp=lr.GetLeftSymbol(r_temp);//得到非终结符号
//			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++)//产生式右边的字符个数
//			{
//				vSymbol.pop_back();
//				vStatus.pop_back();
//			}//将规约产生式右边的字符弹栈
			vSymbol.push_back(Vp);
			vStatus.push_back(r_dir);
			//取到产生式对应的左边的非终结符并压栈 
			//弹栈后,压DIR(goto的内容)到状态栈
			break;
			//4如果不是R/S 就判断是否ACC 是的话表示完成。
		case 'a'://直到acc退出循环
			if (m_sentence.GetAt(nPos) == '#')
			{
				m_buffer.Format("%d",step);
				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.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;//停止循环和记录错误
		}
		
		step++;//步骤自加
	}//7循环
	if (!bError)
	{
		MessageBox("语句分析正确!", "结果提示", MB_OK | MB_ICONSTOP);
	
	}
	else
	{
		MessageBox("分析失败,所给句子不是文法能产生的句子", "结果提示", 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得到的字符后面的状态或产生式序号
{	
		CString buffer1=godir.Mid(1,godir.GetLength()-1);
		int  buffer2;
		buffer2=atoi(buffer1);
		return buffer2;
}


⌨️ 快捷键说明

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