PerlOnTheWay has asked for the wisdom of the Perl Monks concerning the following question:

This is in the first rule of Perl:

grammar : GRAMPROG { PL_parser->expect = XSTATE; } remember stmtseq { newPROG(block_end($3,$4)); $$ = 0; }

How can `$4` work when there're only `3` elements on the right side?

Replies are listed 'Best First'.
Re: Where does $4 come from here?
by ikegami (Patriarch) on Jul 27, 2011 at 08:34 UTC

    You seem to have miscounted. There are five items in the production, and four of them occur before the one that uses $3 and $4.

    1. GRAMPROG
    2. { PL_parser->expect = XSTATE; }
    3. remember
    4. stmtseq
    5. { newPROG(block_end($3,$4)); $$ = 0; }

      Oops! I nver know code blocks should be couted as $1-9 ... Thanks @ikegami ! Cheers!

Re: Where does $4 come from here?
by Anonymous Monk on Jul 27, 2011 at 07:52 UTC
      My understanding is that:

      $1 $2 $3 supposed to refer to the 3 elements on the right of the production.

      While $$ refer to the LHS of the production. But in this production, there're only 3 elements on the right hand side, and I don't see what $4 refers to...

        But in this production, there're only 3 elements on the right hand side, and I don't see what $4 refers to...

        Ok, here is more guessing, if we're talking about GRAMPROG  remember stmtseq and

        remember: /* NULL */ /* start a full lexical scope */ { $$ = block_start(TRUE); } ; stmtseq : /* NULL */ { $$ = (OP*)NULL; } | stmtseq fullstmt { $$ = op_append_list(OP_LINESEQ, $1, $2); PL_pad_reset_pending = TRUE; if ($1 && $2) PL_hints |= HINT_BLOCK_SCOPE; } ;
        then surely $$ = op_append_list(OP_LINESEQ, $1, $2); becomes $2, $3, $4, right?

        :)