17
1
Yet another Brainfuck parsing problem, but this time... different.
You are working in the Infinite Monkeys Incorporated, the company making Brainfuck programs, to solve various interesting problems (by accident, no less - after all, the company makes random programs). However, it appears that your fast Turing machines that only execute Brainfuck have a small and expensive problem with syntax errors - make one, and the computer explodes. It's probably a design flaw, but nobody had bothered to find why it happens.
As Turing machines (especially fast ones) are expensive (after all, they have infinity RAM which costs), it would be better to make sure that the program doesn't have any syntax errors before executing the code. Your company is going to run lots of code, so manual verification won't work. Write a program that reads the STDIN for Brainfuck code, and exits with exit status set to anything other than 0 (error) if program has any syntax error (for example,
]
is a syntax error, because there is no matching[
). Exit with exit status set to 0 if the program is completely fine.Make sure that your program correctly notices mistakes involving
[]
. You wouldn't want another computer to explode, would you? Oh, and make sure it's as short as possible - your boss pays for short programs (because he thinks they are fast, or something). Oh, and you don't have to code in Brainfuck (in fact, you cannot, because Brainfuck doesn't support exit codes) - your code will be ran on normal computer.
So, as you can see, your job is to check if Brainfuck program is "valid" (has paired []
symbols). Please note that Brainfuck programs can have other characters than []
, so don't refuse the program just because it has other commands. Smallest code wins, but probably you would care more about upvotes anyway.
1What about languages that don't allow setting a process exit code? – Kendall Frey – 2014-01-03T17:24:21.907
1@KendallFrey: Simple, use something else. Or in GolfScript, embed Ruby code. Perhaps this blocks some esoteric programming languages, but well... – Konrad Borowski – 2014-01-03T17:26:24.793
1Is it okay to set the error code to something other than 1 (as long as it's not 0 of course) on failure? – marinus – 2014-01-03T18:31:13.380
@marinus: I don't know why you would want that, but I think it's fine. – Konrad Borowski – 2014-01-03T18:33:00.167
@GlitchMr: because then I can use
GCD(a,b)
instead of0 != a || b
. – marinus – 2014-01-03T18:50:47.390Does this solely consist of checking that every
[
has a matching]
? – Peter Taylor – 2014-01-03T19:16:40.420@PeterTaylor: Yes. – Konrad Borowski – 2014-01-03T20:13:39.310
So you consider the following BF program valid:
<+
? What will your Turing machine do? – vsz – 2014-01-04T09:14:04.643@vsz: It is valid, because the program begins in middle of infinite tape. But I guess this makes sense. – Konrad Borowski – 2014-01-04T09:23:38.487