Where in the first, going "to the right", on the main node, you will advance you through the program, but in the second one just following the pointer on each node will do the same.
It seems that the second will be more correct, since you do not need something like a special type of node with a potentially extremely long array of pointers for the very first node
I would almost always prefer the first approach, and I think it will be much easier for you to build your AST if you do not need to maintain a pointer to the next node.
I think that, as a rule, it is easiest to deduce all objects from a common base class, similarly to this:
abstract class Expr { } class Block : Expr { Expr[] Statements { get; set; } public Block(Expr[] statements) { ... } } class Assign : Expr { Var Variable { get; set; } Expr Expression { get; set; } public Assign(Var variable, Expr expression) { ... } } class Var : Expr { string Name { get; set; } public Variable(string name) { ... } } class Int : Expr { int Value { get; set; } public Int(int value) { ... } }
The resulting AST is as follows:
Expr program = new Block(new Expr[] { new Assign(new Var("a"), new Int(1)), new Assign(new Var("b"), new Int(2)), new Assign(new Var("c"), new Int(3)), new Assign(new Var("d"), new Int(4)), new Assign(new Var("e"), new Int(5)), });
Juliet
source share