2009-12-02 58 views
0

我正在爲一種小語言編寫一個編譯器,而我的Parser類目前負責構建一個AST供以後使用。但是,遞歸表達式無法正常工作,因爲每個包含子節點的AST節點中的矢量都無法正常工作。目前我AST的頭文件是這樣的:什麼是導致解析錯誤的複製構造函數錯誤?

class AST 
{ 
public: 
    enum ASTtype {nil, fdecl, pdecl, vdecl, rd, wr, set, rdLV, setLV, exprLV, add, sub, mul, fcall, 
     divide, mod, lt, gt, lte, gte, eq, ne, aAnd, aOr, aNot, aNeg, nConst, t, f, vs, dl, loop, 
     cond, ss}; 
    enum scalarType {tNA, tINVALID, tINT, tLONG, tBOOL}; 
    AST(); 
    AST (AST const&); 
    AST (ASTtype); 
    AST (ASTtype, std::string); 
    void addChild(AST); 
    ASTtype getNodeType(); 
    std::string text; 
    ASTtype nodeType; 
    int size; 
    scalarType evalType; 
    std::vector<AST> children; 
}; 

下面是在作祟表達解析代碼:

void Parser::e(AST& parent) 
{ 
    AST expr; 
    AST::ASTtype check = AST::nil; 
    bool binOp = false; 

    switch (lookahead.type) 
    { 
     case Lexer::AND : check = AST::aAnd ; binOp = true; break; 
     case Lexer::OR  : check = AST::aOr ; binOp = true; break; 
     case Lexer::NOT : check = AST::aNot ; break; 
     case Lexer::NEG : check = AST::aNeg ; break; 
     case Lexer::PLUS : check = AST::add ; binOp = true; break; 
     case Lexer::MINUS : check = AST::sub ; binOp = true; break; 
     case Lexer::SPLAT : check = AST::mul ; binOp = true; break; 
     case Lexer::FSLASH : check = AST::divide; binOp = true; break; 
     case Lexer::MOD : check = AST::mod ; binOp = true; break; 
     case Lexer::EQ  : check = AST::eq ; binOp = true; break; 
     case Lexer::LT  : check = AST::lt ; binOp = true; break; 
     case Lexer::GT  : check = AST::gt ; binOp = true; break; 
     case Lexer::GTE : check = AST::gte ; binOp = true; break; 
     case Lexer::LTE : check = AST::lte ; binOp = true; break; 
     case Lexer::NE  : check = AST::ne ; binOp = true; break; 
    } 
    if (check != AST::nil && binOp) 
    { 
     match(lookahead.type); 
     expr = AST(check); 
     e(expr); 
     e(expr); 
    } else if (check != AST::nil && !binOp) { 
     match(lookahead.type); 
     expr = AST(check); 
    } else if (lookahead.type == Lexer::IDENT) { 

     if (symbols.resolve(lookahead.text).sym_type == symbol::FUNC) 
     { 
      expr = AST(AST::fcall, lookahead.text); 
      match(Lexer::IDENT); 
      while (lookahead.type != Lexer::BANG) 
      { 
       e(expr); 
      } 
      match(Lexer::BANG); 
     } else { 
      expr = AST(AST::exprLV); 
      lv(expr); 
     } 
    } else if (lookahead.type == Lexer::T) { 
     match(Lexer::T); //true 
     expr = AST(AST::t); 
    } else if (lookahead.type == Lexer::F) { 
     match(Lexer::F); //false 
     expr = AST(AST::f); 
    } else { 
     expr = AST(AST::nConst, lookahead.text); 
     match(Lexer::NUM); 
    } 
    parent.children.push_back(expr); 
} 

一個例子表達不工作是+ 1 + 2 + 3 4。 它應該解析爲這樣的AST:+ [1,+ [2,+ [3,4]], 但我得到這個:+ [1,+ []]

任何意見,做錯了嗎?

+0

你能否提供關於這個「複製構造函數bug」的更多細節?你是說如果你有一個'AST'在'children'中有其他'AST',它不能正確複製?爲什麼你包含'e()'代碼而不是複製構造函數,如果複製構造函數有錯誤? – 2009-12-02 00:50:30

+0

我說複製構造函數爲一個猜測。我真的不確定是什麼導致了這個問題,我想如果有人看着e()它可能會顯示我忽略的一個錯誤或對C++的基本誤解。 – iand675 2009-12-02 02:12:42

回答

0

parent.children.push_back(expr)複製表達式。因此,它叫AST::AST(AST const&)。這個錯誤肯定會導致你看到的問題。但是,如果沒有代碼,我們無法找到其中的錯誤。