gpt4 book ai didi

go - 如何检测语句结束?

转载 作者:IT王子 更新时间:2023-10-29 01:50:22 24 4
gpt4 key购买 nike

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

来自 Go FAQ:“Go 借用了 BCPL 的一个技巧:分隔语句的分号在正式语法中,但在可能是语句结尾的任何行的末尾由词法分析器自动注入(inject),没有前瞻性”

我想知道它是如何完成的,我查看了 lex.go 但也许我对 Go 的了解不够(实际上很少)但我没有找到任何对“语句”的引用"或 "分号"。

那么,您如何在词法分析器阶段检测到有效语句的结尾而无需先行查看?

最佳答案

你可以看看语言规范:

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 - 如何检测语句结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28179995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com