Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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?

In reply to Parsing a macro language by bluetrust

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-25 07:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found