|Keep It Simple, Stupid|
How to parse a limited grammarby clinton (Priest)
|on Jan 10, 2008 at 10:04 UTC||Need Help??|
clinton has asked for the
wisdom of the Perl Monks concerning the following question:
I'm writing a reporting module, which allows a (knowledgeable) user to specify the content and format of a report in HTML/Excel/XML/PDF etc, without having to touch the code.
For instance, this report would return a list of order_line objects - one for each row, then proceed to write out each column in the row, based on the current order_line object:
I would have a %vars hash which would contain (eg) the $lang variable and and would store the row variables $order_line, $order and $product, and a %function hash, which would contain the lookup() and currency() code refs.
The question: how should I parse the value fields?
The value fields are short, discreet strings, in a TT style. The grammar is limited, but allows for quite a lot of flexibility.
My first inclination was to use a few regexes and to string together some coderefs, the return value from the previous coderef being passed as an argument to the next coderef. This would be fine, until I hit the logical operators (&& || ?:). Suddenly, this introduced the concept of branching into the code.
It feels like it should be a small job, and so I have been loathe to load up a full parser to do it. Having read Higher Order Perl, I have considered using Dominus' HOP::Parser, but I'm wondering if I should bite the bullet and use one of the standard modules like Parse::RecDescent or Parse::Yapp.
I have no experience with any of these modules. What would you recommend to handle what (in my ignorant opinion) should be a light-weight parsing task?
UPDATE : I have now posted my parser here: A parser for a limited grammar