progs/catastrophic/catastrophic.dart
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 21 Feb 2024 09:14:12 +0000
changeset 959 64ec1884d860
parent 777 a10430cb797c
permissions -rw-r--r--
updated and added pascal.while file

/// A case of catastrophic backtracking in Dart
///---------------------------------------------
/// example provided by Martin Todorov
///
/// regex: (a*)*b
/// strings: aa...a
///
/// run with
///
///    dart catastrophic.dart n


main(List<String> args) {
  final n = int.parse(args[0]);
  final evilRegex = RegExp("(a*)*b");

  for(int i = 1; i <= n; i++) {
    final toMatch = "a" * i;
    final stopwatch = Stopwatch()..start();
    evilRegex.hasMatch(toMatch);
    print("$i a's matched in ${stopwatch.elapsed}");
  }
}