in reply to When the only tool you have is a screwdriver...

Do you have a good set of test cases ?

#!/usr/bin/perl # http://perlmonks.org/?node_id=1144184 use strict; use warnings; sub e { /\Guc*/gc or return 0; # rule 1. & 5. /\Ga/gc and return e(); # rule 2. /\Gc+/gc; # rule 5. /\Gd/gc and return e() && /\Gf/gc && e(); # rule 3. /\Gb/gc and return e(); # rule 4. return 1; } for (qw( u udufu uau uu uc ubu ucbuc uaau ua uauau uccc )) { print e() && /\G\z/ ? "matches" : " fail", " $_\n"; }

outputs:

matches u matches udufu matches uau fail uu matches uc matches ubu matches ucbuc fail uaau fail ua matches uauau matches uccc

Which of these is wrong, if any? Let's keep tweaking until it works :)

Replies are listed 'Best First'.
Re^2: When the only tool you have is a screwdriver...
by Anonymous Monk on Oct 08, 2015 at 18:22 UTC

    Cleaning up (and making more clear) the parsing.

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1144184 use strict; use warnings; sub e { /\Gua/gc ? return e() : /\Gu/gc || return 0; 1 while (/\Gd/gc && e() && /\Gf/gc or /\Gb/gc) && e() or /\Gc/gc; return 1; } for (qw( u udufu uau uu uc ubu ucbuc uaau ua uauau uccc uauuau )) { print e() && /\G\z/ ? "matches" : " fail", " $_\n"; }

    Again, do you have a good set of test cases ?

      Adding random test cases found a weird frankenbug. Fixing backup seems to have fixed it. Do you have any good test cases ?

      #!/usr/bin/perl -l # http://perlmonks.org/?node_id=1144184 use strict; use warnings; sub e { my $p; /\Gua/gc ? return e() : /\Gu/gc || return 0; 1 while /\Gc/gc or ($p = pos(), /\Gb/gc && e() || (pos() = $p, 0)) or ($p = pos(), /\Gd/gc && e() && /\Gf/gc && e() || (pos() = $p, 0)); return 1; } for (qw( u udufu uau uu uc ubu ucbuc uaau ua uauau uccc uauuau udufuaua uaua uauau uccbucdc )) { print e() && /\G\z/gc ? "matches" : " fail", " $_"; }

        s/frankenbug/heisenbug/