From: freesoft12 on
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
From: Peter J. Holzer on
On 2010-04-24 01:51, freesoft12(a)gmail.com <freesoft12(a)gmail.com> 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

From: freesoft12 on
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

From: Uri Guttman on
>>>>> "fc" == freesoft12(a)gmail com <freesoft12(a)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 ------ uri(a)stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: C.DeRykus on
On Apr 24, 8:53 am, "freesof...(a)gmail.com" <freesof...(a)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