librelist archives

« back to archive

Does precedence matter?

Does precedence matter?

From:
Joey Coyle
Date:
2012-09-22 @ 17:59
Hi,

Using 1.4

The following doesn't work, but in the :number rule, if I invert integer 
and decimal, then it does work.  The odd thing, is I have seen examples, 
like on the powerpoint presentation from the main website where they put 
integer first.  Has Parslet changed since that presentation was made?

thanks,
joey

class Mini < Parslet::Parser
 rule(:integer)        { match('[0-9]').repeat(1) }
 rule(:decimal)        { integer >> str('.') >> integer }
 rule(:number)         { integer | decimal }
 root :number
end

puts Mini.new.parse("13.2432")

Re: [ruby.parslet] Does precedence matter?

From:
Jonathan Rochkind
Date:
2012-09-24 @ 16:02
The order of alternatives in a "|" alternation construction _definitely_ 
matters, and always has, and matters for any PEG based grammar, for 
sure, part of how a PEG works. And yeah, the order of alternatives in an 
alternation construction ("|") is basically the (only) way to try and 
set 'precedence' in a PEG.

The order of the rules themselves does not (generally? I think ever?) 
make a difference in the parsing.

On 9/22/2012 1:59 PM, Joey Coyle wrote:
> Hi,
>
> Using 1.4
>
> The following doesn't work, but in the :number rule, if I invert integer
and decimal, then it does work.  The odd thing, is I have seen examples, 
like on the powerpoint presentation from the main website where they put 
integer first.  Has Parslet changed since that presentation was made?
>
> thanks,
> joey
>
> class Mini < Parslet::Parser
>   rule(:integer)        { match('[0-9]').repeat(1) }
>   rule(:decimal)        { integer >> str('.') >> integer }
>   rule(:number)         { integer | decimal }
>   root :number
> end
>
> puts Mini.new.parse("13.2432")
>

Re: Does precedence matter?

From:
Kaspar Schiess
Date:
2012-09-23 @ 11:00
Hi Joey,

Yes, precedence does matter. (to answer your subject first)

In fact, if you have an alternative between a parser that is easy to
satisfy (integer, say with the string '13') and a parser that is more
demanding (decimal, consuming '.2432' as well), the order does matter a
lot. Parslet (PEG parsers) will try alternatives from left to right
strictly, without magic. Once something can be matched, the alternative
is considered matched.

In your case, that would mean that the :number-rule always matches the
integer prefix of any decimal. No decimal can ever be matched with your
parser.

And finally: If that is on the website now, I am sure the input given is
always integer ;) Because it has never worked. It is a bug in
documentation (the worst kind of bugs). Would you care to correct it and
send me a patch?

regards,
kaspar

Re: [ruby.parslet] Re: Does precedence matter?

From:
Joey Coyle
Date:
2012-09-23 @ 11:49
Thanks Kaspar,

That makes sense. 

The error is not in the documentation, it is in the Slide Presentation 
linked on the Documentation page, by Bo Jeanes and David Pick on Slide 16.

So, no way to fix it unless they can correct the presentation.

thanks
joey

Presentations

Parslet, An Introduction introduces parslet in a few poignant slides. (Bo 
Jeanes and David Pick)

On Sep 23, 2012, at 7:00 AM, Kaspar Schiess wrote:

> Hi Joey,
> 
> Yes, precedence does matter. (to answer your subject first)
> 
> In fact, if you have an alternative between a parser that is easy to
> satisfy (integer, say with the string '13') and a parser that is more
> demanding (decimal, consuming '.2432' as well), the order does matter a
> lot. Parslet (PEG parsers) will try alternatives from left to right
> strictly, without magic. Once something can be matched, the alternative
> is considered matched.
> 
> In your case, that would mean that the :number-rule always matches the
> integer prefix of any decimal. No decimal can ever be matched with your
> parser.
> 
> And finally: If that is on the website now, I am sure the input given is
> always integer ;) Because it has never worked. It is a bug in
> documentation (the worst kind of bugs). Would you care to correct it and
> send me a patch?
> 
> regards,
> kaspar
>