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

📄 compilerview.cpp

📁 这是一个词法语法分析器
💻 CPP
字号:
// compilerView.cpp : implementation of the CCompilerView class
//

#include "stdafx.h"
#include "compiler.h"

#include "compilerDoc.h"
#include "compilerView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CCompilerView

IMPLEMENT_DYNCREATE(CCompilerView, CFormView)

BEGIN_MESSAGE_MAP(CCompilerView, CFormView)
	//{{AFX_MSG_MAP(CCompilerView)
	ON_COMMAND(ID_MENU_CIFA, OnMenuCifa)
	ON_COMMAND(ID_MENU_YUFA, OnMenuYufa)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_COMMAND(ID_FILE_SAVE, OnFileSave)
	ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
	ON_COMMAND(ID_CIFA, OnCifa)
	ON_COMMAND(ID_YUFA, OnYufa)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCompilerView construction/destruction

CCompilerView::CCompilerView()
	: CFormView(CCompilerView::IDD)
{
	//{{AFX_DATA_INIT(CCompilerView)
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CCompilerView::~CCompilerView()
{
}

void CCompilerView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCompilerView)
	DDX_Control(pDX, IDC_EDIT3, m_OUT2);
	DDX_Control(pDX, IDC_EDIT2, m_OUT1);
	DDX_Control(pDX, IDC_EDIT1, m_IN);
	//}}AFX_DATA_MAP
}

BOOL CCompilerView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CCompilerView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

}

/////////////////////////////////////////////////////////////////////////////
// CCompilerView printing

BOOL CCompilerView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CCompilerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CCompilerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CCompilerView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CCompilerView diagnostics

#ifdef _DEBUG
void CCompilerView::AssertValid() const
{
	CFormView::AssertValid();
}

