develooper Front page | perl.recdescent | Postings from November 2011

Line by line parsing

Thread Next
Nikolay Shaplov
November 26, 2011 02:53
Line by line parsing
Message ID:

Hi! I have a newbie question:

I'd like to parse a file line by line...  Line can be terminated by \n or by 

I do something like this:

$Parse::RecDescent::skip = '';

my $grammar = q{
  Page: Line(s) {print "Finished! \n"}
  Line: /^.*/   /\n|\Z/  {print "Line: '$item[1]'\n"}

my $parser = new Parse::RecDescent($grammar);

my $text = "aaaaa\nbbbbbb\nccccc";

if ($parser->Page($text))
  print "Happy!\n"
} else
  print "Unhapy :-(\n"

but I get an empty line at the end of the output:

Line: 'aaaaa'
Line: 'bbbbbb'
Line: 'ccccc'
Line: ''

I can not understand why it is here...  $::RD_TRACE  = 1; does not give me any 
useful information....

Can you help me by

1. explaining why does it happens

2. telling what is the most right way to do such line by line parsing? (In 
future it should parse something similar to MediaWiki markup). 

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About