0

我正在编写一个 C 编译器,并且处于语法分析阶段。我有一个语法树节点的层次结构。我想重构代码,但我无法解决这样的依赖关系。

例如,这是一个包含所有预声明类和抽象 Node 类的标头:

#ifndef TINY_C_COMPILER_NODE_H
#define TINY_C_COMPILER_NODE_H

#include "../token.h"
#include <ostream>

class PostfixExprNode;
class IdNode;
class ConstNode;
class StringLiteralNode;
class FloatConstNode;
class IntConstNode;
class PostfixIncrementNode;
class PostfixDecrementNode;
class StructureOrUnionMemberAccessNode;
class StructureOrUnionMemberAccessByPointerNode;
class ArrayAccess;

class Node
{
public:
    virtual void Print(std::ostream &os, int depth) = 0;
};

#endif //TINY_C_COMPILER_NODE_H 

primary_expr.h:

#ifndef TINY_C_COMPILER_PRIMARY_EXPR_H
#define TINY_C_COMPILER_PRIMARY_EXPR_H

#include "node.h"

// primary-expr ::= id | constant | string-literal | (expr)

class PrimaryExprNode: public PostfixExprNode
{
public:
    void Print(std::ostream &os, int depth) override = 0;
};

class IdNode: public PrimaryExprNode
{
public:
    explicit IdNode(Token *token);
    void Print(std::ostream &os, int depth) override;
private:
    Token *token;
};

class ConstNode: public PrimaryExprNode
{
public:
    explicit ConstNode(Token *token): token(token) {}
    void Print(std::ostream &os, int depth) override = 0;
protected:
    Token *token;
};

class IntConstNode: public ConstNode
{
public:
    explicit IntConstNode(Token *token);
    void Print(std::ostream &os, int depth) override;
};

class FloatConstNode: public ConstNode
{
public:
    explicit FloatConstNode(Token *token);
    void Print(std::ostream &os, int depth) override;
};

class StringLiteralNode: public PrimaryExprNode
{
public:
    explicit StringLiteralNode(Token *token);
    void Print(std::ostream &os, int depth) override;
private:
    Token *token;
};


#endif //TINY_C_COMPILER_PRIMARY_EXPR_H

postfix_expr.h:

#ifndef TINY_C_COMPILER_POSTFIX_EXPR_H
#define TINY_C_COMPILER_POSTFIX_EXPR_H

#include "node.h"

//
//postfix-expr ::= primary-expr | postfix-expr [expr] | postfix-expr (`argument-expr-list)
//                | postfix-expr . id | postfix-expr -> id | postfix-expr ++ | postfix-expr --
//                | (type-name) {initializer-list} | (type-name) {initializer-list, }

class PostfixExprNode: public Node
{
public:
    virtual void Print(std::ostream &os, int depth) = 0;
};

class PostfixIncrementNode: public PostfixExprNode
{
public:
    explicit PostfixIncrementNode(PostfixExprNode *node): node(node) {}
    virtual void Print(std::ostream &os, int depth);
private:
    PostfixExprNode *node;
};

class PostfixDecrementNode: public PostfixExprNode
{
public:
    explicit PostfixDecrementNode(PostfixExprNode *node): node(node) {}
    virtual void Print(std::ostream &os, int depth);
private:
    PostfixExprNode *node;
};


class StructureOrUnionMemberAccessNode: public PostfixExprNode
{
public:
    StructureOrUnionMemberAccessNode(PostfixExprNode *structureOrUnion, IdNode *member): member(member),
                                                                                         structureOrUnion(structureOrUnion) {}

    void Print(std::ostream &os, int depth) override;
private:
    PostfixExprNode *structureOrUnion;
    IdNode *member;
};

class StructureOrUnionMemberAccessByPointerNode: public PostfixExprNode
{
public:
    StructureOrUnionMemberAccessByPointerNode(PostfixExprNode *structureOrUnion, IdNode *member): member(member),
                                                                                                  structureOrUnion(structureOrUnion) {}

    void Print(std::ostream &os, int depth) override;
private:
    PostfixExprNode *structureOrUnion;
    IdNode *member;
};

class ArrayAccess: public PostfixExprNode
{
public:
    ArrayAccess(PostfixExprNode *left, PostfixExprNode *inBrackets): left(left), inBrackets(inBrackets) {}
    void Print(std::ostream &os, int depth) override;
private:
    PostfixExprNode *left, *inBrackets;
};

#endif //TINY_C_COMPILER_POSTFIX_EXPR_H

问题是 PrimaryExprNode 及其继承者需要完整声明 PostfixExprNode,因此需要包含 primary_expr.h。另一方面,StructureOrUnionMemberAccessNode 在 Print 函数中需要有关 IdNode 的信息。那么有没有办法解决这个问题呢?或者我只需要将所有内容都保存在一个标题中?

4

0 回答 0