Ambiguity is the primary reason, as others have explained. I would like to also add that in a language without reserved keywords, it's very easy to shadow basic constructs, like if
. This code works in Bash (but not when in POSIX mode):
$ alias if=echo
$ if true
true
$ if true; then echo yes; fi
bash: syntax error near unexpected token `then'
$ unalias if
$ if true; then echo yes; fi
yes
Instead of the fully reserved keywords, one way to introduce keywords without breaking old code is Contextual Keywords.
Basically you introduce keywords that can only be used in places where normal identifiers cannot. For example, C# did that with newer keywords, so did Kotlin, but JavaScript did the opposite and allowed all keywords in certain places like dot-notation and object literals (eg, both x = { if: 1 }
and x.if
became legal).
You can also provide a "quoting mechanism" to allow reserved words (or, depending on your use case, even illegal characters, including punctuation, spaces, and even newlines) to be used as identifiers: C#, VB.NET, F#, and Rust, and Kotlin, and SQL has "delimited identifiers", and others. This general trick is called Stropping.
if if = then then then = else else else = if
floating around in the comments here. I'll let someone more familiar with those languages write a full answer. $\endgroup$<stdbool.h>
. The actual keywords were created using names previously reserved for future use (beginning with_
). $\endgroup$bool
true
false
are keywords. $\endgroup$