创建你自己的语言
如果我想创建自己的语言,有什么工具可以帮助我吗? 我听说过 yacc,但我想知道如何在该语言中实现我想要的功能。
If I were looking to create my own language are there any tools that would help me along? I have heard of yacc but I'm wondering how I would implement features that I want in the language.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
密切相关的问题(全部通过在 stackoverflow 上搜索
[compiler]
获得):有关解析器、解释器和编译器的学习资源
学习编写编译器
构建一个简单的解释器
...
以及类似的主题(来自同一搜索):
我们应该了解多少编译器?
用自己的语言编写编译器
...
编辑:我知道与 stackoverflow 相关的问题搜索不是我们想要的,但是
我们真的需要这个主题的第 n 次迭代吗? 嗯!
Closely related questions (all taken by searching on
[compiler]
on stackoverflow):Learning Resources on Parsers, Interpreters, and Compilers
Learning to write a compiler
Constructing a simple interpreter
...
And similar topics (from the same search):
Bootstrapping a language
How much of the compiler should we know?
Writing a compiler in its own language
...
Edit: I know the stackoverflow related question search isn't what we'd like it to be, but
did we really need the nth iteration of this topic? Meh!
我推荐的第一个工具是龙之书。 这是构建编译器的参考。 设计一种语言并不是一件容易的事,实现它更困难。 龙书对此有帮助。 书中甚至参考了标准的unix工具lex和yacc。 GNU 等效工具称为 flex 和 bison。 它们都生成词法分析器和解析器。 还有更现代的工具用于生成词法分析器和解析器,例如对于 java,有 ANTLR(我还记得 javacc 和 CUP,但我自己只使用 ANTLR)。 事实上,ANTLR 结合了解析器和词法分析器,并且提供了 eclipse 插件,这使得它使用起来非常舒适。 但要比较它们、您需要的解析器类型,并了解您需要它们的用途,您应该阅读 Dragon 书。 您还必须考虑其他事情,例如运行时环境、编程范式等。
如果您已经有了一定的设计思想并且需要特定步骤或细节的帮助,那么答案可能会更有帮助。
The first tool I would recommend is the Dragon Book. That is the reference for building compilers. Designing a language is no easy task, implementing it is even more difficult. The dragon book helps there. The book even reference to the standard unix tools lex and yacc. The gnu equivalent tools are called flex and bison. They both generate lexer and parser. There exist also more modern tools for generating lexer and parser, e.g. for java there are ANTLR (I also remember javacc and CUP, but I used myself only ANTLR). The fact that ANTLR combines parser and lexer and that eclipse plugin is availabe make it very comfortable to use. But to compare them, the type of parser you need, and know for what you need them, you should read the Dragon book. There are also other things you have to consider, like runtime environment, programming paradigm, ....
If you have already certain design ideas and need help for a certain step or detail the anwsers could be more helpful.
ANTLR 是一个用 Java 编写的非常好的解析器生成器。 还有一本很棒的书可供参考。
ANTLR is a very nice parser generator written in Java. There's a terrific book available, too.
我喜欢 Flex (Fast Lex) [词法扫描器]
和 Bison (A Hairy Yacc) [又一个编译器编译器]
两者都是免费的,并且可以在所有 *NIX 安装上使用。 对于 Windows,只需安装 cygwin。
但我老派。
通过使用这些工具,您还可以在互联网上找到许多流行语言的 lex 规则和 yacc 语法。 因此,为您提供了一种快速启动和运行的方法,然后您可以随时自定义语法。
示例:算术表达式处理[优先顺序等是一个致命的问题]您可以快速从网络上获取此语法。
另一种可以考虑的方法是编写 GCC 的前端扩展。
这并不简单,但如果你想要一种编译语言,它可以节省代码生成部分的大量工作(你仍然需要了解爱并理解 flex/bison)。
I like Flex (Fast Lex) [Lexical scanner]
and Bison (A Hairy Yacc) [Yet another compiler compiler]
Both are free and available on all *NIX installations. For Windows just install cygwin.
But I old school.
By using these tools you can also find the lex rules and yacc gramers for a lot of popular languages on the internet. Thus providing you with a quick way to get up and running and then you can customize the grammers as you go.
Example: Arithmetic expression handling [order of precedence etc is a done to death problem] you can quickly get the grammer for this from the web.
An alternative to think about is to write a front-end extension to GCC.
Non Trivial but if you want a compiled language it saves a lot of work in the code generation section (you will still need to know love and understand flex/bison).
我从未完成完整的语言,我使用了 rply 和 llvmlite 实现了一种简单的 Foxbase 语言,位于 https://github 中。 com/acekingke/foxbase_compiler
所以如果你想使用 python,
rply
或llvmlite
会很有帮助。如果你想使用 golang,
goyacc
可能有用。 但是您应该通过手动硬编码来编写词法分析器。 或者您可以使用 https://github.com/acekingke/lexergo 来简化它。I never finished the complete language, I had used rply and llvmlite implements a simple foxbase language, in https://github.com/acekingke/foxbase_compiler
so if you want use python,
rply
orllvmlite
is helpful.if you want use golang,
goyacc
maybe useful. But you should write a lexical analyzer by hard coding by hand. Or you can use https://github.com/acekingke/lexergo to simplify it.