[comp.lang.c] why does yacc use array[-n]?

eric@snark.UUCP (Eric S. Raymond) (09/29/88)

In article <227@olive.athertn.atherton.com>, joshua@atherton.com (Sleaze Hack) writes:
> >case 28:
> ># line 160 "gram.y"
> >{ Mpc_insert_with_searchdir(yypvt[-3],yypvt[-1]);
> >                          yyval = tree(N_INSERT_DECL3);} break;
> 
> My question is: why does YACC do this? [incorrect hypothesis follows].

The answer: YACC isn't doing what you thought it was doing. It turns out (as
you'd know if you'd looked at /usr/lib/yaccpar) that yypvt is a *pointer*, 
a register pointer in fact, into the parser stack.

In fact, it seems to define the 'frame' of syntax values available for
$[0-9]-substitution in the current action. The negative indexing looks a
little weird but is perfectly legal and even portable.
-- 
      Eric S. Raymond                     (the mad mastermind of TMN-Netnews)
      UUCP: ...!{uunet,att,rutgers}!snark!eric = eric@snark.UUCP
      Post: 22 S. Warren Avenue, Malvern, PA 19355      Phone: (215)-296-5718