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

blov · · 419 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>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&#39;t find any links for the lexer grammar. The go specification (<a href="https://golang.org/ref/spec" rel="nofollow">https://golang.org/ref/spec</a>) 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.</p> <hr/>**评论:**<br/><br/>dragonrider22: <pre><p>On mobile, but here&#39;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 <a href="https://github.com/golang/go/blob/master/src/go/scanner/scanner.go" rel="nofollow">https://github.com/golang/go/blob/master/src/go/scanner/scanner.go</a> file.</p></pre>sseth: <pre><p>The trick is to have the lexer inject semicolons at the end of lines <em>only</em> after certain types of tokens. In go you inject if the line ends in an identifier, integer, or right paren, etc.</p> <p>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.</p> <p>For lexing I&#39;d highly recommend watching Rob Pike&#39;s presentation. Also take a look at the text/template package. You will see the technique he&#39;s talking about put into practice there. <a href="https://www.youtube.com/watch?v=HxaD_trXwRE" rel="nofollow">https://www.youtube.com/watch?v=HxaD_trXwRE</a></p> <p>For lexing, you may or may not want to use a code generator. You can write a very nice lexer without one.</p></pre>hobbified: <pre><p>Grammar is what parsers do, not lexers.</p></pre>

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

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