progs/catastrophic.py
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 04 Jul 2020 16:58:12 +0100
changeset 732 c7bdd7eac4cb
parent 701 681c36b2af27
permissions -rwxr-xr-x
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
#!/usr/bin/env python
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
import re
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
import sys
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
613
bfd511b7ecbf updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
     5
# case of catastrophic backtracking in Python
558
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
     6
#
613
bfd511b7ecbf updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
     7
# regex: (a*)*b
bfd511b7ecbf updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
     8
# strings: aa...a
558
447ed6c7cdad updated
Christian Urban <urbanc@in.tum.de>
parents: 420
diff changeset
     9
#
420
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
    10
# call with timing as:
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
    11
#
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
    12
#   > time ./catastrophic.py 20
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
    13
25bc57b32efa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 411
diff changeset
    14
# counter n given on the command line
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
cn = sys.argv[1]
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
613
bfd511b7ecbf updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
    17
# calling the matching function
bfd511b7ecbf updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
    18
s = ("a" * int(cn))
bfd511b7ecbf updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
    19
m = re.match('(a*)*b' , s) 
49
d2c6852ca8da added programs and slides
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
613
bfd511b7ecbf updated
Christian Urban <urbanc@in.tum.de>
parents: 558
diff changeset
    21
print s
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 613
diff changeset
    22
print m