Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to ignore newline in Parse::RecDescent

Reply
Thread Tools

How to ignore newline in Parse::RecDescent

 
 
freesoft12@gmail.com
Guest
Posts: n/a
 
      04-24-2010
Hi,

I am new to Parse::RecDescent and I came across this problem of the
parser not being able to ignore the newline character. Is there a way
I can ask the parser to ignore all newlines?

Regards
John


### parser_test.pl ###
eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}' && eval 'exec
perl -w -S $0 $argv:q' if 0;
#
use strict;
use diagnostics;
use Parse::RecDescent;

my $grammar = q {

start: identifier(s)

identifier : /\S+/
{ print $item[0]."\n"; }
};
my $parser = Parse::RecDescent->new($grammar);

open(IN,"data.txt") or die "Cannot open data.txt";
# slurp all the lines
my @lines = <IN>;
defined $parser->start(@lines) or die "Didn't match anything";
### end of parser

# data.txt
Head node1
Tail node2
 
Reply With Quote
 
 
 
 
Peter J. Holzer
Guest
Posts: n/a
 
      04-24-2010
On 2010-04-24 01:51, <> wrote:
> I am new to Parse::RecDescent and I came across this problem of the
> parser not being able to ignore the newline character. Is there a way
> I can ask the parser to ignore all newlines?

[...]
> open(IN,"data.txt") or die "Cannot open data.txt";
> # slurp all the lines
> my @lines = <IN>;
> defined $parser->start(@lines) or die "Didn't match anything";


You are calling $parser->start with two arguments here: "Head node1\n"
and "Tail node2\n".

AFAICS this isn't allowed. (But the error message is strange - I guess
Parse::RecDescent uses a second parameter for internal and undocumented
purposes.

So I think your question is really: How can I read in an entire file all
at once?

This is a FAQ. read

perldoc -q 'entire file'

hp

 
Reply With Quote
 
 
 
 
freesoft12@gmail.com
Guest
Posts: n/a
 
      04-24-2010
Thanks for the pointer to the perldoc section! I tried this suggestion
in that section and I did not get the newline problem:

$var = do { local $/; <INPUT> };

My updated prog is:
my $grammar = q {

start: identifier(s)

identifier : /\S+/
{ print $item[1]."\n"; }
};
my $parser = Parse::RecDescent->new($grammar);

open(IN,"data.txt") or die "Cannot open data.txt";
# slurp all the lines
my $var = do { local $/; <IN> };
#my @lines = <IN>;
defined $parser->start($var) or die "Didn't match anything";

It now prints:
Head
node1
Tail
node2

Like I wanted it to print.

Regards
John

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      04-24-2010
>>>>> "fc" == freesoft12@gmail com <> writes:

fc> Thanks for the pointer to the perldoc section! I tried this suggestion
fc> in that section and I did not get the newline problem:

fc> $var = do { local $/; <INPUT> };

use File::Slurp. cleaner and faster. especially since you are using a
slower parser.

uri

--
Uri Guttman ------ -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
C.DeRykus
Guest
Posts: n/a
 
      04-24-2010
On Apr 24, 8:53*am, "freesof...@gmail.com" <freesof...@gmail.com>
wrote:
> Thanks for the pointer to the perldoc section! I tried this suggestion
> in that section and I did not get the newline problem:
>
> $var = do { local $/; <INPUT> };
>
> My updated prog is:
> my $grammar = q {
>
> start: identifier(s)
>
> identifier : /\S+/
> * * * * * * { print $item[1]."\n"; }};


>
> my $parser = Parse::RecDescent->new($grammar);
>
> open(IN,"data.txt") or die "Cannot open data.txt";
> # slurp all the lines
> my $var = do { local $/; <IN> };
> #my @lines = <IN>;
> defined $parser->start($var) or die "Didn't match anything";
>
> It now prints:
> Head
> node1
> Tail
> node2
>


And you don't really need to slurp:

while (<IN>)
{
defined $parser->start($_)
or die "Didn't match anything";
}

--
Charles DeRykus
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      04-24-2010
On 2010-04-24 18:45, C.DeRykus <> wrote:
> On Apr 24, 8:53*am, "freesof...@gmail.com" <freesof...@gmail.com>
> wrote:
>> Thanks for the pointer to the perldoc section! I tried this suggestion
>> in that section and I did not get the newline problem:

[...]
>> my $parser = Parse::RecDescent->new($grammar);
>>
>> open(IN,"data.txt") or die "Cannot open data.txt";
>> my $var = do { local $/; <IN> };
>> defined $parser->start($var) or die "Didn't match anything";

[...]
> And you don't really need to slurp:
>
> while (<IN>)
> {
> defined $parser->start($_)
> or die "Didn't match anything";
> }


That parses every line separately which is in general not the same as
parsing a whole file.

hp
 
Reply With Quote
 
C.DeRykus
Guest
Posts: n/a
 
      04-24-2010
On Apr 24, 12:04*pm, "Peter J. Holzer" <hjp-usen...@hjp.at> wrote:
> On 2010-04-24 18:45, C.DeRykus <dery...@gmail.com> wrote:
>
> [...]
> > And you don't really need to slurp:

>
> > while (<IN>)
> > {
> > * * * * defined $parser->start($_)
> > * * * * * * * or die "Didn't match anything";
> > }

>
> That parses every line separately which is in general not the same as
> parsing a whole file.
>


Right, a simple example like the OP's, doesn't but even
a slightly more complex grammar could. For instance, if
the grammar required a blank line too:


start: identifier(s) blank

identifier : /\S+/
{ print $item[1],"\n"; }

blank: /^$/
{ print "blank...\n"; }


Then, you'd need to slurp... I think.

--
Charles DeRykus
 
Reply With Quote
 
freesoft12@gmail.com
Guest
Posts: n/a
 
      04-29-2010

I can't slurp as my orig grammar has a hierarchy to it (meaning that
the identifiers belong to a group and that group belongs to another
etc). I only used a small example to post. I will have to slurp the
whole file rather than passing in every line.

Regards
John
 
Reply With Quote
 
freesoft12@gmail.com
Guest
Posts: n/a
 
      04-29-2010
Thanks for the tip! I will try it out.

Regards
John

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      04-29-2010
>>>>> "fc" == freesoft12@gmail com <> writes:

fc> I can't slurp as my orig grammar has a hierarchy to it (meaning that
fc> the identifiers belong to a group and that group belongs to another
fc> etc). I only used a small example to post. I will have to slurp the
fc> whole file rather than passing in every line.

that makes no sense as that is what slurping is - reading in the whole
file at one time into an array or scalar. sometimes parsing is much
easier and faster when the entire file is in a scalar. since P::RD can't
parse incrementally it does better when you slurp. and yes, slurping is
the technical term! check out File::Slurp for more.

uri

--
Uri Guttman ------ -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Regular expression that doesn't recognize newline Antonio ASP .Net 0 01-19-2005 09:23 AM
Ignore + TEST + Ignore SpooderStank Computer Support 2 04-08-2004 11:26 AM
Searching for Exact Phrase - should I ignore the ignore words? Rob Meade ASP General 6 03-01-2004 11:28 AM
expression eats newline Gerard Oberle Perl 3 08-01-2003 05:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57