# Is it possible to design a language that doesn't have syntax errors? [closed]

In other words, a language where every possible string is valid syntax?

EDIT: This is a theoretical question.
I have no interest in using such a language; I'm just asking whether it's possible.

# Further edit

I went ahead a designed such a language. See ErrorFree

• If we could do that, we'd have created AI. Jan 19, 2011 at 14:07
• @Michael: No; I don't think so. Jan 19, 2011 at 14:10
• !!! Perl !!!
– user1249
Jan 19, 2011 at 15:07
• I strongly object to the question being closed! It's neither subjective nor not constructive!!! Jan 19, 2011 at 20:47
• why not take a assembly language with: exacly 256 instructions, 128 registers, and a general syntax of `instruction operand*`, where an operand may be a register or a number between 0-127 (and everything above that is treated as a register) and if an operand is missing for a multi-arity instruction, '0' is assumed. Jan 19, 2011 at 20:50

Yes, if you look at this in a very analitic way creating a Deterministic Turing Machine that always stops in a good final state for every single string of a certain language, then you'll have demostrated that is possible. The demostration is pretty straight forward, you must a regular TM with a transition function with only one transition, that looks like this:

``````TF(w,q) -> (w,Qa)

Considerations:
L = { w | w is any possible string }
w e L
q e Q
F is a set with all good final states {Qa,Qr}
Qa e F
``````

Its been demostrated that a TM has the same computing power that any single real life computer, so this is absolutely possible

• What on earth does this mean, to us common layfolk? What is 'w', 'e', 'L', 'q', 'Q', 'Qa', 'Qr', 'F', 'TF'. Without any of these defined I have no frame of reference. Jan 20, 2011 at 16:26
• Sorry but there is no easy way to explain the Turing Machine approach to this answer. Check this link to clarify a little: en.wikipedia.org/wiki/Turing_machine Jan 20, 2011 at 17:25

Yes, of course it's possible, it's even trivially easy.

``````<programm> ::= char | char <program> |
``````

I don't understand how anybody can say "no". That said, it might be rather hard to define a meaningful semantic for such a language, but that's possible too. Just look at whitespace.

• So if the language ignores it, it's valid systax? and isn't "tabtabspace" a valid string? Jan 19, 2011 at 14:27
• The semantics of the language were my issue with it, but can't really discuss it without it getting off-topic to philosophy/linguistics proper. Jan 19, 2011 at 14:30
• Michael: Exactly. Everything is syntactically valid, but it can possibly be a NOP (have no special meaning). Nothing wrong with a language ignoring lots of stuff. Just look at all the things C ignores in this sample program: int main() { 3;;; /* comment */ } Jan 19, 2011 at 14:36
• Many people say "no" because they have no conceptional distinction between syntax and semantics. "It doesn't compile? Must be a syntax error, then!" Jan 19, 2011 at 17:40
• Many people say "no" because there is no real meaning in this. As soon as you add structure (i.e. more than a self recursive parsing rule) you have the concept of syntax. Violation of the structure is a syntax induced error. Syntax induced error is a Syntax Error, whether the parser flags it as such or not. Jan 20, 2011 at 16:29

I guess it depends on what you mean by valid syntax.

You could design a language that accepted any string but ignored anything that had not been prescribed specific meaning. This is basically the equivalent of saying "I'll get rid of syntax errors but saying they're not errors" - pretty pointless and hugely undesirable for many reasons.

Beyond that the only way you could have a language which had no syntax errors would be to have every possible string have a valid instruction / use associated with it. The only way I can see to do that would be to have all operations as single characters and to ensure that every single character had an operation assigned to it.

There are a million things wrong with this - obviously there are no reserved words, it's all about where it's used in context and as a result it would be basically illegible and, while immune from syntax errors would be far more likely to experience every other sort of error.

So theoretically possible (AmmoQ puts it far more neatly than I) but entirely undesirable.

• I've read that TECO was like that, each character being assigned a meaning. Jan 19, 2011 at 14:41
• Machine code works pretty much that way. Every possible combination of bytes can be viewed as a program that does something, even if all it does is causing an interrupt. Jan 19, 2011 at 15:19
• David, thats what I was thinking, very TECO like. Although IIRC TECO input could contain syntax errors. But it demonstrates the difficulty of such a dense language -very hard to read and prone to difficult to understand errors. Jan 19, 2011 at 15:22
• @user281377: On the 6502, there are quite a few instructions with no defined meanings. Some have behaviors which are consistent, useful, and not available with any documented instruction (my favorite is nicknamed "DCP"--decrement a memory address and compare the result with the accumulator, setting flags appropriately), but some have behavior that depend upon bus timings in weird and bizarre ways, and some will lock up the processor hard enough to require a reset (even a "non-maskable" interrupt won't help). I think the latter instructions could be considered "syntax errors". May 10, 2014 at 15:02

Code in a non-text based programming language may not have syntax errors.

I am thinking of a visual language like BYOB. You can not accidentally type "if x ten else foo" because the "syntax" is defined by graphical blocks.

The very purpose of syntax is to differentiate between valid and non-valid in a manner that's faster and more effective than executing the code. Syntax is just an optimisation, what goes in it and what goes into semantics is arbitrary.

Usually you want quite the opposite: to make the syntax stretch as far as possible to save more time, but of course you can alsp omit syntax altogether and declare every error a semantic one: you'll end up with a non-tokenizing interpreter.

Ahbefiasdlk aslerhsofa;f jwi [asdfasdf]aew /&Q!@#\$ }{ ;-P

So what does that mean?

As long as the language has structure and grammar, there will always be the concept of a syntax error. The question is whether you enforce it or not. People will make mistakes, and syntax errors are what most language designers reach for to help programmers avoid stupid mistakes.

A syntax error is an error introduced by programmers writing code that has no meaning to the language.

It is impossible to get rid of syntax errors based on the above definition. We've all mispelled identifiers, we've all mispelled method names. Having the language silently accept the mispelling and happily do nothing is not my idea of an enjoyable experience.

It is possible to design a language that can use any valid unicode character (or character sequence) as identifiers. There are challenges, such as normalizing equivalent characters/character sequences so that they are recognized as the same thing--but it's possible. NOTE: there are four standard types of unicode normalization.

• The need for structure does not require a grammar. Consider Piet where the structure is in the position of the character (or color) in a grid, not its relation to other characters in a morpheme. Jan 19, 2011 at 19:03
• Violate the structure and what happens? Jan 19, 2011 at 19:29