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

📄 mylexdlg.cpp

📁 我花了很长时间做的一个语法分析器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MyLEXDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "MyLEX.h"
#include "MyLEXDlg.h"
#include ".\mylexdlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

List<TreeNode*> P; 
CString content1;
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
CString def[8]={"if","then","else","end","repeat","until","read","write"};
int isDefined(CString &str)
{
	for(int i=0;i<8;i++)
	{
		if(str==def[i])
			return i+1;
	}
	return 0;
}
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

	// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

	// 实现
protected:
	DECLARE_MESSAGE_MAP()
public:
//	virtual BOOL DestroyWindow();
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CMyLEXDlg 对话框



CMyLEXDlg::CMyLEXDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyLEXDlg::IDD, pParent)
, m_p(_T(""))
, m_a(_T(""))
, num1(0)
, m_all(_T(""))
, m_s(_T(""))
, p(1)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyLEXDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT1, m_program);
	DDX_Text(pDX, IDC_EDIT1, m_p);
	DDX_Control(pDX, IDC_EDIT2, m_answer);
	DDX_Text(pDX, IDC_EDIT2, m_a);
	//DDX_Control(pDX, IDC_CHECK, m_bCtrl);
}

BEGIN_MESSAGE_MAP(CMyLEXDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_FENXI, OnBnClickedFenxi)
	ON_BN_CLICKED(IDC_CHECK, OnBnClickedCheck)
END_MESSAGE_MAP()


// CMyLEXDlg 消息处理程序

BOOL CMyLEXDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 将\“关于...\”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	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);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	//m_bCtrl.EnableWindow(1);
	return TRUE;  // 除非设置了控件的焦点,否则返回 TRUE
}

void CMyLEXDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CMyLEXDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作矩形中居中
		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;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标显示。
HCURSOR CMyLEXDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

