Why does a programming language need keywords?

前端 未结 13 861
野性不改
野性不改 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条回答
  •  萌比男神i
    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!

提交回复
热议问题