in reply to Parse RecDescent Nesting (Followup)

I'm doing this quickly, as I'm rushing off to the airport for a cross-US trip, but it looks like your grammar is something like:
document: chunk(s?) /\z/ chunk: open chunk close | word(s) open: "<open>" close: "<close>" word: /\w+/
That should be enough to hang the proper return values from.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.


update:
document: stuff /\z/ { return $item{stuff} } stuff: chunk(s?) { return [map {@$_} @{$item[1]} ] } # flatten adjacen +t arrayrefs chunk: open stuff close { return $item{stuff} } | word(s?) # returns a +rrayref open: "<open>" close: "<close>" word: /\w+/

Replies are listed 'Best First'.
Re^2: Parse RecDescent Nesting (Followup)
by tphyahoo (Vicar) on Jan 21, 2005 at 15:53 UTC
    Thanks again Randal (for the update). The stuff to flatten the arrayrefs looks promising, and I am trying to integrate that into what I have.

    The updated grammar seemed dodgy to me though, because it had a circular reference -- "stuff" has "chunks", and "chunks" have "stuff". Or are circular references allowed/desirable/cool in a grammar specification? Trying to understand what this was doing made my brain swim.

    Best, thomas.

    Note: this was second reply to Randal.

      Circular references are what permits nested open-close pairs. I wrote it the way I did so that the grammar was never ambiguous, and yet permits empty open-close pairs, or empty adjacent opens or closes. Allowing "empty" can get you into trouble in a grammar. And that fixes the problem from before.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

      The "circular references", or, as they're more commonly known, "recursive definitions", are precisely what differentiates a grammar from a flat, regular pattern, and the thing Perl's regex-engine can't properly handle, making a parser like Parse::RecDescent useful.