mylexdlg.cpp

来自「我花了很长时间做的一个语法分析器」· C++ 代码 · 共 795 行 · 第 1/2 页

CPP
795
字号
	else return NULL;

	if(p>vl.length)
		return NULL;
	TreeNode* then1=new TreeNode("then");
	match(Then);
	root->list.Insert(then1);

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

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

	return root;
}
TreeNode* CMyLEXDlg::if11(TreeNode* root)
{
	if(p>vl.length)
		return root;
	if((vl.Find(p)->i)==End)
	{
		TreeNode* end=new TreeNode("end");
		match(End);
		root->list.Insert(end);
		return end;
	}
	if((vl.Find(p)->i)==Else)
	{
		TreeNode* else1=new TreeNode("else");
		match(Else);
		root->list.Insert(else1);

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

		TreeNode* end=new TreeNode("end");
		match(End);
		root->list.Insert(end);
		return end;
	}
	return NULL;
}
TreeNode* CMyLEXDlg::repeat_stmt(TreeNode* root)
{
	if(p>vl.length)
		return root;
	TreeNode* repeat=new TreeNode("repeat");
	match(Repeat);
	root->list.Insert(repeat);

	TreeNode* stmt_sequencex=new TreeNode("stmt_sequence");
	if(stmt_seqence(stmt_sequencex))root->list.Insert(stmt_sequencex);
	else return NULL;

	if((vl.Find(p)->i)==id)
	{
		TreeNode* untile=new TreeNode("untile");
		match(Until);
		root->list.Insert(untile);
	}
	else return NULL;

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

	return expx;
}
TreeNode* CMyLEXDlg::read_stmt(TreeNode* root)
{
	if(p>vl.length)
		return root;
	TreeNode* read1=new TreeNode("read");
	match(Read);
	root->list.Insert(read1);

	if((vl.Find(p)->i)==id)
	{
		TreeNode* idd=new TreeNode(vl.Find(p)->name);
		match(id);
		root->list.Insert(idd);
		return idd;
	}
	return NULL;

}
TreeNode* CMyLEXDlg::write_stmt(TreeNode* root)
{
	if(p>vl.length)
		return root;
	TreeNode* write1=new TreeNode("write");
	match(Write);
	root->list.Insert(write1);

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

	return expx;
}
TreeNode* CMyLEXDlg::assign_stmt(TreeNode* root)
{
	if(p>vl.length)
		return root;
	if((vl.Find(p)->i)==id)
	{
		TreeNode* idd=new TreeNode(vl.Find(p)->name);
		match(id);
		root->list.Insert(idd);
	}
	else return NULL;
	if(p>vl.length)
		return NULL;
	if((vl.Find(p)->i)==fuz)
	{
		TreeNode* fuzhi=new TreeNode(vl.Find(p)->name);
		match(fuz);
		root->list.Insert(fuzhi);
	}
	else return NULL;

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

	return expx;
}

