Prev: FAQ 7.26 How can I find out my current or calling package?
Next: FAQ 3.13 How can I use curses with Perl?
From: Peter J. Holzer on 24 Apr 2010 15:04 On 2010-04-24 18:45, C.DeRykus <derykus(a)gmail.com> wrote: > 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: [...] >> 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
From: C.DeRykus on 24 Apr 2010 18:11 On Apr 24, 12:04 pm, "Peter J. Holzer" <hjp-usen...(a)hjp.at> wrote: > On 2010-04-24 18:45, C.DeRykus <dery...(a)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
From: freesoft12 on 29 Apr 2010 18:44 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
From: freesoft12 on 29 Apr 2010 18:45 Thanks for the tip! I will try it out. Regards John
From: Uri Guttman on 29 Apr 2010 19:42 >>>>> "fc" == freesoft12(a)gmail com <freesoft12(a)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 ------ uri(a)stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: FAQ 7.26 How can I find out my current or calling package? Next: FAQ 3.13 How can I use curses with Perl? |