I am writing a compiler for a small language, and my parser class is currently in charge of making AST for use. However, recursive expressions are not working correctly because the children nodes in each AST node are not working correctly. Currently, my AST header file looks like this:
class AST {public: enum ASTtype {nil, fdecl, pdecl, vdecl, rd, wr, set, rdlv, setLV, exprLV, Add, sub ,,,,,,,,,,,,, nem scalar type {TNA, Tinnavad, tint, TLOG, TB OOL}; AST (); AST (AST constant & amp;); AST (Esteppi); AST (Estept, STD :: string); Add Zero Child (AST); ASTtype getNodeType (); Std :: string text; Type ASTtype node; Integer shape; Scaler type evalType; Std :: vector & lt; AST & gt; children; };
Here is the expression parsing code that is causing the problem:
void Parser :: e (AST & parent) {AST expr; AST :: Estimate Check = AST :: Zero; Bool binOp = false; Switch (letterhead type) {Case Lexer :: End: check = AST :: A and; BinOp = true; break; Case Leaser :: or: Czech = AST :: AOR; BinOp = true; break; Case Lexer :: No: Check = AST :: Not One; break; Case Leaser :: Neg: Check = AST :: ANGG; break; Case Lexer :: Plus: Check = AST :: Joint; BinOp = true; break; Case Leaser :: MINUS: Check = AST :: All; BinOp = true; break; Case Leaser :: SPLAT: Czech = AST :: MUL; BinOp = true; break; Case Lexer :: FSLASH: Czech = AST :: Divide; BinOp = true; break; Case Leaser :: MOD: Check = AST :: MOD; BinOp = true; break; Case Lexor :: EQ: Check = AST :: EC; BinOp = true; break; Case Leaser :: LT: Czech = AST :: LT; BinOp = true; break; Case Leaser :: GT: Czech = AST :: GT; BinOp = true; break; Case Leaser :: GTE: Check = AST :: GT; BinOp = true; break; Case Leaser :: LTE: Czech = AST :: LTE; BinOp = true; break; Case Lexer :: NE: Check = AST :: Ne; BinOp = true; break; } If (check! = AST :: Zero and Binop) {match (letterhead typed); Expr = AST (check); E (expr); E (expr); } And if (check! = AST :: Zero and BinOp) {match (lookahead.type); Expr = AST (check); } And if (letterhead type == lexer :: idit) {if (symbols.resolve (lookahead.text) .sym_type == symbol :: FUNC} {expr = AST (AST :: Ficol, Lookahhead Text); Match (Lexer :: Chairman); While (lookahead.type! = Lexer :: BANG) {E (XPR); } Match (Lexer :: Bang); } And {expr = AST (AST :: Expr); Lv (expr); }} And if (lookahead.type == Lexer :: T) {match (Lexer :: T); // true expr = AST (AST :: T); } And if (lookahead.type == Lexer :: F) {match (Lexer :: F); // false expr = AST (AST :: F); } Other {expr = AST (AST :: Enconst, LookAhead.Text); Match (Lexer :: NUM); } Parent.children.push_back (expr); }
An example expression that does not work is + 1 + 2 + 3 4. It should be parsed in an AST like this: + [1, + [2, + [3, 4]], but instead I get this: + [1, + []]
Any advice on doing wrong?
guardian.backs Push_back (expr)
copies the expression Therefore, it calls the AST :: AST (AST const & amp;)
. A bug in that can certainly be the cause of the problem you see, though without the code, we can not find bugs in it.