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

Re: Line by line parsing

Thread Previous
From:
Damian Conway
Date:
November 28, 2011 08:10
Subject:
Re: Line by line parsing
Message ID:
CAATtAp4bgWWgoxN8z31vA0f_tZLYNNi=ARhuptu+aSCN_AFZWg@mail.gmail.com
Hi Nikolay,

Your grammar allows a line to be completely empty
at the end of the input, so the end sequence is:

     Try to match Line against 'ccccc'
     Match
     Input is now ''
     Try to match Line against ''
     Match
     Parser detects no change in the input and terminates.

What you actually want is to parse lines that are *separated*
by newlines. RecDescent has a convenient way of doing that:

     Rulename(s /separator/)

So you could rewrite your grammar like so:

  my $grammar = q{
    Page:
        <skip:''>
        Line(s /\n/)     {print "Finished! \n"}

    Line:
        /[^\n]*/         {print "Line: '$item[1]'\n"}
  };

Try this version and you'll get what you expected.

Note also that I used the <skip:...> directive instead of setting
the global variable. The directive version is much safer.

Damian

Thread Previous


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