For example (in C):
int break = 1;
int for = 2;
Why will the compiler have any problems at all in deducing that break
and for
are variables here?
So, we need keywords because
- we want the programs to be readable
- we do not want to over-complicate the job of already complex compilers of today
- but most importantly, a language is lot more powerful if some 'key'words are reserved for some special actions. Then, the language can think of being useful at a higher level rather than dying in trying to implement a for loop in an unambiguous way.
They don't. PL/1 famously has no keywords; every "keyword" (BEGIN, DO, ...) can also be used a variable name. But allowing this means you can write really obscure code: IF DO>BEGIN THEN PRINT:=CALL-GOTO; Reserving the "statement keywords" as the language isn't usually a loss if that set of names is modest (as it is in every langauge I've ever seen except PL/1 :-).
APL also famously has no keywords. But it has a set of some 200 amazing iconic symbols in which to write complicated operators. (the "domino" operator [don't ask!] is a square box with a calculator divide sign in the middle) In this case, the langauge designers simply used icons instead of keywords. The consequence is that APL has a reputation of being a "write only" language.
Bottom line: not a requirement, but it tends to make programs a lot more readable if the keywords are reserved identifiers from a small set known to the programmers. (Some langauges has insisted that "keywords" start with a special punctuation character like "." to allow all possible identifiers to be used, but this isn't worth the extra trouble to type or the clutter on the page; its pretty easy to stay away from "identifiers" that match keywords when the keyword set is small).
The compiler would have problems if you write something like this:
Is that a loop or a call to a function named
while
? Did you want to return the value 5 from the current function, or did you want to call a function namedreturn
?It often simplifies things if constructs with special meaning simply have special names that can be used to unambiguously refer to them.
As others said, this makes compiler parsing your source code easier. But I would like to say a bit more: it can also make your source code more readable; consider this example:
if (if > 0) then then = 10 end if
The second "if" and the second "then" are variables, while others are not. I think this kind of code is not readable. :)
Then what will the computer do when it comes across a statement like:
Should it actually break? Or should it treat it as
1;
?The language would become ambiguous in certain cases, or you'd have to create a very smart parser that can infer subtle syntax, and that's just unnecessary extra work.
In many cases, it would be possible for the compiler to interprete keywords as normal identifiers, like in your example:
As a matter of fact, I just wrote a compiler for a simple assembly-like toy language which does this, but warns the user in such cases.
But sometimes the syntax is defined in a way that keywords and identifiers are ambiguous:
And the most obvious reason is that editors will emphasize keywords so that the code is more readable for humans. Allowing keywords to be treated as identifiers would make code highlighting harder, and would also lead to bad readability of your code.
Since it's tagged C, the original C language was such that by default any variable was defined as type
int
.It means that
foo;
would declare a variable of typeint
.Let's say you do
break;
. So how does the compiler know whether you want to declare a variable namedbreak
or use the keywordbreak
?