Detecting infinite loop in brainfuck program

前端 未结 9 1540
无人及你
无人及你 2021-02-04 00:18

I have written a simple brainfuck interpreter in MATLAB script language. It is fed random bf programs to execute (as part of a genetic algorithm project). The problem I face is,

9条回答
  •  旧时难觅i
    2021-02-04 00:56

    Infinite loop cannot be detected, but you can detect if the program is taking too much time.

    Implement a timeout by incrementing a counter every time you run a command (e.g. <, >, +, -). When the counter reaches some large number, which you set by observation, you can say that it takes very long time to execute your program. For your purpose, "very long" and infinite is a good-enough approximation.

提交回复
热议问题