dsieyx2015 2015-01-27 21:06
浏览 7
已采纳

如何检测语句的结尾?

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?

  • 写回答

2条回答 默认 最新

  • douzi3756 2015-01-27 21:17
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'