pklv has asked for the wisdom of the Perl Monks concerning the following question:
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 ) )
#!/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";
<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 | |
by ikegami (Patriarch) on Feb 09, 2006 at 23:45 UTC | |
by pklv (Initiate) on Feb 10, 2006 at 02:44 UTC |