Detecting infinite loop in brainfuck program

前端 未结 9 1516
无人及你
无人及你 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条回答
  •  盖世英雄少女心
    2021-02-04 01:11

    State in bf is a single array of chars.

    If I were you, I'd take a hash of the bf interpreter state on every "]" (or once in rand(1, 100) "]"s*) and assert that the set of hashes is unique.

    The second (or more) time I see a certain hash, I save the whole state aside.

    The third (or more) time I see a certain hash, I compare the whole state to the saved one(s) and if there's a match, I quit.

    On every input command ('.', IIRC) I reset my saved states and list of hashes.

    An optimization is to only hash the part of state that was touched.

    I haven't solved the halting problem - I'm detecting infinite loops while running the program.

    *The rand is to make the check independent of loop period

提交回复
热议问题