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

📄 mainfrm.cpp

📁 做实习时的一个简单的词法分析器。适合初学的人
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "CiF.h"

#include "MainFrm.h"
#include "ZCiView.h"

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

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

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_Yuan, OnYuan)
	ON_COMMAND(ID_Ci, OnCi)
	ON_COMMAND(ID_Yu, OnYu)
	//}}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 (CFrameWnd::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( !CFrameWnd::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
{
	CFrameWnd::AssertValid();
}

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

#endif //_DEBUG

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


BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
{
	// TODO: Add your specialized code here and/or call the base class
	if (!m_wndSplitter.CreateStatic(this, 1, 2))    //创建1行2列的切分窗口
	{
		TRACE0("Failed to CreateStaticSplitter\n");
		return FALSE;
	}

	CRect rc;				//获得客户区大小
	GetClientRect(rc);
	int x=int (rc.Width()/1.8);
	int y=rc.Height()/2;

	if (!m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CZCiView), CSize(x,y), pContext))
	{
		TRACE0("Failed to create second pane\n");
		return FALSE;
	} //创建第1个视图

	if (!m_wndSplitter.CreateView(0, 1,	pContext->m_pNewViewClass, CSize(x,y), pContext))
	{
		TRACE0("Failed to create first pane\n");
		return FALSE;
	} //创建第2个视图

	m_Source=&((CEditView*)m_wndSplitter.GetPane(0,0))->GetEditCtrl();  //获得CEditView视图中的编辑框控件的指针
	m_Result=&((CEditView*)m_wndSplitter.GetPane(0,1))->GetEditCtrl();

	return 1;
	
	return CFrameWnd::OnCreateClient(lpcs, pContext);
}

void CMainFrame::OnYuan() 
{
	// TODO: Add your command handler code here
	CStdioFile file;
	CString  m_strLine,m_strSource;
	if(file.Open("Test.pl0",CFile::modeRead))//如果文件打开成功
	{
		while(file.ReadString(m_strLine))//一行一行的读源程序
		{
			m_strSource+=m_strLine;//将源程序保存起来
			m_strSource+="\r\n";
		}	
		
		m_Source->SetWindowText(m_strSource);//在左边屏幕上显示源程序
		file.Close();

	}
}

void CMainFrame::OnCi() 
{
	// TODO: Add your command handler code here
	CString strSource,str,strTemp;
	m_Source->GetWindowText(strSource);
	if(strSource.IsEmpty())//判断是否存在源程序
	{
		m_Result->SetWindowText("\r\n\r\n\r\n\r\n\r\n\r\n\r\n请在左边编辑源程序或打开一个源程序文件!\r\n\r\n然后再进行词法分析");
		return;
	}
	
	char buf[10000];
	m_Source->GetWindowText(buf,10000);   //获得源代码
    ::strcpy(m_ci.m_strS,buf);

    m_ci.CiFaC();
	
	CF * * pCiFa=m_ci.m_CResult;
    int n = m_ci.m_nNum;
	char (*err)[100];
	err=m_ci.m_Err;

	int m_nErrNum=0;//记录出错结果个数
	str.Format("\r\n\t\t词法分析结果\r\n\r\n\r\n输出格式:(行值,类型,值) 内容\r\n\r\n");
	for (int i=0;i<n;i++)
	{
		if (pCiFa[i]->type>0)
		{	//输出标识符
			if(pCiFa[i]->type == 23&&pCiFa[i]->value!=-1)
			{
				if((!isdigit(pCiFa[i]->data[1]))&&pCiFa[i]->data[1]!='\0')//不合法
				{
					strTemp.Format("(%d,%d,%d)\t\t%s",pCiFa[i]->line,pCiFa[i]->type,pCiFa[i]->value,pCiFa[i]->data);
			        str+=strTemp;
					strTemp.Format("     此变量不合法!\r\n");
					str+=strTemp;
					m_nErrNum++;
				}
				else
				{
					strTemp.Format("(%d,%d,%d)\t\t%s\r\n",pCiFa[i]->line,pCiFa[i]->type,pCiFa[i]->value,pCiFa[i]->data);
			        str+=strTemp;
				}
			}
			else
			{
		         strTemp.Format("(%d,%d,%d)\t\t%s\r\n",pCiFa[i]->line,pCiFa[i]->type,pCiFa[i]->value,pCiFa[i]->data);
			     str+=strTemp;
			}
		}
		else
		{
			m_nErrNum++;
			if (pCiFa[i]->value==23 && (pCiFa[i]->data[0]>126 || pCiFa[i]->data[0]<32))
			{
				strTemp.Format("第 %d 行  %s 【  无法显示该字符!】\r\n",pCiFa[i]->line,err[pCiFa[i]->value]);
				str+=strTemp;
			}
			else 
			{
				strTemp.Format("第 %d 行  %s 【 %s 】\r\n",pCiFa[i]->line,err[pCiFa[i]->value],pCiFa[i]->data);
				str+=strTemp;
			}
		}
	}
	if(m_nErrNum > 0)
	{
	    strTemp.Format("\r\n  本程序中共发现 %d 个错误,请更正后在进行语法分析!\r\n",m_nErrNum);
	    str+=strTemp;
	}
	else
	{
	    strTemp.Format("\r\n  本程序中没有发现错误,可以进行语法分析!\r\n",m_nErrNum);
	    str+=strTemp;
	}

	m_Result->SetWindowText(str);
	
}

void CMainFrame::OnYu() 
{
	// TODO: Add your command handler code here
	CWaitCursor cur;

	CString str,strTemp;
	m_Source->GetWindowText(strTemp);
	if(strTemp.IsEmpty())
	{
		m_Result->SetWindowText("\r\n\r\n\r\n\r\n\r\n\r\n\r\n请在左边编辑源程序或打开一个源程序文件!\r\n\r\n然后再进行语法分析");
		return;
	}

	
	char buf[30003];           //源代码缓冲区
	int sz=m_Source->GetWindowText(buf,30000);
	buf[sz]=0;
	::strcpy(m_ci.m_strS,buf);

	bool flag = m_ci.CiFaC();

	CF * * cp=m_ci.m_CResult;

 	str.Format("\r\n\t\t语法分析结果\r\n\r\n\r\n");

	CString strErr="";
    if(flag == false)
	{
        str += "词法分析有错误,请先更正再进行语法分析";
		m_Result->SetWindowText(str);
		return;
	}
	m_ci.YuFaC();   //语法分析

	m_Result->SetWindowText("");

	if (!m_ci.errFlag)
	{
		strTemp.Format("源程序语法正确!\r\n");
		str+=strTemp;
	}
	else
	{
		strTemp.Format("语法上有错误!!\r\n");
		str += strTemp;
        str += m_ci.m_Error;
	}

	m_Result->SetWindowText(str);
	
}

⌨️ 快捷键说明

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