0FGVYqxHQZ has asked for the wisdom of the Perl Monks concerning the following question:

I get the following error when I try to use <ALIAS=\IDENT> in Regexp::Grammars. I can't see what is wrong with my code (str_delim is not misspelled). Could anybody let me know what is wrong?
~/linux/test/perl/library/Regexp/Grammars/</=/\$ cat main.pl #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my $grammar = do { use Regexp::Grammars; qr{ (?#<logfile:->) <delimited_string> <token: delimited_string> <ldelim=str_delim> .*? <rdelim=\ldelim> <token: str_delim> ["'`] }xms; }; my $string = "''"; if ($string =~ $grammar) { print Dumper \%/; } ~/linux/test/perl/library/Regexp/Grammars/</=/\$ ./main.pl error | Found call to <ldelim=str_delim>, but no <rule: str_delim> or | <token: str_delim> was defined in the grammar | (Did you misspell the rule name or forget to define the rule? +) | error | Found call to <______0_88_str_delim>, but no <rule: ______0_8 +8_str_delim> or | <token: ______0_88_str_delim> was defined in the grammar | (Did you misspell the rule name or forget to define the rule? +) | error | Found call to <str_delim>, but no <rule: str_delim> or | <token: str_delim> was defined in the grammar | (Did you misspell the rule name or forget to define the rule? +) | warn | Possible invalid subrule call: | <ldelim=str_delim> | (To silence this warning, use: \<ldelim=str_delim> | warn | Possible invalid subrule call: | <rdelim=(?{; *Regexp::Grammars::LOGFILE | (To silence this warning, use: \<rdelim=(?{; *Regexp::Grammar +s::LOGFILE | warn | Possible invalid subrule call: | <______0_88_str_delim> | (To silence this warning, use: \<______0_88_str_delim> | warn | Possible invalid subrule call: | <str_delim> | (To silence this warning, use: \<str_delim> | Eval-group not allowed at runtime, use re 'eval' in regex m/(?{; *Rege +xp::Grammars::LOGFILE = Regexp::Grammars::_open_log('>>','-'); })((? +{; @! = () if !.../ at ./main.pl line 10.

Replies are listed 'Best First'.
Re: The usage of <ALIAS=\IDENT> of Regexp::Grammars
by DamianConway (Beadle) on Aug 16, 2012 at 04:12 UTC

    The bug is that Perl does not always pass the complete raw regex into an overload 'qr', so the module cannot always convert it correctly. :-(

    In this case, Perl intercepts the \l of the \ldelim and treats it as a "convert the following to lowercase" escape, before Regexp::Grammars ever gets to see the grammar.

    The workaround for the current release is not to use rulenames that start with 'l', 'u', 'L', 'U', 'Q', or 'E' in a matchref. Which sucks, I know.

    The long term solution (which was just uploaded to CPAN as Regexp::Grammars version 1.020) is to change the entire matchref syntax from <\IDENT> to <\_IDENT>. Yes, that's horribly backwards incompatible, but it's the only solution that doesn't involve some deep and scary hacking on the Perl core. I'm sorry.

    Damian

Re: The usage of <ALIAS=\IDENT> of Regexp::Grammars
by Anonymous Monk on Aug 10, 2012 at 01:29 UTC

    I think its a bug like Bug #75644 for Regexp-Grammars: debug does not work

    You can workaround by following http://search.cpan.org/src/DCONWAY/Regexp-Grammars-1.016/t/backref.t and http://search.cpan.org/src/DCONWAY/Regexp-Grammars-1.016/t/backref_ARG.t, which DO NOT test this (your) example from the docs

    The workaround does work

    #!/usr/bin/perl -- use strict; use warnings; use Data::Dumper; my $grammar = do { use Regexp::Grammars; qr{ <debug:on> <delimited_string> <token: delimited_string> <delim=(['"`])> <content=(.*?)> <rdel=\delim> }xms; }; my $string = "'hi'"; if ($string =~ $grammar) { print Dumper \%/; } __END__ =====> Trying <grammar> from position 0 'hi' |...Trying <delimited_string> | |...Trying <delim=(['"`])> hi' | | \_____<delim=(['"`])> matched ''' | |...Trying <content=(.*?)> | | \_____<content=(.*?)> matched '' | |...Trying <rdel=\delim> | | \FAIL <rdel=\delim> i' | | \_____<content=(.*?)> matched 'h' | |...Trying <rdel=\delim> | | \FAIL <rdel=\delim> ' | | \_____<content=(.*?)> matched 'hi' | |...Trying <rdel=\delim> [eos] | | \_____<rdel=\delim> matched ''' | \_____<delimited_string> matched ''hi'' \_____<grammar> matched ''hi'' $VAR1 = { '' => '\'hi\'', 'delimited_string' => { '' => '\'hi\'', 'content' => 'hi', 'delim' => '\'', 'rdel' => '\'' } };

    Regexp::Grammars::Common::String use this syntax

    You should report this bug upstream , maybe write this example as a test case following t/backref*