Why does a programming language need keywords?

2019-03-09 16:43发布

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.

13条回答
疯言疯语
2楼-- · 2019-03-09 17:23

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).

查看更多
一夜七次
3楼-- · 2019-03-09 17:23

The compiler would have problems if you write something like this:

while(*s++);
return(5);

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 named return?

It often simplifies things if constructs with special meaning simply have special names that can be used to unambiguously refer to them.

查看更多
爷的心禁止访问
4楼-- · 2019-03-09 17:24

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. :)

查看更多
太酷不给撩
5楼-- · 2019-03-09 17:25

Then what will the computer do when it comes across a statement like:

while(1) {
  ...
  if (condition)
    break;
}

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.

查看更多
Deceive 欺骗
6楼-- · 2019-03-09 17:27

In many cases, it would be possible for the compiler to interprete keywords as normal identifiers, like in your example:

int break = 1;
int for = 2;

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:

int break;

while(...)
{
    break; // <-- treat this as expression or statement?
}

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.

查看更多
淡お忘
7楼-- · 2019-03-09 17:31

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 type int.

Let's say you do break;. So how does the compiler know whether you want to declare a variable named break or use the keyword break?

查看更多
登录 后发表回答