IUCompilerCourse / Essentials-of-Compilation

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

Is the grammar in exercise 1 correct? #35

Closed EFanZh closed 4 years ago

EFanZh commented 4 years ago

Pr #26 added an int alternation to the exp non-terminal, I don’t think it is correct.

I think I have a solution that doesn’t require the int alternation in the exp non-terminal.

Click here to view my solution. (I am not sure that whether I should put my solution here. But how should I explain myself?) The idea is that we flatten the expression tree into an addition chain. Then partition the terms in the chain into two groups: - The group that only contains *int*s. - The group that only contains `(read)`s and `(- (read))`s. Each group could be empty. Then we compute the sum of the first group, and add the second group to the result. The *exp* non-terminal corresponds to the second group in my solution, which doesn’t need *int* alternation since they are all in the first group.

Is there something I missed? Is the int alternation really necessary?

jsiek commented 4 years ago

You're correct, the int alternative is not needed in exp.