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?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.