TreeNode* CMyLEXDlg::exp(TreeNode* root)
{
	if(p>vl.length)
		return root;
	TreeNode* simple_expx=new TreeNode("sml_exp");
	if(simple_exp(simple_expx))root->list.Insert(simple_expx);
	else return NULL;
	if(p>vl.length)
		return NULL;
	TreeNode* exp1x=new TreeNode("exp\'");
	if(exp1(exp1x))root->list.Insert(exp1x);
	else return NULL;

	return exp1x;
}
TreeNode* CMyLEXDlg::exp1(TreeNode* root)
{
	if(p>vl.length)
		return root;
	if((vl.Find(p)->i)==equ||((vl.Find(p)->i)==sml))
	{
		TreeNode* comparison_opx=new TreeNode("comp_op");
		if(comparison_op(comparison_opx))root->list.Insert(comparison_opx);
		else return NULL;

		TreeNode* simple_expx=new TreeNode("sml_exp");
		if(simple_exp(simple_expx))root->list.Insert(simple_expx);
		else return NULL;

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


}
TreeNode* CMyLEXDlg::simple_exp(TreeNode* root)
{
	if(p>vl.length)
		return root;
	TreeNode* termx=new TreeNode("term");
	if(term(termx))root->list.Insert(termx);
	else return NULL;

	TreeNode* simple_exp1x=new TreeNode("sml_exp\'");
	if(simple_exp1(simple_exp1x))root->list.Insert(simple_exp1x);
	else return NULL;

	return simple_exp1x;
}
TreeNode* CMyLEXDlg::simple_exp1(TreeNode* root)
{
	if(p>vl.length)
		return root;
	if((vl.Find(p)->i)==plus||(vl.Find(p)->i)==dec)
	{
		TreeNode* add_opx=new TreeNode("add_op");
		if(add_op(add_opx))root->list.Insert(add_opx);
		else return NULL;

		TreeNode* termx=new TreeNode("term");
		if(term(termx))root->list.Insert(termx);
		else return NULL;

		TreeNode* simple_exp1x=new TreeNode("sml_exp\'");
		if(simple_exp1(simple_exp1x))root->list.Insert(simple_exp1x);
		else return NULL;

		return simple_exp1x;
	}
	else 
	{
		TreeNode* N=new TreeNode(" ");
		root->list.Insert(N);
		return N;
	}
}
TreeNode* CMyLEXDlg::comparison_op(TreeNode* root)
{
	if(p>vl.length)
		return root;
	if((vl.Find(p)->i)==sml)
	{
		TreeNode* small1=new TreeNode("<");
		match(sml);
		root->list.Insert(small1);
		return small1;
	}
	else if((vl.Find(p)->i)==equ)
	{
		TreeNode* equal=new TreeNode(vl.Find(p)->name);
		match(equ);
		root->list.Insert(equal);
		return equal;
	}
	return NULL;
}
TreeNode* CMyLEXDlg::term(TreeNode* root)
{
	if(p>vl.length)
		return root;
	TreeNode* pfactor=new TreeNode("factor");
	if(factor(pfactor))root->list.Insert(pfactor);
	else return NULL;

	TreeNode* pterm1=new TreeNode("term\'");
	if(term1(pterm1))root->list.Insert(pterm1);
	else return NULL;

	return pterm1;
}
TreeNode* CMyLEXDlg::add_op(TreeNode* root)
{
	if(p>vl.length)
		return root;
	if((vl.Find(p)->i)==plus)
	{
		TreeNode* addition=new TreeNode(vl.Find(p)->name);
		match(plus);
		root->list.Insert(addition);
		return addition;
	}
	else if((vl.Find(p)->i)==dec)
	{
		TreeNode* decc=new TreeNode(vl.Find(p)->name);
		match(dec);
		root->list.Insert(decc);
		return decc;
	}
	return NULL;
}
TreeNode* CMyLEXDlg::term1(TreeNode* root)
{
	if(p>vl.length)
		return root;
	if((vl.Find(p)->i)==mul||(vl.Find(p)->i)==div)
	{
		TreeNode* mulop=new TreeNode("mul_op");
		if(mul_op(mulop))root->list.Insert(mulop);
		else return NULL;

		TreeNode* pfactor=new TreeNode("factor");
		if(factor(pfactor))root->list.Insert(pfactor);
		else return NULL;

		TreeNode* pterm1=new TreeNode("term\'");
		if(term1(pterm1))root->list.Insert(pterm1);
		else return NULL;

		return pterm1;
	}
	else 
	{
		TreeNode* N=new TreeNode(" ");
		root->list.Insert(N);
		return N;
	}
}
TreeNode* CMyLEXDlg::mul_op(TreeNode* root)
{
	if(p>vl.length)
		return root;
	if((vl.Find(p)->i)==mul)
	{
		TreeNode* multip=new TreeNode(vl.Find(p)->name);
		match(mul);
		root->list.Insert(multip);
		return multip;
	}
	else if((vl.Find(p)->i)==div)
	{
		TreeNode* divc=new TreeNode(vl.Find(p)->name);
		match(div);
		root->list.Insert(divc);
		return divc;
	}
	return NULL;
}
TreeNode* CMyLEXDlg::factor(TreeNode* root)
{
	if(p>vl.length)
		return root;
	if((vl.Find(p)->i)==lef)
	{
		TreeNode* left=new TreeNode(vl.Find(p)->name);
		match(lef);
		root->list.Insert(left);

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

		if((vl.Find(p)->i)==rig)
		{
			TreeNode* right=new TreeNode(vl.Find(p)->name);
			match(rig);
			root->list.Insert(right);
		}
		else return NULL;
	}
	else if((vl.Find(p)->i)==num)
	{
		TreeNode* number=new TreeNode(vl.Find(p)->name);
		match(num);
		root->list.Insert(number);
		return number;
	}
	else if((vl.Find(p)->i)==id)
	{
		TreeNode* idi=new TreeNode(vl.Find(p)->name);
		match(id);
		root->list.Insert(idi);
		return idi;
	}
	return NULL;
}
bool CMyLEXDlg::match(int i)
{
	if(i==(vl.Find(p)->i))
	{
		p++;
		return 1;
	}
	else
		return 0;
}
void CMyLEXDlg::OnBnClickedCheck()
{
	CDlg dlg1;
	dlg1.DoModal();
}



BOOL CMyLEXDlg::DestroyWindow()
{
	if(!program)
	   delete program;
	return CDialog::DestroyWindow();
}

void CMyLEXDlg::OnCancel()
{
	
	P.empty();
	vl.empty();
	if(!program)
	   delete program;
	CDialog::OnCancel();
}

⌨️ 快捷键说明

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