Are Perl regexes turing complete?

前端 未结 3 1724
Happy的楠姐
Happy的楠姐 2020-12-04 23:06

I\'ve seen Ruby and Perl programmers do some complicated code challenges entirely with regexes. The lookahead and lookbehind capabilities in Perl regexes make them more powe

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 00:04

    For regexes in Perl there are two cases:

    1. With embedded code: They are of course Turing-complete.
    2. Without embedded code: They always halt so they are not general Turing machines.

    Every regular language can be accepted by a finite automaton. Its input must be a finite string.

    [...] a deterministic finite automaton (DFA)—also known as deterministic finite state machine—is a finite state machine that accepts/rejects finite strings of symbols [...].

    The same goes for Turing machines: The formal definition does not even have input. It must be encoded in the finite number of states.

    Alternative (equivalent) definitions include input, but it must be finite.

提交回复
热议问题