void CCompilerView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CCompilerDoc* CCompilerView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCompilerDoc)));
	return (CCompilerDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CCompilerView message handlers


void CCompilerView::OnFileOpen() 
{
	// TODO: Add your command handler code here
	CFileDialog fdlg(TRUE,"pl0",NULL,OFN_FILEMUSTEXIST,
		"C语言 源程序文件(*.c)|*.c|所有文件(*.*)|*.*||",this);
    fdlg.m_ofn.lpstrTitle="打开C语言源程序文件";//修饰对话框
	if (fdlg.DoModal()==IDOK)
	{
	    m_strCurFile=fdlg.GetPathName();//记录当前文件路径
		CFile f;
		f.Open(m_strCurFile,CFile::modeRead);
		char buf[30001];
		int sz=f.Read(buf,30000);
		buf[sz]=0;
		m_IN.SetWindowText(buf);
		m_IN.SetSel(0,0);
		f.Close();
		char a[255];
		::strcpy(a,"c 编译器  当前源文件:");
		::strcat(a,m_strCurFile.GetBuffer(0));
		SetWindowText(a);
		m_IN.SetModify(FALSE);
	}
}

void CCompilerView::OnFileSave() 
{
	// TODO: Add your command handler code here
	if (m_strCurFile!="")
	{
		char buf[30001];
		int sz=m_IN.GetWindowText(buf,30000);
		CFile f;
	    f.Open(m_strCurFile,CFile::modeWrite|CFile::modeCreate);
	    f.Write(buf,sz);
	    f.Close();
		char a[255];
		::strcpy(a,"C 编译器  当前源文件:");
		::strcat(a,m_strCurFile.GetBuffer(0));
		SetWindowText(a);
		m_IN.SetModify(FALSE);
	}
	else
	{
		OnFileSaveAs();
	}
}

void CCompilerView::OnFileSaveAs() 
{
	// TODO: Add your command handler code here
	CFileDialog fdlg(FALSE,"pl0",NULL,OFN_OVERWRITEPROMPT,
		"C 源程序文件(*.c)|*.c|所有文件(*.*)|*.*||",this);
	fdlg.m_ofn.lpstrTitle="保存C语言源程序文件";
	if (fdlg.DoModal()==IDOK)
	{
		m_strCurFile=fdlg.GetPathName();
		char buf[30001];
		int sz=m_IN.GetWindowText(buf,30000);
		CFile f;
	    f.Open(m_strCurFile,CFile::modeWrite|CFile::modeCreate);
	    f.Write(buf,sz);
	    f.Close();
		m_IN.SetModify(FALSE);
		char a[255];
		::strcpy(a,"C 编译器  当前源文件:");
		::strcat(a,m_strCurFile.GetBuffer(0));
		SetWindowText(a);
	}
}



void CCompilerView::OnMenuCifa() 
{
	// TODO: Add your command handler code here
	char buf[30000];//源程序缓冲区
	int sz=m_IN.GetWindowText(buf,30000);//读入缓冲区
	buf[sz]=0;//文件结束符

    ::strcpy(m_CAn.m_aSourse,buf);
	m_CAn.CiFafenxi();

	if(0==sz)
	{//源程序为空
		m_OUT1.SetWindowText("\r\n源程序输入区为空!\r\n\r\n请先编辑然后词法分析");
		return;
	}

	CifaResult ** p=m_CAn.m_tCifa;

	int s=0;
	int a=p[s]->type;    //p存储的是词法分析结果
	while ((p[s]->type)>=0)   //s,词法结果个数
		s++;
    int ErrorCount=0;
	char (*ep)[100];
	ep=m_CAn.m_tErrorMessage;//存储错误信息
	//显示输出结果
	CString str1,str2;
	str1.Format("\r\n\t词法分析结果\r\n\r\n\r\n(类型,值)单词\r\n\r\n");
	for (int i=0;i<s;i++)
	{
		if (p[i]->type>0)
		{//写入输出字符串
			str2.Format("(%d,%d)\t%s\r\n",p[i]->type,p[i]->value,p[i]->text);
			str1+=str2;//存入str1
		}
		else
		{//p[i]->type==0, 即错误标志
			ErrorCount++;
			if (p[i]->value==2 && (p[i]->text[0]>126 || p[i]->text[0]<32))
			{
				str2.Format("错误号:%d %s 【 无法显示该字符! 】\r\n",
					p[i]->value,ep[p[i]->value]);
				str1+=str2;
			}
			else
			{//当TYPE=0时,VALUE值为错误号
				str2.Format("错误号:%d %s 【 %s 】\r\n",
					p[i]->value,ep[p[i]->value],p[i]->text);
				str1+=str2;
			}
		}
	}
	str2.Format("\r\n 发现 %d 个错误!\r\n",ErrorCount);
	str1+=str2;
    m_OUT1.SetWindowText(str1);//输出词法分析结果
	m_OUT2.SetWindowText("\r\n处于词法分析中\r\n\r\n请修改词法错误!!!");
}

void CCompilerView::OnMenuYufa() 
{
	// TODO: Add your command handler code here
	
	char buf[30000];//源程序缓冲区
	int sz=m_IN.GetWindowText(buf,30000);//读入缓冲区
	buf[sz]=0;//文件结束符
    if(0==sz)
	{   //源程序为空
		m_OUT1.SetWindowText("\r\n源程序输入区为空!\r\n\r\n请先编辑然后词法分析");
		return;
	}
    ::strcpy(m_CAn.m_aSourse,buf);

	m_CAn.CiFafenxi();
    
	CifaResult ** cp=m_CAn.m_tCifa;
	int s=0;
	while ((cp[s]->type)>=0)
		s++;
	char (*ep)[100];
    ep=m_CAn.m_tErrorMessage;//存储错误信息
	for (int i=0;i<s;i++)
		if (cp[i]->type==0)//有词法错误
			if (IDOK==
				AfxMessageBox("源程序有词法错误!\r\n请改正后再进行语法分析!"))//MB_YESNO|,MB_ICONQUESTION
				return;
			//else break;

	m_CAn.YuFafenxi();

	m_OUT2.SetWindowText("");
	CString str1,str2;
	str1.Format("语法分析结果:\r\n\r\n");
	if (0==m_CAn.YuErrorNo)
	{
		str2.Format("源程序在语法上正确!\r\n");
		str1+=str2;
	}
	else
	{
		str2.Format("源程序有语法错误!!\r\n");
        str1+=str2;

		m_IN.SetSel(cp[m_CAn.ErrorAddr]->address,cp[m_CAn.ErrorAddr]->address+::strlen(cp[m_CAn.ErrorAddr]->text));

		str2.Format("\r\n错误号:%d %s\r\n",m_CAn.YuErrorNo,ep[m_CAn.YuErrorNo]);
		str1+=str2;
	   
	} 
	m_OUT2.SetWindowText(str1);
}

void CCompilerView::OnCifa() 
{
	// TODO: Add your command handler code here
	OnMenuCifa();
}

void CCompilerView::OnYufa() 
{
	// TODO: Add your command handler code here
	OnMenuYufa();
}

⌨️ 快捷键说明

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