In case anyone wants to know what it would look like, a Parse::RecDescent parser would be something like this:

#!/usr/bin/perl -w use strict; use Parse::RecDescent; sub Parse::RecDescent::dispatch { my $self = shift(); my ($key, $value) = @_; print "$key: "; if ( ref($value) eq 'ARRAY' ) { print join(", ", @$value); } else { print $value; } print "\n"; } my $parser = new Parse::RecDescent << '__GRAMMAR__'; Pil: Title '{' NameTable Canvas Object(s) '}' /\Z/ Title: /^pageoutput-[^;]+;/ # the name-table NameTable: 'name-table' '{' NTLine(s) '}' NTLine: FileName FileType 'unix-filename' FileName FileName: '"' /[^"]+/ '"' FileType: 'atex-itf' | 'eps' # the canvas Canvas: 'canvas' '{' Data '}' { print "============================= CANVAS\n"; } # the objects Object: 'object' '{' Data '}' { print "============================= OBJECT\n"; } Data: KeyValue(s) KeyValue: Key Value { $thisparser->dispatch( $item[1], $item[2] ); } Key: /^\S+/ Value: '"' /[^"]+/ '"' { $item[2]; } | Number | '{' Array '}' { $item[2]; } Array: Number(s) | KeyValue(s) Number: /\d+/ __GRAMMAR__ undef $/; $parser->Pil(<STDIN>);

This is incomplete. What is left to do is to associate the various key/value pairs with their respective owners (either a 'canvas' or 'object'). If you try it out, note that an object's key/value pairs are printed before that object.


In reply to Re: Parsing a multiline data structure by eg
in thread Parsing a multiline data structure by HamNRye

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.