progs/catastrophic.js
author Christian Urban <urbanc@in.tum.de>
Tue, 08 Oct 2019 21:12:52 +0100
changeset 649 e83afb44f276
parent 614 be065677c8f1
child 701 681c36b2af27
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
614
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
// A case of catastrophic backtracking in JavaScript/Node.js
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
//
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
// regex: (a*)*b
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
// strings: aa...
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
//
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
// call with:
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
//
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
//  $> node catastrophic.py 20
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
//
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
// call with timing as:
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
//
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
//  $> time node catastrophic.py 25
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
const args = process.argv[2]
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
var str = 'a'.repeat(args);
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
console.log(str)
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
var re = /^((a)*)*b$/;
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
var res = re.test(str);
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
be065677c8f1 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
console.log(res)