I am building a new parser for CPAN::Forum with Parse::RecDescent that I have just started to play with.Here is a problem I encountered: I'd like to convert
<xxx> $x < $y </xxx>
to
<div class="code"> $x &lt; $y </div>

I came up with the following grammar:

my $grammar = q { entry: chunk(s) eodata { $item[1] } chunk: text | code text: m{[\w ]+} { qq(<div class="text">$item[1]</div>); } code: opencode codetext closecode {$item[2] } opencode: m{<xxx>} closecode: m{</xxx>} codetext: m{[\w <\$]+(?=</xxx>)} { qq(<div class="code">) . CGI::e +scapeHTML($item[1]) . qq(</div>); } eodata: m{^\Z} };
I wonder if I could solve this problem without that lookahead in the codetext. I mean I already said in the closecode what should be the end of the code. Or should I drop altogether the closecode tag ?

BTW it is not really xxx there but I could not keep the real word while posting on PerlMonks. Extra XP for those who can guess what do I have instead of xxx ;-)

Update:

In the end the text parts will be able to contain some HTML markup and some special tags while the code parts will contain anything that can resemble some software source code. See the discussion about Web forum markup language and the Monastery

The exact nature of the special tags is not yet clear to me, though I am thinking about <<special_tag>>


In reply to Parse::RecDescent with lookahead or without ? by szabgab

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.