Why does a programming language need keywords?

前端 未结 13 706
野性不改
野性不改 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:50

    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:

    void return(int i){printf("%d",i);}
    public int foo(int a)
    {
      if(a > 2)return (a+1)*2;
      return a + 3;
    }
    

    What happens if a is greater than 2?

    • The language specification may require the compiler to fail
    • The language specification may require the compiler use the return function
    • The language specification may require the compiler to return

    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.

提交回复
热议问题