如何检测语句结束?

How end of the statement is detected?

我不会Go,我只是在研究各种语言的语法

来自 Go 常见问题解答:"Go borrows a trick from BCPL: the semicolons that separate statements are in the formal grammar but are injected automatically, without lookahead, by the lexer at the end of any line that could be the end of a statement."

我想知道它是如何完成的,我看了一下 lex.go 但也许我对 Go 了解不够(实际上很少)但我没有找到对 "statement" 的任何参考或 "semicolon".

那么 – 你如何在词法分析器阶段检测到有效语句的结束而不需要先行?

您可以查看语言规范:

The formal grammar uses semicolons ";" as terminators in a number of productions. Go programs may omit most of these semicolons using the following two rules:

When the input is broken into tokens, a semicolon is automatically inserted into the token stream at the end of a non-blank line if the line's final token is

  • an identifier
  • an integer, floating-point, imaginary, rune, or string literal
  • one of the keywords break, continue, fallthrough, or return
  • one of the operators and delimiters ++, --, ), ], or }

To allow complex statements to occupy a single line, a semicolon may be omitted before a closing ")" or "}".

http://golang.org/ref/spec#Semicolons

Go 解析器根据 Go 语法识别句子结构(例如语句、表达式)。解析器使用扫描器(词法分析器)生成的标记。

分号由扫描器自动插入到令牌流中,因此,解析器没有额外的工作量。分号插入代码可以在Go scanner中找到here

Go 语言规范定义了扫描器如何插入分号,如下所示;

Semicolons

The formal grammar uses semicolons ";" as terminators in a number of productions. Go programs may omit most of these semicolons using the following two rules:

  1. When the input is broken into tokens, a semicolon is automatically inserted into the token stream at the end of a non-blank line if the line's final token is

    • an identifier
    • an integer, floating-point, imaginary, rune, or string literal
    • one of the keywords break, continue, fallthrough, or return
    • one of the operators and delimiters ++, --, ), ], or }
  2. To allow complex statements to occupy a single line, a semicolon may be omitted before a closing ")" or "}".