progs/catastrophic/catastrophic.js
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 09 Dec 2021 00:07:51 +0000
changeset 862 3ea5ba4bc3b4
parent 753 d94fdbef1a4f
permissions -rwxr-xr-x
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
745
7dc3643a0cc5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 741
diff changeset
     1
#!/usr/local/bin/node
614
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
// A case of catastrophic backtracking in JavaScript/Node.js
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
//
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
// regex: (a*)*b
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
// strings: aa...
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
//
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
// call with:
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
//
753
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 745
diff changeset
    10
//  $> ./catastrophic.js 20
614
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
//
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
// call with timing as:
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
//
753
d94fdbef1a4f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 745
diff changeset
    14
//  $> time ./catastrophic.js 25
614
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
const args = process.argv[2]
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
var str = 'a'.repeat(args);
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
console.log(str)
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
var re = /^((a)*)*b$/;
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
var res = re.test(str);
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
console.log(res)