How to write a _parser_ for a declarative language in golang

agolangf · · 238 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi reddit,</p> <p>I&#39;m planning on writing a declarative programming language. I&#39;d like to write it in golang. After reading a bunch of things on the internet, I think I&#39;m comfortable writing a lexer, but I have no idea how to go about writing the <em>parser</em>. Can anyone recommend some starting points?</p> <p>As a secondary goal, I want to be able to create a tool like gofmt for my language. If I could have a single source of truth (some grammar) for the language, this would be great. Not sure how this sort of things is done and if this makes sense.</p> <p>Thanks!</p> <p>PS: Sorry I forgot the trailing question mark in the title!</p> <p><strong>EDIT:</strong> thanks everyone for their answers, I&#39;ve got a lot of reading and research to do into a lot of your advice, I&#39;ll do my homework and come back if I get stuck! Cheers!</p> <hr/>**评论:**<br/><br/>postman_: <pre><p><a href="https://en.m.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools" rel="nofollow">The Dragon Book</a> has everything you need. You might be interested in LL- and LR-parsing in particular. Go has yacc somewhere in its standard distribution.</p></pre>Growlizing: <pre><p>The Dragon Book all the way.</p></pre>purpleidea: <pre><p>I&#39;ll check it out, thanks! Do you know if it has a section about declarative languages in particular?</p></pre>Growlizing: <pre><p>Been a while since I read it so I can&#39;t remember. I do recall I found this course reeally helpful though: <a href="http://web.stanford.edu/class/cs143/" rel="nofollow">http://web.stanford.edu/class/cs143/</a></p></pre>purpleidea: <pre><p>Can you elaborate on how I would use yacc to accomplish my goal of a declarative language? Thanks!</p></pre>postman_: <pre><p>You would use it for LALR(1) syntax parsing. It doesn&#39;t matter if the language is declarative or imperative. Just skim through the book.</p></pre>kpmy: <pre><p>Also, there is no difference between parser for imperative language vs. parser for declarative language. All way down to AST (abstract syntax tree) it will look similar. Difference will show itself on semantics of nodes and virtual machine/code generator implementation.</p></pre>purpleidea: <pre><p>This was helpful, thanks!</p></pre>kpmy: <pre><p>Read the N. Wirth&#39;s Compiler Construction. Also here is my vision of declarative language written in golang <a href="https://github.com/kpmy/lomo" rel="nofollow">https://github.com/kpmy/lomo</a></p></pre>aklfu: <pre><p><a href="https://github.com/cznic/pl0" rel="nofollow">Starting point</a></p></pre>purpleidea: <pre><p>Thanks for the link, but this looks like an imperative language, and I&#39;m particularly interested in how the declarative aspect would be different... Any more pointers? Thanks!!</p></pre>rco8786: <pre><p>It&#39;s not really any different. </p></pre>everdev: <pre><p>Handwritten parser and lexer tutorial: <a href="http://blog.gopheracademy.com/advent-2014/parsers-lexers/" rel="nofollow">http://blog.gopheracademy.com/advent-2014/parsers-lexers/</a> This helped me a lot in my project. Then, if the syntax is similar enough to Go, you can use Go&#39;s text/scanner package: <a href="http://golang.org/pkg/text/scanner/" rel="nofollow">http://golang.org/pkg/text/scanner/</a></p></pre>purpleidea: <pre><p>Cool link, I&#39;ll look into it, thanks!</p></pre>
238 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传