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 at 1:50

46 Answers 46

58
\$\begingroup\$

Changeling, 2 bytes




That's two linefeeds. Valid Changeling must always form a perfect square of printable ASCII characters, so it cannot contain two linefeeds in a row.

The error is always a parser error and always the same:

This shape is unpleasant.

accompanied by exit code 1.

Try it online!

\$\endgroup\$
  • \$\begingroup\$ This also works with 2Col. Try it online!. But the reason this breaks in 2Col is that every line needs to consist of exactly 2 characters, but the empty line breaks that. \$\endgroup\$ – Kritixi Lithos Jul 20 '17 at 6:10
  • 2
    \$\begingroup\$ +1 because this is automatically the winner, since 1-byte solutions are not allowed because "you can't just use a character which is always rejected." \$\endgroup\$ – Zacharý Jul 20 '17 at 14:46
  • 1
    \$\begingroup\$ @Cowsquack tfw I forgot about my own language \$\endgroup\$ – Skidsdev Jul 21 '17 at 10:54
  • 1
    \$\begingroup\$ @Skidsdev tfw I re-forgot about my own language, and forgot about me forgetting about my own language \$\endgroup\$ – Skidsdev Nov 19 '18 at 18:19
  • \$\begingroup\$ @Zacharý What about 0-byte solutions? \$\endgroup\$ – PyRulez Nov 19 '18 at 23:23
34
\$\begingroup\$

Java, 4 bytes

;\u;

Try it online!

This is an invalid Unicode escape sequence and will cause an error in the compiler.

error: illegal unicode escape
\$\endgroup\$
  • \$\begingroup\$ Doesn't work - one could have a string literal like "\\u;". \$\endgroup\$ – feersum Jul 20 '17 at 6:57
  • \$\begingroup\$ @feersum Fixed at the cost of one byte \$\endgroup\$ – Kritixi Lithos Jul 20 '17 at 7:07
  • 21
    \$\begingroup\$ @TheLethalCoder: Java preprocesses source code to alter \uXXXX escapes before doing anything else, so yes, this will work even inside comments.za \$\endgroup\$ – nneonneo Jul 20 '17 at 8:58
  • 3
    \$\begingroup\$ I think this is the shortest Java answer in the history of this site still.. \$\endgroup\$ – Magic Octopus Urn Mar 2 '18 at 18:31
  • 1
    \$\begingroup\$ @MagicOctopusUrn Actually, there is this 0 bytes Java answer (which isn't relevant anymore in the current meta, since it outputs to STDERR instead of STDOUT). Although both are pretty amazing and clever. :) \$\endgroup\$ – Kevin Cruijssen Mar 20 '18 at 16:13
26
\$\begingroup\$

COBOL (GNU), 8 bytes


THEGAME

First, a linefeed to prevent you from putting my word in a commented line.

Then, historically, COBOL programs were printed on coding sheets, the compiler relies heavily on 80-character limited lines, there are no multiline comments and the first 6 characters are comments (often used as editable line numbers), you can put almost anything there, AFAIK. I chose THEGAM at the beginning of the next line.

Then, the 7th symbol in any line only accepts a very restricted list of characters : Space (no effect), Asterisk (comments the rest of the line), Hyphen, Slash, there may be others, but certainly not E.

The error given by GnuCobol, for instance, is :

error: invalid indicator 'E' at column 7

Try it online!

Also, you just lost the game.

\$\endgroup\$
  • 34
    \$\begingroup\$ Also, you just lost the game. I almost downvoted \$\endgroup\$ – Stephen Jul 21 '17 at 13:30
26
\$\begingroup\$

JavaScript, 7 bytes


;*/\u)

Note the leading newline.

  • \u) is an invalid Unicode escape sequence and this is why this string is invalid
  • 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 \u) exposed
  • As stated by @tsh, the bottom line can be turned into a regex by having a / after the string, so by having the ) in front of the \u, we can ensure that the regex literal will always be invalid
  • As stated by @asgallant, one could do 1||1(string)/ to avoid having to evaluate the regex. The semi-colon at the beginning of the second line stops that from happening by terminating the expression 1||1 before it hits the second line, thus forcing a SyntaxError with the ;*.

Try it!

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