int CMyLEXDlg::GetToken(CString & name)
{
	name="";
	char ch=m_p.GetAt(num1);
	if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
	{
		while((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
		{
			name+=ch;
			ch=m_p.GetAt(++num1);
		}
		//num1--;
		int ii=0;
		if(ii=isDefined(name))
			return ii;
		else return id;
	}
	else if(ch>='0'&&ch<='9')
	{
		while(ch>='0'&&ch<='9')
		{
			name+=ch;
			ch=m_p.GetAt(++num1);
		}
		//num1--;
		return num;
	}
	switch(ch)
	{
	case '+':name+=ch;num1++;return plus;break;
	case '-':name+=ch;num1++;return dec;break;
	case '*':name+=ch;num1++;return mul;break;
	case '/':name+=ch;num1++;return div;break;
	case '=':name+=ch;num1++;return equ;break;
	case '<':name+=ch;num1++;return plus;break;
	case '{':name+=ch;num1++;return lef;break;
	case '}':name+=ch;num1++;return rig;break;
	case ';':name+=ch;num1++;return fen;break;
	case ':':name+=ch;ch=m_p.GetAt(++num1);if(ch=='='){name+=ch;num1++;}return fuz;break;
	case' ':
	case'\t':num1++;return 1000;
	case'\r':num1++;return 1001;
	case'\n':num1++;return 1000;
	default:return 0;
	}
	return 0;
}

void CMyLEXDlg::OnBnClickedFenxi()
{
	p=1;
	P.empty();
	content1="";
	m_answer.SetWindowText("");
	UpdateData();
	content1=m_p;
	num1=0;
	int j=0;
	CString idName,temp;
	m_a="line 1:";
	int line=1;
	while(num1<m_p.GetLength())
	{
		if(j=GetToken(idName))
		{
			if(j!=1000&&j!=1001)
			{
				VEC * v=new VEC();
				v->i=j;
				v->name=idName;
				vl.Insert(v);
				temp.Format("<%d,",j);
				idName+=">";
				m_a+=temp+idName;
				idName="";
			}
			else if (j==1001) 
			{
				program=prase();
				if(!program)
				{
					CString str;
					str.Format("第%d行,第%d个字符串 \"%s\" 附近有语法错误!!",line,p,vl.Find(p)->name);
					AfxMessageBox(str);
					str="";
					vl.empty();
					p=1;
					return ;
				}
				P.Insert(program);
				vl.empty();
				p=1;
				line++;
				idName.Format("line %d:",line);
				m_a+="\r\n"+idName;	
			}
		}
		else 
		{
			idName.Format("%s处有错误",idName);
			m_a+=idName;
			break;
		}
	}
	if(vl.length)
	{
		program=prase();
		if(!program)
		{
			CString str;
			str.Format("第%d行,第%d个字符串 \"%s\" 附近有语法错误!!",line,p-1,vl.Find(p-1)->name);
			AfxMessageBox(str);
			str="";
			vl.empty();
			p=1;
			return ;
		}
		P.Insert(program);
		vl.empty();
		p=1;
	}
	m_answer.SetWindowText(m_a);

}

TreeNode* CMyLEXDlg::prase()
{
	TreeNode* root=new TreeNode("stmt_seqence");
	if(stmt_seqence(root))
		return root;
	return NULL;
}
TreeNode* CMyLEXDlg::stmt_seqence(TreeNode* root)
{
	if(p>vl.length)
		return root;
	if((vl.Find(p)->i)==If||(vl.Find(p)->i)==Repeat||(vl.Find(p)->i)==Read||(vl.Find(p)->i)==Write||(vl.Find(p)->i)==id)
	{
		TreeNode* statement1=new TreeNode("statement");
		if(statement(statement1))root->list.Insert(statement1);
		else return NULL;
		TreeNode* stmt_sequence11=new TreeNode("stmt_sequence\'");
		if(stmt_seqence1(stmt_sequence11))root->list.Insert(stmt_sequence11);
		else return NULL;
		return root;
	}
	return NULL;
}
TreeNode* CMyLEXDlg::stmt_seqence1(TreeNode* root)
{
	if((vl.Find(p)->i)==fen)
	{
		TreeNode* fen1=new TreeNode(";");
		match(fen);
		root->list.Insert(fen1);

		if(p>vl.length)
		  return root;
		TreeNode* statementx=new TreeNode("statement");
		if(statement(statementx))root->list.Insert(statementx);
		else return NULL;

		TreeNode* stmt_sequence1x=new TreeNode("stmt_sequence\'");
		if(stmt_seqence1(stmt_sequence1x))root->list.Insert(stmt_sequence1x);
		else return NULL;

		return root;
	}
	else 
	{
		TreeNode* N=new TreeNode(" ");
		root->list.Insert(N);
		return N;
	}

}
TreeNode* CMyLEXDlg::statement(TreeNode* root)
{
	if(p>vl.length)
		return root;
	else if((vl.Find(p)->i)==If)
	{
		TreeNode* if_stmtx=new TreeNode("if_stmt");
		if(if_stmt(if_stmtx)){root->list.Insert(if_stmtx);return if_stmtx;}
		else return NULL;
	}
	else if((vl.Find(p)->i)==Repeat)
	{
		TreeNode* repeat_stmtx=new TreeNode("repeat_stmt");
		if(repeat_stmt(repeat_stmtx)){root->list.Insert(repeat_stmtx);return repeat_stmtx;}
		else return NULL;
	}
	else if((vl.Find(p)->i)==Read)
	{
		TreeNode* read_stmtx=new TreeNode("read_stmt");
		if(read_stmt(read_stmtx)){root->list.Insert(read_stmtx);return read_stmtx;}
		else return NULL;
	}
	else if((vl.Find(p)->i)==Write)
	{
		TreeNode* write_stmtx=new TreeNode("write_stmt");
		if(write_stmt(write_stmtx)){root->list.Insert(write_stmtx);return write_stmtx;}
		else return NULL;
	}
	else if((vl.Find(p)->i)==id)
	{
		TreeNode* assign_stmtx=new TreeNode("assign_stmt");
		if(assign_stmt(assign_stmtx)){root->list.Insert(assign_stmtx);return assign_stmtx;}
		else return NULL;
	}
	return NULL;
}
TreeNode* CMyLEXDlg::if_stmt(TreeNode* root)
{
	if(p>vl.length)
		return root;
	TreeNode* if1=new TreeNode("if");
	match(If);
	root->list.Insert(if1);

	if(p>vl.length)
		return NULL;
	TreeNode* expx=new TreeNode("exp");
	if(exp(expx))root->list.Insert(expx);

⌨️ 快捷键说明

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