|
|
|
|
|
by Darmani
5155 days ago
|
|
The reason for using semicolons in JS is quite simple: It makes it easy to determine the boundaries between statements, both for humans and machines. With semicolons, the search for the boundary between statements reduces to the search for that single character -- that means fast code skimming. The boundary is also robust against changes; as long as that semicolon stays in place, nothing that happens within that statement can cause that boundary to become an interior. There is an exception: Strings. This is no trouble for machines, as strings are processed by the lexer. Humans can't instantly take a string and process it as a chunk, but a UI innovation does this work for us: syntax highlighting. As an example, consider the code: x = a && b
foo()
If I try to change the "b", I might accidentally turn the code into x=a &&
foo()
The grammar allows the malformed statement to interact with its neighbor, and thus, instead of getting a parser error, I'll get a program bug.LR parsers take advantage of this for error recovery, allowing them to find more than one parse error at a time. A more sophisticated view is to think about syntax abstractions. Given a program like "x=y; return x;" we can create a program written in the abstract syntax "statement; statement". (The dual operation is to concretized that back into the set of all two-statement programs. This kind of relationship is known a Galois connection.) Having semicolons makes the abstract syntax unambiguous, allowing us to efficiently reason at the statement level. |
|