Why does a programming language need keywords?

前端 未结 13 842
野性不改
野性不改 2021-02-01 03:25

For example (in C):

int break = 1;
int for = 2;

Why will the compiler have any problems at all in deducing that break and fo

13条回答
  •  渐次进展
    2021-02-01 03:49

    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.

提交回复
热议问题