85
\$\begingroup\$

The challenge is to find a string of characters that cannot appear in any legal program in your programming language of choice. That includes comments, strings, or other "non-executable" parts.

Challenge

  • Your program may be specific to a particular version or implementation of your language's compiler/interpreter/runtime environment. If so, please specify the particulars.
  • Only standard compiler/interpreter/runtime options are permitted. You cannot pass some weird flag to your compiler to get a specific result (e.g. passing a flag to convert warnings into errors).
  • If your programming language requires a specific encoding (e.g. UTF-8), your string must also be correctly encoded (i.e. strings which fail solely due to character decoding errors are not allowed).
  • Every individual character in your submission must be admissible in a legal program; that is, you can't just use a character which is always rejected.
  • The compiler/interpreter/runtime must give an error when given any source code that contains your string as a substring. The error does not have to be the same across programs - one embedding of your string might cause a syntax error, while another might cause a runtime error.

Scoring

  • Shortest illegal string for each language wins.
  • You should explain why your string is illegal (why it cannot appear anywhere in a legal program).
  • Dispute incorrect solutions in the comments. More specifically, you should provide a link to TIO or equivalent demonstrating a legal program (i.e. one that doesn't produce any errors) that contains the proposed substring.
  • Some languages (e.g. Bash, Batch, Perl) allow arbitrary binary data to be appended to a program without affecting validity (e.g. using __DATA__ in Perl). For such languages, you may submit a solution that can appear only in such a trailing section. Make sure to make a note of that in your answer. (The definition of this "trailing section" is language-dependent, but generally means any text after the parser has entirely stopped reading the script).

Example

In Python, I might submit

x
"""
'''

but this can be embedded into the larger program

"""
x
"""
'''
y
'''

so it isn't admissible.

\$\endgroup\$
  • 2
    \$\begingroup\$ Can a counter-example rely on input from STDIN? \$\endgroup\$ – Zacharý Jul 20 '17 at 14:17
  • 6
    \$\begingroup\$ Would this make a good CnR? \$\endgroup\$ – CalculatorFeline Jul 20 '17 at 16:59
  • 2
    \$\begingroup\$ Too late now I guess, but it seems like this could have been a cops and robbers challenge. There's a lot of skill evident in the attempts to make valid programs, as well as coming up with the strings in the first place. \$\endgroup\$ – user2390246 Jul 21 '17 at 7:43
  • 5
    \$\begingroup\$ My condolences to the Perl attempts. :) \$\endgroup\$ – Kaz Jul 23 '17 at 15:40
  • 2
    \$\begingroup\$ I'm pretty sure it's completely impossible in non-literate Haskell, thanks to nested comments. \$\endgroup\$ – dfeuer Mar 19 '19 at 1:50

46 Answers 46

1 2
1
\$\begingroup\$

AutoHotkey, 5 bytes

` is the escape character. You can only escape a " when assigning it to a variable.

\n*/ prevents it from being commented out or assigned to a variable.


*/`"
\$\endgroup\$
1
\$\begingroup\$

JavaScript, 11 characters

`
`*/}'"`\u)

The backticks make sure to kill template strings, the quotes get rid of strings, the newline avoid commented lines, the end of comment avoids block comments, and the last backtick and escape (with a ! to avoid appending numbers) try to start a invalid string.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Scratch (scratchblocks2), 3 bytes

There's no such thing as an error in scratchblocks2 - just red-colored blocks - but this can't be expressed in actual Scratch, so I think it's OK.


<?

Leading newline to avoid this just being commented or ::ed out.

Then a predicate block with it's label starting with ? - there's no such block.

\$\endgroup\$
  • 1
    \$\begingroup\$ Can't test it (I'm not going to install scratchblocks2), but I don't see why say [\n<?] breaks. The [] seem to support parsing \n (like for drop-down menus). \$\endgroup\$ – ბიმო Nov 22 '18 at 17:27
  • \$\begingroup\$ There's a leading newline. \$\endgroup\$ – W. K. Nov 22 '18 at 21:15
1
\$\begingroup\$

Husk1, 3 bytes

Try it online!

Explanation

The newlines force to be parsed as a supposed built-in, however since it's not (yet) implemented the parsing fails with unexpected "\9674" or an error because of empty lines.

Note: Initially I tried to force an inference failure, but the type-checking is done lazily and one can easily "un-break" programs with adding a valid main function.


1: The code might work at some point in the future. So to be precise any version of Husk as of before the date of this post (ie. at least up to commit 0806b9d).

\$\endgroup\$
  • \$\begingroup\$ An inference failure would have been fun, but this is probably shorter than that would be anyhow. \$\endgroup\$ – Unrelated String Apr 4 '19 at 2:28
1
\$\begingroup\$

ECMAScript Regex, 4 bytes

]](+

Try it on regex101

This is a quite a bit easier than PCRE. There's no \Q...\E, no free-spacing mode, and no comments. But if we used just ](+ we could still be inside a character class and have our ] escaped, as [\](+] which would be treated as a character class of ](+. So we still need the double ] to make sure we exit any character class we may have been in, which works even if a range was started, e.g. [!-]](+.

(+ is illegal in any context other than a character class, and will give an error message such as "Nothing to repeat" or "Incomplete group structure" / "The preceding token is not quantifiable".

\$\endgroup\$
1
\$\begingroup\$

PCRE Regex, 6 7 bytes

\E
)](+

Try it on regex101

Any string not containing \E would be legal inside a \Q...\E literal sequence. By starting this one with \E, we break out of such a sequence if we were in one. And if we weren't in one, but are preceded by a \, then it will be treated as a literal \E, and we'll still be guaranteed not to be inside a \Q...\E.

(+ is not part of any legal group structure, and will generate a compile-time error ("Incomplete group structure" and/or "The preceding token is not quantifiable" / "quantifier does not follow a repeatable item") anywhere other than:

  1. Within a \Q...\E. We've handled this. We can't be inside one thanks to the \E.
  2. Immediately after a \. But since we have it immediately after a \E, it can't be immediately after a \. Note that \E alone is valid, even if there was no \Q before it.
  3. Inside a #... style comment. This can only happen in free-spacing mode, but this can be turned on by (?x) anywhere in a regex, so we need to handle it.
  4. Inside a (?#...) style comment.
  5. Inside a character class, e.g. [(+] or [\Q\E(+] – or even [\Q\E](+], which will be treated as [](+], which, since an empty character class is not part of PCRE syntax (except in PCRE2 with the PCRE2_ALLOW_EMPTY_CLASS option enabled), is treated as a character class consisting of ](+ (the beginning of a character class is the only place where ] does not need to be escaped).

Because of #3, we need a newline, to break out of any #... comment we may be in.

Because of #4, we need a ), to break out of any (?#...) comment we may be in.

And it is because of #5 that we need to put ] in front of the (+. This closes any character class we might have been in. If we hadn't already put a newline and/or a ) to close a potential comment, we'd need this to be ]], because a character class can't be empty and ] is allowed to be the first character in a class without being escaped. In any case, thanks to having a character before our ], it even works if a range was started, e.g. [!-\E)](+.

Edit: Silly me, didn't protect it from being inside comments. Fixed.

\$\endgroup\$
1
\$\begingroup\$

Keg, 3 4 bytes

Fixed a for loop bug noted by @Jono2906


)ø.

Try it online!

Explanation

\n    Terminate a line-comment
)     End a for loop
 ø    Clear the stack
  .   Try to print the TOS item, which will create an error to the program.
\$\endgroup\$
  • \$\begingroup\$ 3 bytes using strings \$\endgroup\$ – Lyxal Nov 26 '19 at 20:51
  • \$\begingroup\$ But this is valid. \$\endgroup\$ – user85052 Nov 27 '19 at 4:06
0
\$\begingroup\$

Gaia, 3 bytes


#“

Try it online!

Each line in Gaia is a separate function, so the newline ensures that the code starts at the beginning of a function. Even putting a newline in a string literal will start a new function, since Gaia allows omitting closing quotes. In addition, all functions are parsed before execution, so adding additional functions below won't help.

The # is a meta, which has to directly follow an operator. At the start of the function, there is no operator, so it's a syntax error.

The is an opening quote for string literals. It's there because Gaia also allows omitting the opening quote of strings at the start of a function. If this opening quote wasn't here, you could write #” which is entirely legal.

\$\endgroup\$
0
\$\begingroup\$

Ly, 6 bytes


\""{)

(note the leading newline)

The newline prevents line comments, Ly doesn't have block comments, the \"" ensures that all open string literals will close, and the unmatched brackets raise the error.

\$\endgroup\$
0
\$\begingroup\$

FRACTRAN, 2 bytes

()

Try it online!

Since FRACTRAN doesn't have any way of including comments or literals (AFAICT), this will always error any valid program, since all valid programs must be a valid fraction, and this string can never be part of a valid fraction.

\$\endgroup\$
0
\$\begingroup\$

Ink, 5 bytes


*/{}

Leading newline ends single-line comments.
*/ ends multi-line comments. Thanks to the leading newline, you can't put a / in front of it to make it the start of a comment rather than the end of one.
{ and } enclose things meant to be parsed, rather than simply printed. If there's nothing between them, the compiler gets sad because it Expected some kind of logic, conditional or sequence within braces: { ... } but saw '}'. This happens even inside string literals, so there's no need to check if we're inside one of those.

\$\endgroup\$
0
\$\begingroup\$

Milky Way, 2 bytes

Tries to execute an undefined opcode. Milky Way does not have comments. The newline is for ending strings.


)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Unreadable, 2 bytes

''

Try it online!

All Unreadable commands must be of the form '""…", with one ' followed by 1 to 10 "s. Having two successive 's anywhere in the program leads to error: parser failed: invalid command (0): '.

\$\endgroup\$
0
\$\begingroup\$

Haskell, 5 bytes

-}
''

By itself, this produces parse error on input '}', because the curly brace isn't closing anything.

If it's preceded by {-, you get parse error (possibly incorrect indentation or mismatched brackets.

If you wrap that in a block do:

main=do{{-
-}
''}

you get parse error on input '}' again, because the parser sees an incomplete character literal.

You cannot wrap this sequence in a string because newlines in strings must be wrapped in backslashes, which can otherwise only contain other whitespace.

Please note that I am not considering language extensions like Template Haskell.

\$\endgroup\$
  • \$\begingroup\$ Cracked. Note also that Haskell supports nested block comments, so I can always add more {-s than you have -}s. \$\endgroup\$ – Anders Kaseorg Jan 25 at 13:35
0
\$\begingroup\$

JavaScript, 7 bytes

 */
#`#
  • Adding a // at the beginning will still not work because of the leading newline, leaving the second line uncommented.

  • Adding a /* will not uncomment the string completely because of the closing */ that completes it, leaving the # exposed.

  • Adding ` will not quote the string completely, because of ` that completes a string, leaving the # exposed.

  • Regular expressions won't work because of # following / character.

  • / following * cannot be parsed as a regular expression, as regular expressions cannot have newlines

Try it!

clicky.onclick=a=>{console.clear();console.log(eval(before.value+" */\n#`#"+after.value));}
textarea {
  font-family: monospace;
}
<button onclick="console.clear();">Clear console</button>
<br>
<textarea id=before placeholder="before string"></textarea>
<pre><code> */
#`#</code></pre>
<textarea id=after placeholder="after string"></textarea>
<br>
<button id=clicky>Evaluate</button>

\$\endgroup\$
  • \$\begingroup\$ cracked. i'm pretty sure this could be fixed with two newlines at the start. edit: nevermind \$\endgroup\$ – randomdude999 Jan 13 at 20:52
0
\$\begingroup\$

Turing Machine Code 5 bytes

Assuming block editing isn't allowed:

0

Or with the symbols showing:

< cr >< lf >
0
< cr >< lf >

Without block editing, it is impossible to stick this behind the comment symbol (';'), as the '0' will end up on the next line anyway. There is no block commenting in Turing Machine Code, a fact that is taken advantage of in other answers here as well. This patch of code would not only not run, it would kill the whole program before it can begin to execute, no matter where it is placed.

Try it online!

\$\endgroup\$
1 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.