Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Random Tips on Parse::RecDescent

by davistar (Initiate)
on Feb 02, 2006 at 19:39 UTC ( #527410=note: print w/ replies, xml ) Need Help??


in reply to Random Tips on Parse::RecDescent

hsm,
Thanks for the excellent tips on PRD! I'm trying to use tip #16 for C-like multiline comments '/*' and '*/'.
I don't quite understand your syntax for the following production:

xcomment: <skip: qr/[ \t]*/> newline(0..) '<!--' { ($text,$return) = main::parse_delimited($text,'<!--','- ++->'); $return = ['xcomment',$return]; }
I translated your xcomment production into the following production:
comment: <skip: qr/[ \t]*/> newline(0..) '/*' { ($text,$return) = main::parse_delimited($text,'/*','*/'); $return = ['comment',$return]; }
My question is I don't understand the newline(0..) part I must missing something obvious. I had errors if I used newline(0..) and main::parse_delimited so I removed newline(0..) and changed main:: to $thisparser-> and ran the following test with no compile errors but it fails to match the comment production. What am I missing? Any help debugging would be appreciated!
#!/usr/bin/perl -w use strict; use Parse::RecDescent; use Text::DelimMatch; $::RD_ERRORS = 1; $::RD_WARN = 1; $::RD_HINT = 1; $::RD_TRACE = 1; my $grammar = q{ { sub parse_delimited { my $text = shift; my $startdelim = shift; my $enddelim = shift; my $mc = new Text::DelimMatch( $startdelim, $enddelim ); my ( $p, $m, $r ) = $mc->match( $text ); if ($p) { $text = $p; } else { $text = ""; } $text .= $r if ($r); $m =~ s/^$startdelim//; $m =~ s/$enddelim$//; return $text, $m; } } file: line(s) eofile { use Data::Dumper 'Dumper'; print Dumper @item} line: comment | <error> eofile: /^\Z/ comment: <skip: qr/[ \t]*/> '/*' { ($text,$return) = $thisparser->parse_delimited($text, '/*' +, '*/'); $return = ['comment',$return]; } }; my $parser = new Parse::RecDescent($grammar) or die "Bad grammar!\n"; while (<DATA>) { chomp; print "$_...\n"; defined($parser->file($_)) or print "Bad text!\n"; } __DATA__ /*Hello World */
I also get the following errors in the trace which are disconcerting but I don't think it's causing the problem:
Argument "/*" isn't numeric in addition (+) at C:/Perl/site/lib/Parse/ +RecDescent.pm line 2783, <DATA> line 1. Use of uninitialized value in substitution (s///) at (eval 15)[C:/Perl +/site/lib/Parse/RecDescent.pm:2618] line 22, <DATA> line 1. Use of uninitialized value in concatenation (.) or string at (eval 15) +[C:/Perl/site/lib/Parse/RecDescent.pm:2618] line 23, <DATA> line 1. Use of uninitialized value in substitution (s///) at (eval 15)[C:/Perl +/site/lib/Parse/RecDescent.pm:2618] line 23, <DATA> line 1. Use of uninitialized value in substitution (s///) at (eval 15)[C:/Perl +/site/lib/Parse/RecDescent.pm:2618] line 23, <DATA> line 1.


Comment on Re: Random Tips on Parse::RecDescent
Select or Download Code
Replies are listed 'Best First'.
Re^2: Random Tips on Parse::RecDescent
by Anonymous Monk on Apr 27, 2012 at 10:53 UTC

    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

Re^2: Random Tips on Parse::RecDescent
by hsmyers (Canon) on Feb 03, 2006 at 02:03 UTC
    I'll take a look as soon as I blow the dust off of the section of my brain that once retained this stuff!! Please do not hold your breath while I am doing so, but know that I am in fact checking it out...

    --hsm

    "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
      hsm,
      I realize this is an old post and I won't hold my breath ;^)
      Please don't spend too much time on it I just was hoping it might ring some bells. I will also continue to investigate.
      Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://527410]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2015-07-08 08:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (98 votes), past polls