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

I must be missing something obvious in here but I just can't seem to find it. Trying to craft a grammar using Regexp::Grammars and I can't get this code to compile. I keep getting the "Quantifier follows nothing" error.

my $token_grammar = qr { <nocontext:> <debug: on> <[unit]>+ <token: unit> <word> | <punctuation> <token: word> \b \w+ \b | <hyphenword> | <con +traction> <token: hyphenword> \b \w+ ( - | -- ) \w+ \b <token: contraction> \b \w+ ' \w \b <token: punctuation> ( \.+ | ,+ | ?+ | !+ | "+ | '+ | ;+ | : ++ | -+ | &+ | \(+ | \)+ | \[+ | \]+ ) };

I've tried reducing it to just

my $token_grammar = qr { <nocontext:> <debug: on> <[unit]>+ <token: unit> <word> <token: word> \b \w+ \b }

But this just gives be a "Nested Quantifiers" error. Any ideas? Thanks for looking!

Replies are listed 'Best First'.
Re: Quantifier follows nothing error in Regexp::Grammars (quantifier "?")
by Anonymous Monk on Oct 09, 2013 at 06:57 UTC

    The question mark is a quantifier

    $ perl -le " qr{\d****} " Nested quantifiers in regex; marked by <-- HERE in m/\d** <-- HERE **/ + at -e line 1. $ perl -le " qr{(?#an error follows)****} " Quantifier follows nothing in regex; marked by <-- HERE in m/(?#an err +or follows)* <-- HERE ***/ at -e line 1. $ perl -le " qr{?} " Quantifier follows nothing in regex; marked by <-- HERE in m/? <-- HER +E / at -e line 1.

    You need to learn to character class :) See perlrecharclass

    Try  <token: punctuation>  ( [.,?!"';:\-&()\[\]]+ )

    See also RFC: A walkthrough from JSON ABNF to Regexp::Grammars and The usage of <ALIAS=\IDENT> of Regexp::Grammars

      Minor comment: the hyphen should be the first character in the character class:

      <token: punctuation> ( [-.,?!"';:\&()\[\]]+ )

      otherwise it is interpreted as defining a range rather than a literal hypen.

        the hyphen should be the first character in the character class ... otherwise it is interpreted as defining a range rather than a literal hypen.

        ...or the last...
        ...or escaped with a backslash \.

        Cheers, Sören

        Créateur des bugs mobiles - let loose once, run everywhere.
        (hooked on the Perl Programming language)

        perldoc perlrecharclass says you don't need to escape [ within a (non-Posix) character class, and for ] there is a similiar rule as for the hyphen, so
        <token: punctuation> ( [][.,?!"';:&()-]+ )
        should be valid without any backslash.
        Update: corrected position of hyphen, had it outside of the regex :-/

        Minor comment: the hyphen should be the first character in the character class:

        Why do you think so, what are the reasons for this habit/style?

        I vaguely recall maybe that I read this advice before, but it didn't stick :) (not memorable)

Re: Quantifier follows nothing error in Regexp::Grammars
by hdb (Monsignor) on Oct 09, 2013 at 06:10 UTC

    Could you please post a complete script that exhibits your error?

    Update: I think you also need a /x modifier like my $token_grammar = qr {...}x;.