http://www.perlmonks.org?node_id=473596

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

Ahoy Perl Pirates,

I'm having trouble figuring out how to convert a macro language into a data structure for processing.

The macro script looks like this:
page p1 { question 4B { label { Do you like your pie with ice cream? } single { 1 Yes 2 No } } question 4C { label { Do you like your pie with whipped cream? } single { 1 Yes 2 No } } }
(It's for writing questionnaires.) There's an awful lot of nesting going on.

I was thinking that the exercise would be simpler if we processed the script by searching for the macro grammar patterns:
1. Block Type -- (word) (optional:word) { (nested patterns) } 2. String Type -- (string)
The in-memory data structure for processing it is all up to me, but I was thinking that it would probably look something like this in the end:
my $nestedhash = { type => page, name => p1, contains => [ <-one or more $nestedhash structures-> <-or, one or more simple scalars (for the strings)-> ], };
How on earth would you go about converting one to another?

I've looked into Parse::RecDescent and it seems to be ideal, but it's a complicated module and none of the tutorials I've looked at have an example dealing with nested grammar. If I could get it to work though, it looks like it would be easier to extend than the other solutions I had in mind involving a loop and either a regular expression for finding childless blocks -- or a floating reference that moves up and down the data structure as we process each line. (I've done those sorts of things before, and the code was always unreadable afterwards.)

What do you think, mateys?

Is there a simple solution to this?