Why does a programming language need keywords?

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

    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?

    0 讨论(0)
  • 2021-02-01 03:36

    It's not necessary -- Fortran didn't reserve any words, so things like:

    if if .eq. then then if = else else then = if endif
    

    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:

    DO 10 I = 1,10
    

    Without them being side-by-side, you can probably see how you'd miss how this was different:

    DO 10 I = 1.10
    

    Unfortunately, the latter isn't a DO loop at all -- it's a simple assignment of the value 1.10 to a variable named DO 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!

    0 讨论(0)
  • 2021-02-01 03:37

    FWIW, Tcl doesn't have any reserved words. You can have variables and functions named "if", "break", etc. The interpretation of a token is totally dependent on the context. The same token can represent a command in one context, a variable in another, or a literal string in another.

    0 讨论(0)
  • 2021-02-01 03:38

    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.

    0 讨论(0)
  • 2021-02-01 03:39

    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.

    0 讨论(0)
  • 2021-02-01 03:44

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

    0 讨论(0)
提交回复
热议问题