CMM语法分析(C++实现)

这篇博客探讨了如何使用递归下降方法实现CMM语法分析,主要针对C++编程。作者创建了一个树节点类来存储语法树,并提供了文法规则。尽管文法规定if-else语句必须包含花括号,但作者在尝试打印语法树时遇到了困难。博客中包含了处理不同文法元素的函数,如stmtSequence和ifStmt,以及不推荐使用的打印语法树的辅助函数。
摘要由CSDN通过智能技术生成

语法分析,采用递归下降实现(本人觉得LL(1)看着太麻烦了),使用

构造了一个树节点的类用来存储(本来没写,后来不知道怎么打印语法树就写了下,写了之后还是不清楚怎么打印语法树—_—,现在看来太蠢了,不知道那位仁兄有更好的方法赐教一下)

文法:(该文发修改自一位仁兄,现在找不到链接了,若原作者或之情人士看到请提醒我)

(该文发规定较为严格,对于if-else必须使用if(条件){语句},{}不能省略)

program->stmt-sequence
stmt-sequence->{statement}
statement->declare-stmt|assign-stmt|write-stmt|read-stmt|if-stmt|while-stmt
declare-stmt->type-specifier identifier(= exp);
assign-stmt->identifier = exp;
identifier->ID{'['INTNUM']'}
write-stmt->write exp;
read-stmt->read identifier;
if-stmt->if'('condition')''{'stmt-sequence'}'(else'{'stmt-sequence'}')
while-stmt->while'('condition')''{'stmt-sequence'}'
condition->exp comparision-op exp
comparision-op-><|>|==|<>
exp->(+|-)term{addop term}
addop->+|-
term->facter{mulop factor}
mulop->*|/
factor->INTNUM|REALNUM|'('exp')'|identifier
type-specifier->int|real

SynTree.h

#ifndef __SynTree__Test__
#define __SynTree__Test__

#include <stdio.h>
#include <string>

using namespace std;

class SynTree
{
public:
	SynTree(void);
	~SynTree(void);

	static SynTree* create();
	bool init();

	void setNBro(SynTree *brother);
	SynTree* getNBro();

	void setFChild(SynTree *child);
	SynTree* getFChild();

	void setContent(string newContent);
	string getContent();

	static SynTree* getTheHead();

private:
	SynTree *nBro;	//下一个兄弟节点
	SynTree *fChild;	//第一个子节点
	static SynTree *theHead;
	string content;
};


#endif // !__SynTree__Test__
SynTree.cpp

#include "SynTree.h"

using namespace std;

SynTree* SynTree::theHead = SynTree::create();

SynTree::SynTree()
{

}

SynTree::~SynTree()
{
	delete nBro;
	delete fChild;
	nBro = NULL;
	fChild = NULL;
}

SynTree* SynTree::create()
{
	SynTree *pRet = new SynTree();
	if(pRet && pRet->init())
	{
		return pRet;
	}
	else
	{
		delete pRet;
		pRet = NULL;
		return NULL;
	}
}

bool SynTree::init()
{
	bool bRet = false;
	do
	{
		nBro = NULL;
		fChild = NULL;
		content = "";

		bRet = true;

	}while(0);

	return bRet;
}

void SynTree::setNBro(SynTree *brother)
{
	this->nBro = brother;
}

SynTree* SynTree::getNBro()
{
	return nBro;
}

void SynTree::setFChild(SynTree *child)
{
	this->fChild = child;
}

SynTree* SynTree::getFChild()
{
	return this->fChild;
}

void SynTree::setContent(string newContent)
{
	this->content = newContent;
}

string SynTree::getContent()
{
	return this->content;
}

SynTree* SynTree::getTheHead()
{
	return theHead;
}
Task3.h

#ifndef __Task3__Test__
#define __Task3__Test__

#include <stdio.h>
#include "WordList.h"
#include <string.h>
#include "SynTree.h"

using namespace std;

class Task3
{
public:
	Task3(void);
	~Task3(void);

	void stmtSequence();	//处理程序
	void statement(SynTree *nowTree);	//处理句子
	void declareStmt(SynTree *nowTree);	//处理声明语句
	void assignStmt(SynTree *nowTree);	//处理赋值语句
	void writeStmt(SynTree *nowTree);	//处理write语句
	void readStmt(SynTree *nowTree);	//处理read语句
	void ifStmt(SynTree *nowTree);	//处理if语句
	void whileStmt(SynTree *nowTree);	//处理while语句
	void identifier(SynTree *nowTree);	//处理标识符以及标识符数组
	void exp(SynTree *nowTree);	//处理算术表达式
	void condition(SynTree *nowTree);	//处理判断式
	void comparisionOp(SynTree *nowTree);	//处理判断式符号
	void addop(SynTree *nowTree);	//处理+和-
	void term(SynTree *nowTree);	//处理算术表达式中的乘和除
	void mulop(SynTree *nowTree);	//处理*和/
	void factor(SynTree *nowTree);	//处理算术表达式中的因子
	void typeSpecifier(SynTree *nowTree);	//处理声明符号中的类型

	void printSynTree();
	void printNBro(SynTree *tree);
	void printFChild(SynTree *tree);


private:
	WordList *nowWordList;	//指向当前处理的wordlist
	bool flag;

	SynTree *stmtFlag;	//记录到哪个语句
};

#endif // !__Task3__Test__
Task3.cpp
#include "Task3.h"
#include <iostream>
#include <cctype>


using namespace std;

Task3::Task3()
{
	nowWordList = WordList::getHead();
	stmtFlag = SynTree::create();
}

Task3::~Task3()
{

}

void Task3::stmtSequence()
{

	while(nowWordList->getNext() != NULL)
	{
		if(nowWordList->getNext()->getType() == NOTE
			|| nowWordList->getNext()->getType() == NOTE_WRONG)
		{
			if(nowWordList->getNext()->getNext() != NULL)
			{
				nowWordList
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值