Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10 5/3/83; site cal-unix
Path: utzoo!linus!vaxine!wjh12!genrad!decvax!ittvax!dcdwest!sdcsvax!sdcrdcf!hplabs!hao!seismo!rlgvax!cal-unix!rwauto
From: rwauto@cal-unix.UUCP
Newsgroups: net.unix-wizards
Subject: Non-obvious effect in YACC
Message-ID: <167@cal-unix>
Date: Wed, 6-Jun-84 14:15:22 EDT
Article-I.D.: cal-unix.167
Posted: Wed Jun  6 14:15:22 1984
Date-Received: Sat, 9-Jun-84 00:14:10 EDT
Organization: CALCULON BC#1, Rockville, MD.
Lines: 39

[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