Hello all. I'm working on a hairy parsing problem in HTML::Template::Expr and I'm not making much progress. In case you're not familiar, HTML::Template::Expr is an add-on to HTML::Template which adds basic expression support to the templating language. Stuff like:

<tmpl_if expr="color eq 'blue'">BLUE</tmpl_if> <tmpl_var expr="my_func(color, 'foo')"> <tmpl_if expr="(color eq 'blue') || (color eq 'red')">

All the above expressions (and a lot more) work just fine. I parse them with a Parse::RecDescent grammar which produces a tree. Executing the tree is a piece of cake.

Now I want to support:

<tmpl_if expr="(color eq 'blue') || (color eq 'red') || (color eq 'b +lack')">

But that won't parse with my grammar. The way I have it setup each || requires exactly two items and must be enclosed in parens unless it's on the outer-most scope. The outer-most scope is special because my code will add an enclosing () if none is present. To do three conditions this is required:

<tmpl_if expr="((color eq 'blue') || (color eq 'red')) || (color eq +'black')">

I've been hacking on the grammar all morning but nothing's working. I'm sure it's got something to do with leftop but what exactly is beyond me. If anyone could help I'd be greatly appreciative!

Here's the grammar, which you can also find in the latest version of the module:

expression : subexpression /^\$/ { \$return = \$item[1]; } subexpression : binary_op { \$item[1] } | function_call { \$item[1] } | var { \$item[1] } | literal { \$item[1] } | '(' subexpression ')' { \$item[2] } | <error> binary_op : '(' subexpression op subexpression ')' { [ \$item[3][0], \$item[3][1], \$item[2], \$item[4] ] + } op : />=?|<=?|!=|==/ { [ ${\BIN_OP}, \$item[1] ] } | /le|ge|eq|ne|lt|gt/ { [ ${\BIN_OP}, \$item[1] ] } | /\\|\\||or|&&|and/ { [ ${\BIN_OP}, \$item[1] ] } | /[-+*\\/\%]/ { [ ${\BIN_OP}, \$item[1] ] } function_call : function_name '(' args ')' { [ ${\FUNCTION_CALL}, \$item[1], \$item[3] ] } | function_name ...'(' subexpression { [ ${\FUNCTION_CALL}, \$item[1], [ \$item[3] ] ] } | function_name '(' ')' { [ ${\FUNCTION_CALL}, \$item[1] ] } function_name : /[A-Za-z_][A-Za-z0-9_]*/ { \$item[1] } args : <leftop: subexpression ',' subexpression> var : /[A-Za-z_][A-Za-z0-9_]*/ { \\\$item[1] } literal : /-?\\d*\\.\\d+/ { \$item[1] } | /-?\\d+/ { \$item[1] } | <perl_quotelike> { \$item[1][2] }

Thanks!
-sam

PS: I'd also be interested in a way to remove the need for parens entirely ("n == 10 || n == 50") which seems like it could be the same problem...


In reply to Left-associative binary operators in Parse::RecDescent by samtregar

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



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