语法分析,采用递归下降实现(本人觉得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