<p>While I know how to use lex&yacc and I have a relatively good knowledge on the subject on various langs, I do not manage to get information on how to create a parser with lex&yacc in Go. The only sources I found are:<a href="https://golang.org/cmd/yacc/">https://golang.org/cmd/yacc/</a> and also a question in stackoverflow:<a href="http://stackoverflow.com/questions/8422146/go-how-to-create-a-parser">http://stackoverflow.com/questions/8422146/go-how-to-create-a-parser</a> The first link does not provide many details. The second is relative old (4 years at the time of writing).
Any help would be greatly appreciated</p>
<hr/>**评论:**<br/><br/>pdq: <pre><p>Watch <a href="https://www.youtube.com/watch?v=HxaD_trXwRE">Lexical Scanning in Go</a>, by Rob Pike</p></pre>zemo: <pre><p>I wrote my config language <a href="https://github.com/jordanorelli/moon">moon</a> using this technique. That video only talks about the lexing step. The lex entry point is <a href="https://github.com/jordanorelli/moon/blob/master/lex.go#L200-L208">here</a> and the parse entry point is <a href="https://github.com/jordanorelli/moon/blob/master/parse.go#L157-L167">here</a>.</p></pre>eatonphil: <pre><p><a href="https://github.com/jcla1/gisp" rel="nofollow">Gisp</a> is a Lisp written in Go using this video's techniques.</p></pre>Phovox: <pre><p>Gisp is amazing!! It is impressive, the examples are definitely great and this goes far beyond the scope of my current project! Great link, thanks a lot!</p></pre>purpleidea: <pre><p>Great talk on lex-ing, but the parsing step is completely missing. I'd like more information on this part too.</p></pre>ngaut: <pre><p>There is a complete SQL parser in TiDB: <a href="https://github.com/pingcap/tidb/tree/master/parser">https://github.com/pingcap/tidb/tree/master/parser</a></p></pre>Phovox: <pre><p>Cool as well, ... ! Thanks</p></pre>mike_hman: <pre><p><a href="https://crypto.stanford.edu/%7Eblynn/nex/" rel="nofollow">Nex</a> is a good replacement for lex in Go. </p>
<p>I've used Nex and Go Yacc in a university project to build an interpreter for a simple subset of javascript. Its performance ended up being about 2-3 times slower than an almost identical interpreter in C (Go and C map very well to one-another, and I actually started the project in C). Its probably because it wasn't optimized for specific Go idioms very well. </p>
<p>You can check out <a href="https://github.com/mhoc/msp" rel="nofollow">my code here</a> if you want somewhere to start. And if you're really interested in the specific language it is compiling, which was for a project and honestly a completely terrible programming language, there <a href="https://github.com/mhoc/cs352-test" rel="nofollow">are test cases in this repo</a>. </p></pre>Phovox: <pre><p>Right now this is my preferred option, so thanks a lot for the link. I was not aware of this and it really looks nice! Thanks</p></pre>james-h: <pre><p>I've written <a href="https://github.com/JamesOwenHall/json2" rel="nofollow">a JSON parser</a> from scratch that you can use as an example as well.</p></pre>Phovox: <pre><p>Oh, wow! But you wrote both the scanner and the parser right from scratch ... I mean you use no particular tool for that work ... right?</p></pre>james-h: <pre><p>That's right. I didn't use any third party libraries or parser generators. This might not be necessarily what you're looking for, but I think it's relevant nonetheless.</p></pre>everdev: <pre><p>Handwritten parser and lexer tutorial: <a href="http://blog.gopheracademy.com/advent-2014/parsers-lexers/">http://blog.gopheracademy.com/advent-2014/parsers-lexers/</a></p>
<p>This helped me a lot in my project. Then, if the syntax is similar enough to Go, you can use Go's text/scanner package: <a href="http://golang.org/pkg/text/scanner/">http://golang.org/pkg/text/scanner/</a></p></pre>Phovox: <pre><p>This intro is definitely cool as well! Believe it or not I was not aware either :( of the package text/scanner :( Shame on me! Thanks a lot for the links</p></pre>dchapes: <pre><p>See Adam Presley's "Writing a Lexer and Parser in Go"</p>
<ul>
<li><a href="https://www.reddit.com/r/golang/comments/32emo1/writing_a_lexer_and_parser_in_go_part_1/">https://www.reddit.com/r/golang/comments/32emo1/writing_a_lexer_and_parser_in_go_part_1/</a></li>
<li><a href="https://www.reddit.com/r/golang/comments/35sb63/writing_a_lexer_and_parser_in_go_part_2/">https://www.reddit.com/r/golang/comments/35sb63/writing_a_lexer_and_parser_in_go_part_2/</a></li>
<li><a href="https://www.reddit.com/r/golang/comments/386bt7/writing_a_lexer_and_parser_in_go_part_3/">https://www.reddit.com/r/golang/comments/386bt7/writing_a_lexer_and_parser_in_go_part_3/</a></li>
</ul></pre>Phovox: <pre><p>Goodness! I swear I search in reddit for other related threads and I found none! Thanks a lot really!!</p></pre>calebdoxsey: <pre><p>You can use peg too: <a href="https://github.com/pointlander/peg" rel="nofollow">https://github.com/pointlander/peg</a>.</p>
<p>Here's an example grammar: <a href="https://github.com/pointlander/peg/blob/master/grammars/calculator/calculator.peg" rel="nofollow">https://github.com/pointlander/peg/blob/master/grammars/calculator/calculator.peg</a>.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传