[Tutor] BF Program hangs

Dave Angel davea at ieee.org
Sun Jun 19 14:13:32 CEST 2011


On 01/-10/-28163 02:59 PM, Kaustubh Pratap chand wrote:
 > Hello i made this program which interprets brainf***
 >
 >   But i don't understand why it doesn't ends....and also it doesn't 
print anything
 >
 >   import sys
 >
 >   cell=[0] * 30000
 >   code_pointer=0
 >   cell_pointer=0
 >   close_brace_pos=0
 >   open_brace_pos=0
 >
 >   bf=raw_input("Input bf program:")
 >
 >   while (code_pointer<len(bf)):
 >
 >   c=bf[code_pointer]

Indentation error.  Presumably a flaw in your email program.  Try using 
text mode.


 >
 >   if c=='.':
 >   sys.stdout.write(cell[cell_pointer])
 >   code_pointer+=1

write() doesn't take an integer parameter, but only character strings. 
If you want to be true to the original language, you'll need a chr() 
function there.

But if I were you, I'd be debugging this by making it print something 
more verbose, in case the problem is invisible characters.


 >
 >   elif c==',':
 >   cell[cell_pointer]=sys.stdin.read(1);
 >   code_pointer+=1

This one's a bit tougher, since I don't know any portable way to get a 
single character of input from a user.  In any case, you'll be wanting 
to use ord() to convert (one of) user's character to an integer.
 >
 >   elif c=='>':
 >   cell_pointer+=1
 >   code_pointer+=1
 >    <snip>

In any case, I'd make two other changes till you get it working:

1) make the input explicit, so people can know what you're testing this 
with.  After all, if someone were to run it and just press enter, then 
of course it would print nothing.

2) add a print statement of some sort to the loop, so you can see how 
the pointers are doing.

DaveA


More information about the Tutor mailing list