Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The example in random tip #16 does not explicitly spell out that you need to :

use Text::DelimMatch;

and in the grammar definition you need a rule :

newline: "\n"

With those two things in place the code works fine for parsing multi line HTML comments.

Parsing multi line C style comments is complicated by the fact that * is a regexp character so it needs escaping. I managed to get the following code to work OK based on technique outlined in the tip. I'm sure it could be done better but I was struggling with the escaping

# Function to cope with multiline comments # Must be placed in main section of program sub parse_multilinecomment { my $text = shift; my $mc = new Text::DelimMatch( '\\/\\*', '\\*\\/' ); my ( $p, $m, $r ) = $mc->match( '/*' . $text ); if ($p) { $text = $p; } else { $text = ""; } $text .= $r if ($r); $m =~ s/^\/\*//; $m =~ s/\*\/$//; return $text, $m; }

and the grammar rules :

newline: "\n" multilinecomment: <skip: qr/[ \t]*/> newline(0..) '/*' { ($text,$return) = main::parse_multilinecomment($text); print $return . "\n"; $return = ['xcomment',$return]; }

Successfully matches the following example :

/* A multiple line /* with nested */ comment */

Hope this may help someone

Adrian


In reply to Re^2: Random Tips on Parse::RecDescent by Anonymous Monk
in thread Random Tips on Parse::RecDescent by hsmyers

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 chilling in the Monastery: (5)
As of 2024-04-19 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found