How to ignore newline in Parse::RecDescent

Discussion in 'Perl Misc' started by freesoft12@gmail.com, Apr 24, 2010.

  1. Guest

    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
     
    , Apr 24, 2010
    #1
    1. Advertising

  2. 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
     
    Peter J. Holzer, Apr 24, 2010
    #2
    1. Advertising

  3. Guest

    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
     
    , Apr 24, 2010
    #3
  4. Uri Guttman Guest

    >>>>> "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 ---------
     
    Uri Guttman, Apr 24, 2010
    #4
  5. C.DeRykus Guest

    On Apr 24, 8:53 am, "" <>
    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
     
    C.DeRykus, Apr 24, 2010
    #5
  6. On 2010-04-24 18:45, C.DeRykus <> wrote:
    > On Apr 24, 8:53 am, "" <>
    > 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
     
    Peter J. Holzer, Apr 24, 2010
    #6
  7. C.DeRykus Guest

    On Apr 24, 12:04 pm, "Peter J. Holzer" <> wrote:
    > On 2010-04-24 18:45, C.DeRykus <> 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
     
    C.DeRykus, Apr 24, 2010
    #7
  8. Guest

    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
     
    , Apr 29, 2010
    #8
  9. Guest

    Thanks for the tip! I will try it out.

    Regards
    John
     
    , Apr 29, 2010
    #9
  10. Uri Guttman Guest

    >>>>> "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 ---------
     
    Uri Guttman, Apr 30, 2010
    #10
  11. Uri Guttman Guest

    >>>>> "TM" == Tad McClellan <> writes:

    TM> <> wrote:
    >> I can't slurp


    >> I will have to slurp



    TM> Which is it?

    either way he needs a bib! :)

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Apr 30, 2010
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jean-gert nesselbosch

    XPath1.0-grammar compatible to Parse::RecDescent

    jean-gert nesselbosch, May 12, 2006, in forum: XML
    Replies:
    0
    Views:
    407
    jean-gert nesselbosch
    May 12, 2006
  2. ccm news
    Replies:
    0
    Views:
    3,079
    ccm news
    Jan 15, 2009
  3. Lex Williams

    Alternative to Parse::RecDescent

    Lex Williams, Aug 26, 2008, in forum: Ruby
    Replies:
    4
    Views:
    198
    James Gray
    Aug 27, 2008
  4. Jon Ericson

    Huffman coding and Parse::RecDescent

    Jon Ericson, Apr 22, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    172
    Jon Ericson
    Apr 23, 2004
  5. A. Farber
    Replies:
    1
    Views:
    116
    Anno Siegel
    May 11, 2004
Loading...

Share This Page