An Exercise: Bigger Doubts
Here is the trace for x + y + z:
| Parse Stack | Input | Semantic Stack |
|---|---|---|
$ |
x + y + z $ |
|
$E |
x + y + z $ |
|
$E'T |
x + y + z $ |
|
$E'T'F |
x + y + z $ |
|
$E'T' m-id id |
x + y + z $ |
|
$E'T' m-id |
+ y + z $ |
|
$E'T' |
+ y + z $ |
[x] |
$E' |
+ y + z $ |
[x] |
$E' m-+ T + |
+ y + z $ |
[x] |
$E' m-+ T |
y + z $ |
[x] |
$E' m-+ T'F |
y + z $ |
[x] |
$E' m-+ T' m-id id |
y + z $ |
[x] |
$E' m-+ T' m-id |
+ z $ |
[x] |
$E' m-+ T' |
+ z $ |
[y] [x] |
$E' m-+ |
+ z $ |
[[y] [x] |
$E' |
+ z $ |
[[x] + [y]] |
$E' m-+ T + |
+ z $ |
[[x] + [y]] |
$E' m-+ T |
z $ |
[[x] + [y]] |
$E' m-+ T'F |
z $ |
[[x] + [y]] |
$E' m-+ T' m-id id |
z $ |
[[x] + [y]] |
$E' m-+ T' m-id |
$ |
[[x] + [y]] |
$E' m-+ T' |
$ |
[z] [[x] + [y]] |
$E' m-+ |
$ |
[z] [[x] + [y]] |
$E' |
$ |
[ [[x] + [y]] + [z] ] |
$ |
$ |
[ [[x] + [y]] + [z] ] |
[ [[x] + [y]] + [z] ] |
I have highlighted in
aquamarine
the two rows at the critical point in the derivation.
The parser sees the [T',+] pair and chooses
the T' := ε rule. This ensures that the
first make-+ semantic action is executed before
the second - causes an expansion with the
E' := -TE' rule — and thus before the second
make-+ semantic action is pushed on to the stack. The
left subtraction node is constructed first, and the algorithm
gives the desired order of precedence.
I feel a lot better now.