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

I am new to RecDescent and need a little guidance. I am parsing a custom file format to convert it to XML. I have the token parsing down, but I am having trouble building the XML tree as the tokens are evaluated inside-out versus outside-in (hope that makes sense). I'm sure autotree might be able to help me, but wasn't able to get it working only referring to the man page. Any assistance would be greatly appreciated!

--------------- Input File ---------------
CAR(agg)=( ID(str)='12345' TITLE(agg)=( TITLE(text 5)='Prius' ) RATING(text 4)='Good' CAT(agg)=( LABEL(text 6)='Hybrid' REL(int)=8 ) CAT(agg)=( LABEL(text 6)='Family' REL(int)=10 ) ) CAR(agg)=( ID(str)='22222' TITLE(agg)=( TITLE(text 7)='Voyager' ) RATING(text 3)='Bad' CAT(agg)=( LABEL(text 6)='Family' REL(int)=10 ) CAT(agg)=( LABEL(text 3)='Van' REL(int)=9 ) )

--------------- Perl Script ---------------
#!/usr/bin/perl use Parse::RecDescent; $grammar = q{ startrule: root(s) root: object { print "root\n"; } object: name "(agg)=(" body(s?) closeobject { print "object <$item{name}>\n"; } name: /\w+/ body: /\s+/ | object | property { print "property $item{property}\n"; } property: name /\(\w+\s*\d*\)=/ value { "<$item{name}>$item{value}</$item{name}>" } value: value1 | value2 value1: /\w+/ value2: /'.+'/ { substr $item[1], 1, (length $item[1]) - 2 } closeobject: ")" { print "close\n"; } }; $parser = Parse::RecDescent->new($grammar); undef $/; $text = <>; print "<ROOT>\n"; $parser->startrule($text); print "</ROOT>\n";

--------------- Desired Output ---------------
<ROOT> <CAR> <ID>12345</ID> <TITLE>Prius</TITLE> <RATING>Good</RATING> <CAT> <LABEL>Hybrid</LABEL> <REL>8</REL> </CAT> <CAT> <LABEL>Family</LABEL> <REL>10</REL> </CAT> </CAR> <CAR> <ID>22222</ID> <TITLE>Voyager</TITLE> <RATING>Bad</RATING> <CAT> <LABEL>Family</LABEL> <REL>10</REL> </CAT> <CAT> <LABEL>Van</LABEL> <REL>9</REL> </CAT> </CAR> </ROOT>

Replies are listed 'Best First'.
Re: RecDescent help needed!
by blokhead (Monsignor) on Feb 09, 2006 at 23:31 UTC
    Constructing a string by printing things during the parsing phase is a really bad idea. Things don't necessarily happen in an intuitive order, and some productions may be applied but then rolled back based on the rest of the parse (especially in recursive descent parsing). Use the productions for what they were intended -- returning a semantic value. In this case, the productions should return the appropriate parts of the XML representation.
    my $p = Parse::RecDescent->new( <<'END_GRAMMAR' ); parse: item(s) { "<ROOT>" . join("", @{$item[1]}) . "</R +OOT>" } item: /\w+/ /\([^\)]+\)=/ data { "<$item[1]>$item[3]</$item[1]>" } data: aggregate | string | integer aggregate: "(" item(s) ")" { join "", @{$item[2]} } string: /'([^']*)'/ { $1 } integer: /\d+/ { $item[1] } END_GRAMMAR undef $/; print $p->parse(<DATA>);
    This ignores and does not enforce the "data types" (agg, text N, int) in your markup language. But that would be easy to fix by changing the productions to:
    item: /\w+/ "(agg)=" aggregate | /\w+/ "(text" /\d+/ ")=" string | /\w+/ "(int)=" int
    (and of course updating the semantic actions accordingly)

    Another thing to note is that in your input, there are two layers of "TITLE" tags, but your desired output has only one. You'd have to add a special-case productions to give your desired output (or post-process the XML output):

    blokhead

      I only glanced at your code, but I have two quick comments:

      1) I like how you used <<'END_GRAMMAR' instead of q{...}. Escape slashes are simpler that way.

      2) use strict and use warnings are suppiciously missing. Even if they are used in the .pl, they also need to be placed in the grammar as follows, if you wish for them to work for the "actions":

      my $p = Parse::RecDescent->new( <<'END_GRAMMAR' ); { use strict; use warnings; } parse: ...
      Thank you blokhead! I was scraping together bits of knowledge from other examples but your code really helped!