\$\endgroup\$
  • 2
    \$\begingroup\$ /* */\u0045 = 3 seems valid JavaScript code. \$\endgroup\$ – tsh Jul 20 '17 at 5:36
  • 2
    \$\begingroup\$ 3 */\u;/ is still valid. \$\endgroup\$ – tsh Jul 20 '17 at 5:55
  • 3
    \$\begingroup\$ Interesting to note that as of ES2018 (which won't be official until the end of this year) you can just wrap the whole thing in backticks due to this. You could probably fix this though just by inserting a backtick after the / (not that you need to fix it). (Also, the ; doesn't force the parsing of the bad regex, it forces a SyntaxError with the *.) \$\endgroup\$ – ETHproductions Jul 20 '17 at 15:28
  • 1
    \$\begingroup\$ @Leushenko But this doesn't work against #if 0 as seen here: Try it online! \$\endgroup\$ – Kritixi Lithos Jul 21 '17 at 18:17
  • 4
    \$\begingroup\$ In newer JS versions, String.raw with a template string can make this not break, because the invalid escape fails. Would be: String.raw`code here` \$\endgroup\$ – iovoid Feb 14 '18 at 4:23
17
\$\begingroup\$

Python, 10 bytes (not cpython)


?"""?'''?

Edit:

Due to @feersum's diligence in finding obscure ways to break the Python interpreter, this answer is completely invalidated for any typical cpython environment as far as I can tell! (Python 2 and 3 for both Windows and Linux) I do still believe that these cracks will not work for Pypy on any platform (the only other Python implementation I have tested).

Edit 2:

In the comments @EdgyNerd has found this crack taking advantage of a non-ascii encoding declaration! This seems to decode to print(""). I don't know exactly how this was found but I imagine the way to fix this sort of exploit would maybe be to try different combinations of any invalid characters where the ?s are, and find one that doesn't behave well with any encoding.


Note the leading newline. Cannot be commented out due to the newline, and no combination of triple quoted strings should work if I thought about this correctly.

@feersum in the comments seems to have completely broken any cpython program on Windows as far as I can tell by adding the 0x1A character to the beginning of a file. It seems that maybe (?) this is due to the way this character is handled by the operating system, apparently being a translated to an EOF as it passes through stdin because of some legacy DOS standard.

In a very real sense this isn't an issue with python but with the operating system. If you create a python script that reads the file and uses the builtin compile on it, it gives the more expected behavior of throwing a syntax error. Pypy (which probably does just this internally) also throws an error.

\$\endgroup\$
  • 1
    \$\begingroup\$ @officialaimm consider """?'''""" \$\endgroup\$ – KSab Jul 20 '17 at 5:20
  • 3
    \$\begingroup\$ I made a program with this substring that runs on my machine. However, I think it does not run on many interpreters/platforms/versions. Can you specify which version of Python interpreter and OS this answer is targeting? \$\endgroup\$ – feersum Jul 20 '17 at 7:54
  • 1
    \$\begingroup\$ Python 3 on Windows 7 happens to be exactly where my crack is working. Pastebin of base64-encoded program \$\endgroup\$ – feersum Jul 20 '17 at 18:19
  • 1
    \$\begingroup\$ I can crack this one as well. Simply put a 0x1A character at the beginning of the file, and all the rest of it is ignored (this actually works for Python 3 as well). \$\endgroup\$ – feersum Jul 24 '17 at 2:55
  • 2
    \$\begingroup\$ I know this is really old, but after working with some people over in the Python Discord, we found this crack, although I don't know if changing the encoding can be considered cheating \$\endgroup\$ – EdgyNerd Oct 5 at 18:40
15
\$\begingroup\$

C (clang), 16 bytes

 */
#else
#else

Try it online!

*/ closes any /* comment, and the leading space makes sure we didn’t just start one. The newline closes any // comment and breaks any string literal. Then we cause an #else without #if or #else after #else error (regardless of how many #if 0s we might be inside).

\$\endgroup\$
  • 5
    \$\begingroup\$ Cracked again. \$\endgroup\$ – feersum Jul 20 '17 at 7:18
  • 2
    \$\begingroup\$ Also since C++11 raw strings seem to work, a solution is impossible with gcc. \$\endgroup\$ – feersum Jul 20 '17 at 7:22
  • \$\begingroup\$ @feersum Huh, TIL that GCC accepts those in C code. I could specify -std=c99, but let’s try switching to clang. \$\endgroup\$ – Anders Kaseorg Jul 20 '17 at 7:28
  • 3
    \$\begingroup\$ I'm really surprised that gcc accepts C++11 raw strings. Specifying the compiler version or implementation is perfectly OK, so if it's illegal in Clang, it's fair game. \$\endgroup\$ – nneonneo Jul 20 '17 at 17:29
  • 1
    \$\begingroup\$ @l4m2 I can't parse your question (who is they, and what do you mean by again?), but note that a C++ raw string literal supports a custom delimeter: R"foobar(...)foobar", and only a right paren followed by the matching delimeter and a quote will close it. \$\endgroup\$ – Anders Kaseorg Nov 2 '18 at 0:41
13
\$\begingroup\$

Ada - 2 bytes

I think this should work:


_

That's newline-underscore. Newline terminates comments and isn't allowed in a string. An underscore cannot follow whitespace; it used to be allowed only after letters and numbers, but the introduction of Unicode made things complicated.

\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome to the site! :) \$\endgroup\$ – DJMcMayhem Jul 21 '17 at 16:57
12
\$\begingroup\$

Pyth, 6 bytes

¡¡$¡"¡

¡ is an unimplemented character, meaning that if the Pyth parser ever evaluates it, it will error out with a PythParseError. The code ensures this will happen on one of the ¡s.

There are three ways a byte can be present in a Pyth program, and not be parsed: In a string literal (" or .", which are parsed equivalently), in a Python literal ($) and immediately after a \.

This code prevents \ from making it evaluate without error, because that only affects the immediately following byte, and the second ¡ errors.

$ embeds the code within the $s into the compiled Python code directly. I make no assumptions about what might happen there.

If the program reaches this code in a $ context, it will end at the $, and the ¡ just after it will make the parser error. Pyth's Python literals always end at the next $, regardless of what the Python code might be doing.

If the program starts in a " context, the " will make the string end, and the final ¡ will make the parser error.

\$\endgroup\$
9
\$\begingroup\$

x86 32-bit machine code, 11 bytes (and future-proof 64-bit)

90 90 90 90 90 90 90 90 90 0f 0b

This is times 9 nop / ud2. It's basically a NOP sled, so it still runs as 0 or more nops and then ud2 to raise an exception, regardless of how many of the 0x90 bytes were consumed as operands to a preceding opcode. Other single-byte instructions (like times 9 xchg eax, ecx) would work, too.

x86 64-bit machine code, 10 bytes (current CPUs)

There are some 1-byte illegal instructions in 64-bit mode, until some future ISA extension repurposes them as prefixes or parts of multi-byte opcodes in 64-bit mode only, separate from their meaning in 32-bit mode. 0x0e is push cs in 32-bit mode, but illegal on current CPUs (tested on Intel Skylake) in 64-bit.

0e 0e 0e 0e 0e 0e 0e 0e 0e 0e

Rules interpretation for executable machine code:

  • The bytes can't be jumped over (like the "not parsed" restriction), because CPUs don't raise exceptions until they actually try to decode/execute (non-speculatively).

  • Illegal means always raises an exception, for example an illegal-instruction exception. (Real programs can catch that with an exception handler on bare metal, or install an OS signal handler, but I think this captures the spirit of the challenge.)


It works because a shorter byte-string ending in ud2 could appear as an imm32 and/or part of the addressing mode for another instruction, or split across a pair of instructions. It's easiest to think about this in terms of what you could put before the string to "consume" the bytes as part of an instruction, and leave something that won't fault.

I think an instruction can consume at most 9 bytes of arbitrary stuff: a SIB byte, a disp32, and an imm32. i.e. the first 2 bytes of this instruction can consume 8 NOPs and a ud2, but not 9.

c7 84 4b 00 04 00 00 78 56 34 12        mov dword [rbx+rcx*2+0x400],0x12345678

Can't beat 9 nops:

    db 0xc7, 0x84   ; opcode + mod/rm byte: consumes 9 bytes (SIB + disp32 + imm32)
    times 9 nop          ; 1-byte xchg eax, ecx or whatever works, too
    ud2
  ----
   b:   c7 84 90 90 90 90 90 90 90 90 90        mov    DWORD PTR [rax+rdx*4-0x6f6f6f70],0x90909090
  16:   0f 0b                   ud2    

64-bit mode:

 c7 84 0e 0e 0e 0e 0e 0e 0e 0e 0e        mov    DWORD PTR [rsi+rcx*1+0xe0e0e0e],0xe0e0e0e
 0e                      (bad)  

But the bytes for 8 NOPs + ud2 (or times 9 db 0x0e) can appear as part of other insns:

    db 0xc7, 0x84   ; defender's opcode + mod/rm that consumes 9 bytes

    times 8 nop          ; attacker code
    ud2

    times 10 nop    ;; defenders's padding to be consumed by the 0b opcode (2nd half of ud2)
----
  18:   c7 84 90 90 90 90 90 90 90 90 0f        mov    DWORD PTR [rax+rdx*4-0x6f6f6f70],0xf909090
  23:   0b 90 90 90 90 90       or     edx,DWORD PTR [rax-0x6f6f6f70]
  29:   90                      nop
  2a:   90                      nop
  ...
\$\endgroup\$
  • \$\begingroup\$ The rules here weren't really clear enough for me to consider posting an asm/machine code answer. For example, why can't you just do ud2? It seems you're saying that you interpret the rules as forbidding jumping over the bytes, so ud2 would work just fine on its own, no? Oh…I guess you're saying the issue is that ud2 can appear as a prefix to a valid instruction? The second part of this answer was a little difficult for me to understand. \$\endgroup\$ – Cody Gray Jul 21 '17 at 6:02
  • \$\begingroup\$ @CodyGray: Right, the 2 bytes that encode ud2 can appear in the imm32 of any instruction. I was thinking about this in terms of what bytes can you put before such a string that "consume" 0f 0b as part of an earlier instruction instead of decoding as ud2. I wasn't totally happy with how I ended up presenting it, but I wanted to illustrate why only 8 nops wasn't enough, and what happened with 9 nops + ud2. \$\endgroup\$ – Peter Cordes Jul 21 '17 at 7:58
  • \$\begingroup\$ @CodyGray: An asm source program would be a totally different answer. That would have to error the parser used by the assembler, not produce faulting machine-code. So something like %else / %else might work to defeat %if 0, which can normally protect any invalid text from being parsed. (idea from a CPP answer) \$\endgroup\$ – Peter Cordes Jul 21 '17 at 8:02
  • \$\begingroup\$ Don't quite satisfy. Your solution may be just in .data. (though it makes it impossible) \$\endgroup\$ – l4m2 Nov 1 '18 at 2:29
  • \$\begingroup\$ @l4m2: To make the question answerable / interesting, I had to limit it to code that's executed (and not jumped over). See the rules interpretation bullet points in my answer. That would also rule out static data, of course. Because then it's not machine code at all, it's just data. This question required more adaptation than most for a machine-code answer to make sense, because there is no compile/assemble stage where you can error the parser, we're just talking about bytes already in memory. \$\endgroup\$ – Peter Cordes Nov 1 '18 at 2:38
7
\$\begingroup\$

C#, 16 bytes


*/"
#endif<#@#>

Works because:

  • // comment won't work because of the new line
  • /* comment won't work because of the */
  • You can't have constants in the code alone
  • Adding #if false to the start won't work because of the #endif
  • The " closes any string literal
  • The <#@#> is a nameless directive so fails for T4 templates.
  • The new line tricks it so having / at the start won't trick the */

Each variation fails with a compilation error.

\$\endgroup\$
  • 1
    \$\begingroup\$ Weird that you decided to include T4 templates in your code. Isn't T4 considered a separate language? \$\endgroup\$ – Arturo Torres Sánchez Jul 20 '17 at 21:07
  • 1
    \$\begingroup\$ @ArturoTorresSánchez I don't know I had never heard of them. Someone commented this didn't work when you included T4 templates so I added the fix. \$\endgroup\$ – TheLethalCoder Jul 21 '17 at 7:59
7
\$\begingroup\$

Commodore 64 Basic, 2 bytes


B

(that's a newline followed by the letter "B").

Any line in a Commodore 64 program must begin with either a line number or a BASIC keyword, and stored programs only permit line numbers. There are no keywords beginning with "B" (or "H", "J", "K", "Q", "X", "Y", or "Z").

\$\endgroup\$
  • \$\begingroup\$ If I append =0 then this just becomes an assignment statement... \$\endgroup\$ – Neil Jul 21 '17 at 9:54
  • 1
    \$\begingroup\$ @Neil, that would be a valid immediate-mode command, but not a valid program. \$\endgroup\$ – Mark Jul 21 '17 at 10:05
7
\$\begingroup\$

APL and MATL and Fortran, 3 bytes


'

Newline, Quote, Newline always throws an error since block comments do not exist:

\$\endgroup\$
  • \$\begingroup\$ This applies to MATL too \$\endgroup\$ – Luis Mendo Jul 20 '17 at 7:39
  • \$\begingroup\$ I think this would work in Fortran too. \$\endgroup\$ – Steadybox Jul 21 '17 at 2:27
  • \$\begingroup\$ Also J I believe \$\endgroup\$ – Jonah Nov 16 at 4:45
  • 1
    \$\begingroup\$ @Jonah Nope. \$\endgroup\$ – Adám Nov 16 at 19:16
  • \$\begingroup\$ Ah. Would \n)\n'\n work for J? \$\endgroup\$ – Jonah Nov 16 at 19:38
6
\$\begingroup\$

INTERCAL, 12 bytes

DOTRYAGAINDO

Try to crack it online!

INTERCAL's approach to syntax errors is a bit special. Essentially, an invalid statement won't actually error unless the program tries to execute it. In fact, the idiomatic syntax for comments is to start them with PLEASE NOTE, which really just starts a statement, declares that it isn't to be executed, and then begins it with the letter E. If your code has DODO in the middle of it, you could prepend DOABSTAINFROM(1)(1) and tack any valid statement onto the end and you'll be fine, if it's DODODO you can just bend execution around it as (1)DON'TDODODOCOMEFROM(1). Even though INTERCAL lacks string literal syntax for escaping them, there's no way to use syntax errors to create an illegal string, even exhausting every possible line number with (1)DO(2)DO...(65535)DODODO, since it seems that it's plenty possible to have duplicate line numbers with COME FROM working with any of them.

To make an illegal string, we actually need to use a perfectly valid statement: TRY AGAIN. Even if it doesn't get executed, it strictly must be the last statement in a program if it's in the program at all. 12 bytes is, to my knowledge, the shortest an illegal string can get using TRY AGAIN, because it needs to guarantee that there is a statement after it (executed or not) so DOTRYAGAIN is just normal code, and it needs to make sure that the entire statement is indeed TRY AGAIN, so TRYAGAINDO doesn't work because it can easily be turned into an ignored, normal syntax error: DON'TRYAGAINDOGIVEUP, or PLEASE DO NOT TRY TO USE TRYAGAINDO NOT THAT IT WOULD WORK. No matter what you put on either side of DOTRYAGAINDO, you'll error, with either ICL993I I GAVE UP LONG AGO, ICL079I PROGRAMMER IS INSUFFICIENTLY POLITE, or ICL099I PROGRAMMER IS OVERLY POLITE.

\$\endgroup\$
  • \$\begingroup\$ There might be a few other compile-time errors which can fire before ICL993I I GAVE UP LONG AGO. \$\endgroup\$ – Unrelated String Apr 4 at 1:47
  • \$\begingroup\$ If, while using every line label, you also COME FROM every line label, it might be a bit difficult to divert control flow around the block, but there's nothing at all stopping you from just GIVING UP! \$\endgroup\$ – Unrelated String Apr 8 at 1:19
5
\$\begingroup\$

Literate Haskell, 15 bytes

Repairing a deleted attempt by nimi.


\end{code}
5
>

Try it online!

nimi's original attempt is the last two lines, based on Literate Haskell not allowing > style literate code to be on a neighboring line to a literate comment line (5 here). It failed because it can be embedded in a comment in the alternate ("LaTeX") literate coding style:

\begin{code}
{-
5
>
-}
\end{code}

However, the \begin{code} style of Literate Haskell does not nest, neither in itself nor in {- -} multiline comments, so by putting a line with \end{code} just before the line with the 5, that workaround fails, and I don't see a different one.

\$\endgroup\$
5
\$\begingroup\$

Shakespeare Programming Language, 2 bytes

.:

Explanation: If this string is in the title of the play, the . ends it and the : is not a valid character name. Similar problems occur in an act and scene name. No character can speak a line beginning with :, and the . will end a Recall statement, which can otherwise create a comment.

\$\endgroup\$
4
\$\begingroup\$

Free Pascal, 18 bytes


*)}{$else}{$else}

First close all possible comments, then handle conditional compile.

Please comment here if I forgot something.

\$\endgroup\$
  • 3
    \$\begingroup\$ @user902383 Does your example contain the leading newline of his snippet? \$\endgroup\$ – Brian J Jul 20 '17 at 18:44
  • \$\begingroup\$ @BrianJ nope, i thought it was just formatting issue, my bad \$\endgroup\$ – user902383 Jul 21 '17 at 9:23
  • \$\begingroup\$ I don't think it's possible in Free Pascal. Just put them after begin end.. \$\endgroup\$ – jimmy23013 Jul 24 '17 at 15:28
  • \$\begingroup\$ @jimmy23013 but it seems that codes after the end. become valid is allowed by the question. \$\endgroup\$ – tsh Jul 25 '17 at 1:13
4
\$\begingroup\$

Brain-Hack (a variation of Brain-Flak), 3 2 bytes

Thanks to Wheat Wizard for pointing out that Brain-Hack doesn't support comments, saving me a byte.

(}

Try it online!

\$\endgroup\$
  • \$\begingroup\$ How do you do comments in Brain-Flak? I don't know of any way to do those. \$\endgroup\$ – Erik the Outgolfer Jul 20 '17 at 18:26
  • \$\begingroup\$ @EriktheOutgolfer # TIO \$\endgroup\$ – Riley Jul 20 '17 at 18:27
  • \$\begingroup\$ Huh undocumented behavior. \$\endgroup\$ – Erik the Outgolfer Jul 20 '17 at 18:29
  • \$\begingroup\$ @EriktheOutgolfer I always assumed they were documented somewhere. I'll look at adding them. \$\endgroup\$ – Riley Jul 20 '17 at 18:34
  • \$\begingroup\$ You don't need the newline in BrainHack or Craneflak, Rain-Flak is the only one of the three versions that has line comments. Although Craneflak parses on the fly so its impossible to solve this in Craneflak, any solution could be beaten by prepending (()){()}. \$\endgroup\$ – Wheat Wizard Aug 7 '17 at 23:26
3
\$\begingroup\$

CJam, 7 bytes

"e#"
:"

Try it online!

\$\endgroup\$
3
\$\begingroup\$

VBA, 2 Bytes

A linefeed followed by an underscore - the _ functions as the line continuation character in VBA, and as there is nothing in the line directly to the left or above the line continuation, coupled with VBA's lack of multiline comments means that this will always throw the compile time error Compile Error: Invalid character


_
\$\endgroup\$
  • \$\begingroup\$ You do depend on your pattern starting on a new line... so, add a newline. \$\endgroup\$ – Deduplicator Jul 20 '17 at 19:18
  • \$\begingroup\$ @Deduplicator it already has a new line, - it does not matter what follows the _, only that there is no valid line to the left or above it \$\endgroup\$ – Taylor Scott Jul 20 '17 at 19:48
  • \$\begingroup\$ What if it is embedded like this: myfunction( \n_ )? \$\endgroup\$ – Deduplicator Jul 20 '17 at 20:00
  • \$\begingroup\$ @Deduplicator the line continuation character must be on the same line as it is continuing ie Public Function Foo( ByVal bar as Integer, _ (new line) bas as long) as double - so yes, it would result in an error if you called the function you described \$\endgroup\$ – Taylor Scott Jul 20 '17 at 21:00
  • \$\begingroup\$ Ok, in that case it's more like myfunction( _ \n_ ). Sorry for the confusion. To put it another way, you should have used two newlines. \$\endgroup\$ – Deduplicator Jul 20 '17 at 21:34
3
\$\begingroup\$

SmileBASIC, 2 bytes


!

Nothing continues past the end of a line, so all you need is a line break followed by something which can't be the start of a statement. ! is the logical not operator, but you aren't allowed to ignore the result of an expression, so even something like !10 would be invalid (while X=!10 works, of course)

Similar things will work in any language where everything ends at the end of a line, as long as it parses the code before executing it.

There are a lot of alternative characters that could be used here, so I think it would be more interesting to list the ones that COULD be valid.

@ is the start of a label, for example, @DATA; ( could be part of an expression like (X)=1 which is allowed for some reason; any letter or _ could be a variable name X=1, function call LOCATE 10,2, or keyword WHILE 1; ' is a comment; and ? is short for PRINT.

\$\endgroup\$
  • \$\begingroup\$ oh, for some reason when I edited the post it was duplicated... \$\endgroup\$ – 12Me21 Mar 1 '18 at 20:11
3
\$\begingroup\$

AWK, 4 bytes



/

Try it online!

Since AWK doesn't have a method to do multi-line comments, need 2 newlines before and 1 after / to prevent commenting out or turning this into a regex, e.g. add 1/. The most common message being `unexpected newline or end of string.

With previous crack

\$\endgroup\$
  • 1
    \$\begingroup\$ I enjoyed this. \$\endgroup\$ – stephanmg Nov 15 at 15:48
  • \$\begingroup\$ Thanks. I enjoyed coming up with it :) \$\endgroup\$ – Robert Benson Nov 18 at 15:54
2
\$\begingroup\$

Fortran, 14 bytes


end program
e

No multiline comments or preprocessor directives in Fortran.

\$\endgroup\$
  • 1
    \$\begingroup\$ Is there a good way to test this online? Also, which version/compiler of Fortran? \$\endgroup\$ – Robert Benson Jul 23 '17 at 15:21
2
\$\begingroup\$

JavaScript (Node.js), 9 8 bytes

`*/
\u`~

Try it online!

I think this should be illegal enough.

Previous JS attempts in other answers


;*/\u)

By @Cows quack

As an ES5 answer this should be valid, but in ES6 wrapping the code with a pair of backticks wrecks this. As a result valid ES6 answers must involve backticks.

`
`*/}'"`\u!

By @iovoid

This is an improved version involving backticks. However a single / after the code breaks this (It becomes a template literal being multiplied by a regex, useless but syntactically valid.) @Neil made a suggestion that changing ! to ). This should theoretically work because adding / at the end no longer works (due to malformed regex.)

Explanation

`*/
\u`~

This is by itself illegal, and also blocks all single and double quotes because those quotes cannot span across lines without a \ at the end of a line

//`*/
\u`~

and

/*`*/
\u`~

Blocks comments by introducing illegal escape sequences

``*/
\u`~

Blocks initial backtick by introducing non-terminated RegExp literal

console.log`*/
\u`~

Blocks tagged template literals by introducing an expected operator between two backticks

\$\endgroup\$
  • \$\begingroup\$ "Template literal... multiplied by a regex." Only in JavaScript would that be a legal expression. \$\endgroup\$ – Andrew Ray Nov 26 at 20:52
2
\$\begingroup\$

Rockstar, 4 5 bytes

Crossed out 4 is still 4 :(

)
"""

Rockstar is a very... wordy language.
While " can be used to define a string, such as Put "Hello" into myVar, to my knowledge there is no way for 3 quotes to appear outside of a comment, and the close paren ensures that won't happen either (Comments in Rockstar are enclosed in parentheses, like this).

Rockstar also has a poetic literal syntax, in which punctuation is ignored, so the newline makes sure that the 3 quotes are the start of a line of code, which should always be invalid

\$\endgroup\$
  • \$\begingroup\$ What about (()"""), wouldn't that be a no-op? \$\endgroup\$ – ბიმო Nov 21 '18 at 23:46
  • \$\begingroup\$ @BMO first paren opens a comment, 2nd paren does nothing because it's commented, 3rd paren closes the comment, then you have """) being parsed as code, which is invalid \$\endgroup\$ – Skidsdev Nov 22 '18 at 1:20
  • \$\begingroup\$ Hmm, nested comments are not in the specs. Comments seem to be discouraged anyways. But you oversaw poetic string literals which allow any string, so Goethe says )""" is valid. \$\endgroup\$ – ბიმო Nov 22 '18 at 17:13
  • \$\begingroup\$ @BMO good point, can be fixed by inserting a newline inbetween ) and """ \$\endgroup\$ – Skidsdev Nov 23 '18 at 18:21
2
\$\begingroup\$

Powershell, 10 8 12 14 13 14 16 bytes

-2 byte thanks to Mazzy finding a better way to break it
+4 -1 bytes thanks to IsItGreyOrGray

$#>
'@';
"@";
@=

I hope this works. ' and " to guard against quotes, #> to break the block-comment, new lines to stop the single-line comment, both '@ and "@ to catch another style of strings, and then starts an improper array to throw a syntax error.

The logic being they can't use either set of quotes to get in, they can't block-comment it out, If @" is used, it'll create a here-string which can't have a token afterwards, and if they leave it alone, it'll try to make a broken array. This statement wants to live so hard, I keep finding even more holes in the armor.

\$\endgroup\$
  • 1
    \$\begingroup\$ Or protectors + @= \$\endgroup\$ – mazzy Nov 19 '18 at 22:26
  • 1
    \$\begingroup\$ @IsItGreyOrGray AAAAAAAAAAAw heck. \$\endgroup\$ – Veskah Jan 25 at 21:12
  • 2
    \$\begingroup\$ It looks like changing #> to $#> will break it as "not recognized as the name of a cmdlet..." It might be made legal again somehow, but I don't have a way. Yet. :) \$\endgroup\$ – GreyOrGray Jan 25 at 21:38
  • 1
    \$\begingroup\$ @IsItGreyOrGray Sonofagun. Now featuring semi-colon armor? \$\endgroup\$ – Veskah Jan 25 at 23:06
  • 1
    \$\begingroup\$ Nice! I've got nothing. Everything I've tried has failed. \$\endgroup\$ – GreyOrGray Jan 28 at 14:31
2
\$\begingroup\$

Runic Enchantments, 3 bytes

One of many possible variations.

Try it online!

Runic utilizes unicode combining characters in a "M modifies the behavior of C" (where C is a command). As such, no two modifiers are allowed to modify the same command and the parser will throw an error if such an occurrence is found.

Similarly, certain commands that redirect the IP cannot be modified in any way, due to the existence of direction modifying modifier characters (and both in the same cell makes no sense).

There is no way to escape or literal-ize the string to make it valid. Tio link contains a ; in order to bypass the higher-priority "no terminator" error.

\$\endgroup\$
2
\$\begingroup\$

TI-Basic (83+/84+/SE, 24500 bytes)

A

(24500 times)

TI(-83+/84+/SE)-Basic does syntax checking only on statements that it reaches, so even 5000 End statements in a row can be skipped with a Return. This, in contrast, cannot fit into the RAM of a TI-83+/84+/SE, so no program can contain this string. Being a bit conservative with the character count here.

The original TI-83 has 27000 bytes of RAM, so you'll need 27500 As in that case.

TI-Basic (89/Ti/92+/V200, 3 bytes)

"

Newline, quote, newline. The newline closes any comments (and disallows embedding the illegal character in a string, since AFAIK multiline string constants are not allowed), the other newline disallows closing the string, and the quote gives a syntax error.

You can get to 2 bytes with

±

without the newline, but I'm not sure whether this counts because ± is valid only in string constants.

\$\endgroup\$
  • \$\begingroup\$ Done, thanks :) \$\endgroup\$ – bb94 Apr 5 at 7:52
2
\$\begingroup\$

Go, 6 bytes


*/```

Try to crack it online!

The grave accent (`) marks a raw string literal, inside which all characters except `, including newlines and backslashes, are interpreted literally as part of the string. Three `'s in a row are the core: adjacent string literals are invalid and ` always closes a ` string, so there's no way to make sense of them. I had to use 3 more bytes for anti-circumvention, a newline so we can't be inside a single-line comment or a normal quoted string, and a */ so we can't be inside a multi-line comment.

\$\endgroup\$
2
\$\begingroup\$

Ruby, 58 23 27 bytes AND proof of impossibility at bottom

This snippet is valid in any Ruby version prior to Ruby 2.3 (when heredocs were added):


=end
)}end/;[}'"\//;[}#{]}

Old version (invalid):


=end
)}end/;kill(Process.pid,-9)'"\//;kill Process.pid,-9

This cannot be used anywhere in a Ruby program except after __END__.

Proof of impossibility

This solution is impossible in any Ruby version after and including Ruby 2.3.

Any Ruby solution can be inserted into this snippet and function as a valid program:

<<'string'

# Insert code here

string

With this particular snippet, you can add (note leading newline)


string

to your solution in order to invalidate the program. However, changing the "name" of the heredoc will again invalidate your solution. Heredocs can have infinite placeholders, meaning a solution accounting for all of them would be infinitely long. Thus an answer in Ruby 2.3+ is impossible.

Thanks to histocrat for pointing this out.

\$\endgroup\$
  • \$\begingroup\$ Invalid, cannot appear. Needing a kill command means it can be put in a program that still runs \$\endgroup\$ – ASCII-only Jan 24 at 6:14
  • \$\begingroup\$ Can kill be redefined to not work properly? For example define kill to do nothing with the input? \$\endgroup\$ – Wheat Wizard Jan 24 at 13:40
  • \$\begingroup\$ @ASCII-only--yes, that's true. I thought that the question was to raise an error, not disallow a valid program. Sorry about that. Will remove. \$\endgroup\$ – CG One Handed Jan 25 at 0:05
  • 1
    \$\begingroup\$ Not quite--If you put single quotes around the heredoc terminator like so, it won't interpolate with #{. I don't think this is solvable in Ruby due to this feature. \$\endgroup\$ – histocrat Sep 6 at 17:11
  • 2
    \$\begingroup\$ Proof of impossibility is still a valid answer. Though I think the proof might need to be a little more strict \$\endgroup\$ – Jo King Sep 11 at 1:43
1
\$\begingroup\$

S.I.L.O.S, 4 bytes

Silos is competitive \o/


x+

S.I.L.O.S runs on a two pass interpreter / compiler. Before execution a "compiler" attempts to simplify the source into an array describing the sourc Each line is treated separately. x+a is an assignment operator that will add ea to the value of x and store it into x. However the "compiler" will break. Therefore, we take this string and add a new line before and after ensuring it's on its own line and breaks the compiler.

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Why doesn't ax+ error out? \$\endgroup\$ – Erik the Outgolfer Jul 20 '17 at 18:40
  • \$\begingroup\$ undefined compiler behavior @EriktheOutgolfer \$\endgroup\$ – Rohan Jhunjhunwala Jul 20 '17 at 19:27

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.