如何检测语句的结尾?

huangapple go评论82阅读模式
英文:

How end of the statement is detected?

问题

我不了解Go语言,我只是在学习各种语言的语法。

根据Go的常见问题解答(FAQ)中所述:“Go借鉴了BCPL的一个技巧:在形式语法中,用于分隔语句的分号是由词法分析器自动注入的,无需前瞻,在任何可能是语句结束的行的末尾注入分号。”

我想知道这是如何实现的,我查看了lex.go,但可能我对Go了解不够(实际上了解很少),没有找到任何关于“语句”或“分号”的参考。

那么,你是如何在词法分析阶段检测到有效语句的结束而无需前瞻的呢?

英文:

I don't know Go, I am just studying various languages syntax.

From Go FAQ: "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."

I wonder how it is done, I took a look at lex.go but maybe I don't know Go enough (very little actually) but I didn't find any reference to "statement" or "semicolon".

So – how can you detect at lexer stage end of the valid statement without even lookahead?

答案1

得分: 5

你可以查看语言规范:

正式的语法使用分号 ";" 作为许多产生式的终止符号。Go程序可以使用以下两个规则省略大部分这些分号:

当输入被分解为标记时,如果行的最后一个标记是:

  • 标识符
  • 整数、浮点数、虚数、符文或字符串字面量
  • 关键字 break、continue、fallthrough 或 return
  • 运算符和分隔符 ++、--、)、] 或 }

那么分号会自动插入到标记流的末尾,作为非空行的结束符号。

为了允许复杂语句占据一行,可以在闭合的 ")" 或 "}" 前省略分号。

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

英文:

You can look in the language specification:

> 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

答案2

得分: 2

Go解析器根据Go语法识别句子结构(例如语句、表达式)。解析器使用扫描器(词法分析器)生成的标记。扫描器会自动将分号插入标记流中,因此解析器没有额外的工作量。分号插入的代码可以在Go扫描器的这里找到。

Go语言规范定义了扫描器如何插入分号,规则如下:

分号

形式化语法在许多产生式中使用分号“;”作为终结符号。根据以下两个规则,Go程序可以省略大多数这些分号:

  1. 当输入被分解为标记时,如果行的最后一个标记是:

    • 标识符
    • 整数、浮点数、虚数、符文或字符串字面量
    • 关键字break、continue、fallthrough或return之一
    • 运算符和分隔符++、--、)、]或}
      则在非空行的末尾自动将分号插入标记流中。
  2. 为了允许复杂语句占据一行,可以在闭合的“)”或“}”之前省略分号。

英文:

Go parser recognizes the sentence structures (e.g., statements, expressions) according to the Go grammar. Parser uses tokens produced by the scanner (lexical analyzer).

Semicolon is automatically inserted into the token stream by scanner, therefore, there is no extra workload for parser. The semicolon insertion code can be found here in Go scanner.

Go language specification defines how the scanner inserts semicolon as follow;

> 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 "}".

huangapple
  • 本文由 发表于 2015年1月28日 05:06:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/28179995.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定