This queue is for tickets about the Regexp-Grammars CPAN distribution.

Report information
The Basics
Id:
98825
Status:
open
Priority:
Low/Low

People
Owner:
Nobody in particular
Requestors:
ch_mail [...] gmx.at
Cc:
AdminCc:

BugTracker
Severity:
(no value)
Broken in:
(no value)
Fixed in:
(no value)



Subject: Perl: Regexp:Grammars
Date: Fri, 12 Sep 2014 10:39:03 +0200
To: bug-regexp-grammars@rt.cpan.org
From: "ch_mail@gmx.at" <ch_mail@gmx.at>
Hello! I am not sure its a bug or I am just to stupid to do it correctly. But I am sure you can help :-) I am trying to use RegexpGrammars for defining a grammar in Perl. With my first attempt I got 2 questions. Please have a quick look at my short code. use strict; use warnings; use Regexp::Grammars; my $gr = qr { <debug: off> <warning: off> <root> <objrule: root> ^<X=val> <O=op> <Y=val>$ <MATCH=(?{ if ($MATCH {O} eq "+") { $MATCH = $MATCH {X} + $MATCH {Y}; } elsif ($MATCH {O} eq "*") { $MATCH = $MATCH {X} * $MATCH {Y}; } elsif ($MATCH {O} eq "/") { $MATCH = $MATCH {X} / $MATCH {Y}; } print"\nCALC=$MATCH"; })> <objtoken: val> <X=([0-9]+)> <MATCH=(?{ print "\nVAL: " . $MATCH {X}; $MATCH = $MATCH {X}; })> <objtoken: op> <X=([\+\*\/])> <MATCH=(?{ print "\nOP: " . $MATCH {X}; $MATCH = $MATCH {X}; })> }xms; ########################################## my $input = "10 + 3"; if ($input =~ $gr) { foreach (keys %/) { print "\nHSH: \"$_\" = " . $/{$_}; } } The output is: VAL: 10 OP: + VAL: 3 [eos] \_____<grammar> matched '10 + 3' CALC=13 HSH: "root" = 1 HSH: "" = 10 + 3 So I think it is working correctly (CALC=13) but I am not sure about the [eos] line. I disabled debugging. And I also expected 13 to be in the result-hash but root = 1 instead of 13... Using the lastest version of the module with Perl 5.20. Thank you! Best regards, Chris
Subject: Re: [rt.cpan.org #98825] Perl: Regexp:Grammars
Date: Mon, 15 Sep 2014 12:59:00 +1000
To: bug-Regexp-Grammars@rt.cpan.org
From: Damian Conway <damian@conway.org>
Hi Chris, What you're doing is fine. The errant debugging line is a bug in the module, which I have now corrected...and am in the process of re-uploading to CPAN. Thanks for the report. Apologies for the confusion. :-) Damian
Subject: Re: [rt.cpan.org #98825] Perl: Regexp:Grammars
Date: Mon, 15 Sep 2014 09:02:10 +0200
To: bug-Regexp-Grammars@rt.cpan.org
From: "ch_mail@gmx.at" <ch_mail@gmx.at>
Thank you very much! Best regards, Chris On 2014-09-15 04:59, damian@conway.org via RT wrote:
Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=98825 > > > Hi Chris, > > What you're doing is fine. > > The errant debugging line is a bug in the module, > which I have now corrected...and am in > the process of re-uploading to CPAN. > > Thanks for the report. Apologies for the confusion. :-) > > Damian > >
Subject: Re: [rt.cpan.org #98825] Perl: Regexp:Grammars
Date: Mon, 15 Sep 2014 16:56:04 +0200
To: bug-Regexp-Grammars@rt.cpan.org
From: "ch_mail@gmx.at" <ch_mail@gmx.at>
Hi! Please let me ask one more question regarding the topic. Why is this not working? <rule: root> ^<expr>$ <rule: expr> <X=expr> OR <Y=expr> | <X=str> <token: str> [a-z0-9]+ Input would be something like "foo OR bar". Output is. =========> Trying <grammar> from position 0 foo OR bar |...Trying <root> | |...Trying <expr> | | |...Trying <X=expr> Infinite recursion in regex at xx.pl line 48. I would expect the reduction of expr to string left and right of the OR. Thanks! Best regards, chris On 2014-09-15 04:59, damian@conway.org via RT wrote:
Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=98825 > > > Hi Chris, > > What you're doing is fine. > > The errant debugging line is a bug in the module, > which I have now corrected...and am in > the process of re-uploading to CPAN. > > Thanks for the report. Apologies for the confusion. :-) > > Damian > >
Subject: Re: [rt.cpan.org #98825] Perl: Regexp:Grammars
Date: Tue, 16 Sep 2014 08:24:43 +1000
To: bug-Regexp-Grammars@rt.cpan.org
From: Damian Conway <damian@conway.org>
Hi Chris,
Show quoted text
> Why is this not working? > > <rule: root> > ^<expr>$ > > <rule: expr> > <X=expr> OR <Y=expr> > | <X=str> > > <token: str> > [a-z0-9]+
Because recursive descent grammars (and indeed modern Perl regexes in general) are not like bottom-up grammars. They are really just a way of building collections of parsing subroutines. For example, the above grammar is more-or-less equivalent to: sub root { try { match_start_of_string(); expr(); match_end_of_string(); } } sub expr { try { my $X = expr(); match_literal('OR') my $Y = expr(); } or try { my $X = str(); } } sub str { try { match_pattern('[a-z0-9]+'); } } So when the regex grammar attempts to match <expr> (i.e. to call the expr() subroutine), the very first thing that <expr> does is to try and match <expr> (i.e. to call expr() again). Which leads to the infinite recursion. There are two possible solutions. Either don't use Regexp::Grammars at all (i.e. use https://metacpan.org/release/Marpa-R2 instead), or else reconfigure the grammar so that it's no longer left-recursive: <rule: root> ^<expr>$ <rule: expr> <X=str>+ % (OR) <token: str> [a-z0-9]+ Damian PS: This RT mechanism is intended for bug reports, not for general support questions. It is preferable to email non-bug-related questions to me directly (dconway@cpan.org) but please bear in mind that, while I will do my best to help, I have only a very limited amount of spare time available to offer this kind of assistance, and these questions are often better directed to a support forum such as perlmonks.org or stackoverflow.com. I do understand that you may not have been sure whether this particular issue was a bug (although it is explicitly listed as a limitation in the module's documentation).


This service runs on Request Tracker, is sponsored by The Perl Foundation, and maintained by Best Practical Solutions.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.