in reply to RecDescent help needed!

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

Replies are listed 'Best First'.
Re^2: RecDescent help needed!
by ikegami (Patriarch) on Feb 09, 2006 at 23:45 UTC

    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: ...
Re^2: RecDescent help needed!
by pklv (Initiate) on Feb 10, 2006 at 02:44 UTC
    Thank you blokhead! I was scraping together bits of knowledge from other examples but your code really helped!