IUCompilerCourse / Essentials-of-Compilation

A book about compiling Racket and Python to x86-64 assembly
1.25k stars 135 forks source link

Question about AST formats in the Racket book section 1.2 #175

Open siadat opened 1 month ago

siadat commented 1 month ago

Thanks for this great book. Just started reading the Racket book and I had a question regarding Section 1.2 (page 4). The AST examples are written in a format that is similar to the BNF grammar, and are not Racket code. Is that intentional?

For example, the AST for negative 8 is represented as this:

(Prim '- ((Int 8)))

The equivalent Racket code would be

(Prim '- (list (Int 8)))

I'm learning Racket at the same time, and I was wondering why we don't use Racket to represent the AST. Thanks!

jsiek commented 1 month ago

It looks like I forgot the single-quote in a bunch of places. That is, in Racket, '(1 2 3) is another way to write (list 1 2 3) I'll look into fixing this when I get back from vacation.

siadat commented 1 month ago

Thanks! In the grammar, (exp) represents a list with one expression, which matches the ASTs. So, if we update the ASTs we might want to consider updating the grammar as well, perhaps to something like this:

exp ::= (Prim '- (list exp))

Just an observation regarding single-quote vs list: with single-quote the expressions inside the list get quoted as well and the result may be different, e.g. (list (+ 1 2) 3) != '((+ 1 2) 3).

I am excited to continue to follow the book this week. Enjoy your vacation! :)