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.
If we are speaking of C++ - it already has very complicated grammar. Allowing to use keywords as variable names, for example, will make it even more complicated.
I guess it look very weird if not impossible to write the parser. E.g
It's not necessary -- Fortran didn't reserve any words, so things like:
are complete legal. This not only makes the language hard for the compiler to parse, but often almost impossible for a person to read or spot errors. for example, consider classic Fortran (say, up through Fortran 77 -- I haven't used it recently, but at least hope they've fixed a few things like this in more recent standards). A Fortran DO loop looks like this:
Without them being side-by-side, you can probably see how you'd miss how this was different:
Unfortunately, the latter isn't a DO loop at all -- it's a simple assignment of the value
1.10
to a variable namedDO 10 I
(yes, it also allows spaces in a name). Since Fortran also supports implicit (undeclared) variables, this is (or was) all perfectly legal, and some compilers would even accept it without a warning!several reasons:
The keywords may seem unambiguous in your samples. But that is not the only place you would use the variable 'break' or the variable 'for'.
writing the parser would be much harder and error prone for little gain.
using a keyword as a function or procedure name in a library may have undesired, possibly security relevant, side effects.
Depending on the language definition a compiler may or may not need keywords. When it does not know what to do it can try to apply precedence rules or just fail.
An example:
What happens if a is greater than 2?
You can define a language which dosn't use keywords. You can even define a language which alowes you to replace all symbols (since they are only very short keywords themselfes).
The problem is not the compiler, if your specification is complete and error free it will work. The problem is PEBCAD, programs using this feature of the language will be hard to read as you have to keep track of the symbol definitions.
Because we want to keep what little sanity points we've got: