Thrillofit123
Thrillofit123

Reputation: 205

Abstract Syntax Tree - Rookie level (Trying to understand)

So I'm trying to learn the AST for the moment and been practicing with some simple functions like

var a;
var b;
b = 666
a = b;
print a;

which ended like this: Simple

so after been trying to understand it. I pretty much starting to understand it but the issue is coming now when i'm trying to do a function like this:

var a;
var b;
b = 666;
a = b+42*6;
print a;

Where i'm getting trouble is that a=b+42*6 where I don't know how I should make the tree of it. I do know what to do if its only one of them like + or * but not when it's all together. I have been trying to look into youtube clips, tutorials from other sites and so on but couldn't find any similar like that.

So I would need your help guys!

Anyways. I also have a grammar but it would be looking something like in the picture:)

EDIT:

Like this

I ended up like this. It doesn't feel right...?

EDIT 2:

enter image description here

Upvotes: 1

Views: 692

Answers (1)

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31193

Since the expression is a = b + 42 * 6 you have to start by thinking how you would do it yourself. The first part is 42 * 6 based on mathematical rules, so you write that as an expression (using the same terminology you used):

    expr
   /  | \
  42  *  6

The result of this will be added to b, so that's the next one:

    expr
   /  | \
   b  +  \
        expr
       /  | \
      42  *  6

And then you want to assign it all to a, so add that:

  assign
 /  |   \
a   =    \
        expr
       /  | \
       b  +  \
            expr
           /  | \
          42  *  6

Upvotes: 2

Related Questions