<?xml version="1.0" encoding="windows-1252"?>
<node id="967587" title="Re^2: Random Tips on Parse::RecDescent" created="2012-04-27 06:53:40" updated="2012-04-27 06:53:40">
<type id="11">
note</type>
<author id="961">
Anonymous Monk</author>
<data>
<field name="doctext">
&lt;p&gt;The example in random tip #16 does not explicitly spell out that you need to :&lt;/p&gt;
&lt;code&gt;use Text::DelimMatch;&lt;/code&gt;
&lt;p&gt;and in the grammar definition you need a rule :&lt;/p&gt;
&lt;code&gt;newline: "\n"&lt;/code&gt;
&lt;p&gt;With those two things in place the code works fine for parsing multi line HTML comments.&lt;/p&gt;
&lt;p&gt;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&lt;/p&gt;
&lt;code&gt;
# 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-&gt;match( '/*' . $text );

        if ($p) {
            $text = $p;
        }
        else {
            $text = "";
        }
        $text .= $r if ($r);
        $m =~ s/^\/\*//;
        $m =~ s/\*\/$//;
        return $text, $m;
 }
&lt;/code&gt;
&lt;p&gt;and the grammar rules :&lt;/p&gt;
&lt;code&gt;
	newline: "\n"
		
	multilinecomment:
        &lt;skip: qr/[ \t]*/&gt; newline(0..) '/*'
        {
            ($text,$return) = main::parse_multilinecomment($text);
			print $return . "\n";
             $return = ['xcomment',$return];
        }	
&lt;/code&gt;
&lt;p&gt;Successfully matches the following example :&lt;/p&gt;
&lt;code&gt;
/*
	A multiple line
	/*
		with nested
	*/

	comment
	
	

*/
&lt;/code&gt;
&lt;p&gt;Hope this may help someone&lt;/p&gt;

&lt;p&gt;Adrian&lt;/p&gt;


</field>
<field name="root_node">
180778</field>
<field name="parent_node">
527410</field>
<field name="reputation">
2</field>
</data>
</node>
