In my previous Meditation I point out that the grammar feature can be used for binary data, and that subsumes unpack.

Well, what about pack? More generally, if I already have a grammar that states the way something should be arranged, why can't I use that to generate output?

A trivial example is a grammar that has no backtracking possible. <int><char><float> would be similar in meaning to "icf" as a pack control string. More usfully, given a definition of what goes where and how, and a list of named things, apply one to the other and produce output. A high-level nonterminal grammar production makes a very good definition of "what goes where and how", each item in the production being a name for the data and an output rule for how to do the formatting.

I think a Perl6 pattern can be at the very least bent to this end, by generating as a side-effect and not really consuming input. But maybe some feature designs could be involved to make this a cleaner concept, not a trick.

How introspective is the Perl6 pattern definition? Instead of matching against one, can I examine it to see what it's made of? It would be elegant if the pattern object itself had a parse tree, in the same form as the ones it builds.

—John

Edit kudra, 2002-09-05 Replaced I tags in title with '


In reply to Using Perl6 patterns/grammar definition for 'output'? by John M. Dlugosz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.