Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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/ line 2783, <DATA> line 1. Use of uninitialized value in substitution (s///) at (eval 15)[C:/Perl +/site/lib/Parse/] line 22, <DATA> line 1. Use of uninitialized value in concatenation (.) or string at (eval 15) +[C:/Perl/site/lib/Parse/] line 23, <DATA> line 1. Use of uninitialized value in substitution (s///) at (eval 15)[C:/Perl +/site/lib/Parse/] line 23, <DATA> line 1. Use of uninitialized value in substitution (s///) at (eval 15)[C:/Perl +/site/lib/Parse/] line 23, <DATA> line 1.

In reply to Re: Random Tips on Parse::RecDescent by davistar
in thread Random Tips on Parse::RecDescent by hsmyers

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (7)
    As of 2020-04-03 11:31 GMT
    Find Nodes?
      Voting Booth?
      The most amusing oxymoron is:

      Results (27 votes). Check out past polls.