Experimenting with the parser

Because the abstract syntax tree is just a data structure, we can examine it directly within the Julia REPL environment. Let's just start with a simple expression: x + y:

In Julia, every expression is represented as an Expr object. We can create an Expr object by just parsing a string with the Meta.parse function.

Here, the expression object is displayed with a syntax that resembles the original source code so that it is easier to read. We can confirm that the object has the type of Expr as follows:

In order to view the abstract syntax tree, we can use the dump function to print the structure:

In Julia, every expression is represented by a head node and an array of arguments.

In this case, the head node contains just a call symbol. The args array contains the + operator and two variables, x and y. Note that everything here is a symbol—that's OK because we are examining the source code itself, which is understandably just a tree of symbols.

Since we have had so much fun here, let's try a few other expressions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.255.127