Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^2: Random Tips on Parse::RecDescent

by Anonymous Monk
on Apr 27, 2012 at 10:53 UTC ( #967587=note: print w/replies, xml ) Need Help??

in reply to Re: Random Tips on Parse::RecDescent
in thread Random Tips on Parse::RecDescent

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


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://967587]
[GotToBTru]: late to the conversation .. but thanks, Corion, for the reminder about Perl::Tidy
[Corion]: GotToBTru: Heh ;) I'm not really endorsing it, as I found that any gains made through it are easily squandered by the hours spent on configuring it. At least in my case :)
[GotToBTru]: Like most Swiss Army Knives, you can cut yourself trying to find the blade you actually want
[GotToBTru]: i am satisfied with the default settings turning the seemingly random formatting into something reliable
[Corion]: GotToBTru: Yeah, I started a quick(ly implemented but slowly running) implementation of a program that would guess the "best" configuration (that is, least amount of changes) from my code, but then found that it stops parsing the source as soon as it ..
[GotToBTru]: sadly, it can't do anything about this
[Corion]: ... encounters sub foo($bar,$baz) {, which is something I like nowadays
[Corion]: GotToBTru: Yeah, that stuff is hard to do away automatically ;)

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2017-02-27 14:30 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (387 votes). Check out past polls.