Non-obvious effect in YACC
rwauto at cal-unix.UUCP
rwauto at cal-unix.UUCP
Thu Jun 7 04:15:22 AEST 1984
[take this, and this ...]
Don't read this if you already know how adding a semantic action
in mid-rule impacts the pseudo-variables.
I just spent some time unscrewing a problem caused by a fun little
side effect of putting a semantic action in the middle of a YACC
production. The documentation on YACC points out that you can gain
control in mid-production. For example, the rule
A : B
{ action ($1); }
C D
{ reaction ($3, $4); }
causes action to be called after B is seen. Note, however, the
numbers of the pseudo-variables in the call to reaction. One might
think that C and D would be referenced by $2 and $3. Not so. YACC
places a marker non-terminal in the production so as to gain control,
since the parser executes semantic actions on reductions. References
to pseudo-variables are converted to offsets into the value stack.
Therefore, in the above example, $2 refers to the position in the
value stack associated with with the marker non-terminal, and is
therefore utterly bogus. (m -> epsilon yields no value.)
This is implicitly stated in the documentation. They give an example
of a semantic action in the middle of a rule, and if you look at the
pseudo-variables they use, you can see the way it works. However, it
isn't very clear.
I hope this will save someone some time sometime.
--
And the Cosmic AC said, "Let there be light."
Rick Wise
CALCULON Corp.
seismo!rlgvax!cal-unix!rwauto
More information about the Comp.unix.wizards
mailing list