How is no semicolon achieved in GO language(grammar speciifcally)?

blov · 2017-04-15 20:00:14 · 772 次点击    
这是一个分享于 2017-04-15 20:00:14 的资源,其中的信息可能已经有所发展或是发生改变。

Ok so i know go automatically inserts a semicolon if the last token in a input line is an identifier,a basic literal such as a number or string constant, or one of the tokens : break continue fallthrough return ++ -- ) }. Is this implemented in lexers grammar or parsers grammar?(i know lexer breaks the input into tokens). I want to see the grammar where this is implemented but i can't find any links for the lexer grammar. The go specification (https://golang.org/ref/spec) i believe has mostly parsers grammar and code examples. i am implementing a small language using antlr and bits of java and go grammar and i would like to omit the use of semicolon, how would i go about doing this? thanks for the help.


评论:

dragonrider22:

On mobile, but here's some starting points. The go/scanner package is good to look at as well as the go/parser package. The scanner struct has a Boolean field to determine if a semicolon needs to be added. This is set based on the token. You can see this at the bottom of the https://github.com/golang/go/blob/master/src/go/scanner/scanner.go file.

sseth:

The trick is to have the lexer inject semicolons at the end of lines only after certain types of tokens. In go you inject if the line ends in an identifier, integer, or right paren, etc.

Maybe think of a few places where go omits the semicolon and think about what type of token is at the end of a line.

For lexing I'd highly recommend watching Rob Pike's presentation. Also take a look at the text/template package. You will see the technique he's talking about put into practice there. https://www.youtube.com/watch?v=HxaD_trXwRE

For lexing, you may or may not want to use a code generator. You can write a very nice lexer without one.

hobbified:

Grammar is what parsers do, not lexers.


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

772 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传