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

📄 mainfrm.cpp

📁 一个简单的基于Chart的自底向上句法分析器
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "BottomUpParser.h"

#include "MainFrm.h"
#include "parsing.h"
#include "TestTreeDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern CObArray rules, edges; // 规则、线图全局变量
extern int wordNum; // 分析句法结构的同时记录句中词数
extern CString myTrace;

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_BottomUp, OnBottomUp)
	ON_COMMAND(ID_OpenRuleSet, OnOpenRuleSet)
	ON_COMMAND(ID_ShowTree, OnShowTree)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CMDIFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CMDIFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CMDIFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers


void CMainFrame::OnBottomUp() 
{// 自底向上句法分析方法
	// TODO: Add your command handler code here
	if (rules.GetSize()==0) {
		AfxMessageBox("您没有打开规则库");
		return;
	}

	CFileDialog dlg(TRUE,"","*.*",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"词性标注文件|*.pos|所有文件||");
	if(dlg.DoModal()!=IDOK)
		return;
	
	FILE *in, *out;

	in=fopen((const char *)dlg.GetPathName(),"rt");
	if(!in) {
		AfxMessageBox("无法打开输入文件");
		return;
	}

	out=fopen((const char *)(dlg.GetPathName()+".parse"),"wt");
	if(!out) {
		AfxMessageBox("无法创建输出文件");
		fclose(in);
		return;
	}

	CStdioFile inFile(in),outFile(out);
	CString line;

	while(inFile.ReadString(line)) {
		CString tmp, tmp1;
		line.TrimLeft();
		line.TrimRight();
		if (line!="") {// 不是空串,才进行分析
			
			tmp="\n\n当前输入句子:"+line;
			
			line=Parsing(line);

			if (line.Find("句子不合语法")>=0)
				AfxMessageBox(tmp+"\n句子不合语法!");
			else
				OnShowTree(); // 显示句法树

			tmp1.Format("%d",wordNum);
			tmp=tmp+"  (共 "+tmp1+" 个词)"+"\n\n";
			outFile.WriteString(tmp);
		}
		
		outFile.WriteString(line);		
		
		// 在结果文件中记录中间分析过程,2002-12-18
		if (myTrace!="" && tmp!="") {
			myTrace = "序号\t根\t起点\t终点\t成分\n" + myTrace;
			outFile.WriteString(myTrace); 
		}
	}

	inFile.Close();
	outFile.Close();
	AfxMessageBox("全部句子分析完毕");	
}

void CMainFrame::OnOpenRuleSet() 
{
	// TODO: Add your command handler code here
	CFileDialog dlg(TRUE,"txt","*.txt",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"词性标注文件|*.pos|所有文件||");
	if(dlg.DoModal()!=IDOK) {
		AfxMessageBox("您没有打开规则库");	
		return;
	}

	FILE *rf;

	rf=fopen((const char *)dlg.GetPathName(),"rt");  //打开规则文件
	if(!rf) {
		AfxMessageBox("无法打开规则库文件");
		return;
	}

	CStdioFile ruleFile(rf);
	CString line;
	CRule * r=NULL;

	for(int i=0;i<rules.GetSize();i++)
		if(rules[i]!=NULL)
			delete rules[i];
	rules.RemoveAll();

	while(ruleFile.ReadString(line)) {
		int i=line.Find(';');
		if(i<0)
			continue;
		line=line.Left(i);
		line.TrimLeft();
		r=new CRule(line);
		rules.Add(r);
	}

	ruleFile.Close();
}

void CMainFrame::OnShowTree() 
{
	// TODO: Add your command handler code here
	CTestTreeDlg myDlg;
	myDlg.DoModal();	
}

⌨️ 快捷键说明

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