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

Line by line parsing

Thread Next
From:
Nikolay Shaplov
Date:
November 26, 2011 02:53
Subject:
Line by line parsing
Message ID:
201111261452.30063.n@shaplov.ru

Hi! I have a newbie question:

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

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: ''
Finished! 
Happy!
===================

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


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About