Python 解释器:创建自己的编程语言?
请记住,这是使用 python。 好吧,今天我正在摆弄我制作的一个名为 Pyline 的应用程序。它是一个类似命令行的界面,具有一些很酷的功能。不过,我在做的时候有一个想法:既然它像一个“操作系统”,那它就不会有自己的语言吗?
嗯,我在网上看到了一些关于如何制作解释器、解析器和编译器的文章,但对我来说并不是真正可读。我看到的只是一堆垃圾代码。我是那些需要评论或自述文件或某种形式或与用户沟通而无需代码本身的人之一,所以我认为 Stack Overflow 对于像我这样的青少年来说非常有用。我可以寻求帮助吗?
Remember, this is using python.
Well, I was fiddling around with an app I made called Pyline, today. It is a command line-like interface, with some cool features. However, I had an idea while making it: Since its like a "OS", wont it have its own language?
Well, I have seen some articles online on how to make a interpreter, and parser, and compiler, but it wasn't really readable for me. All I saw was a crapload of code. I am one of those guys who need comments or a readme or SOME form or communication towards the user without the code itself, so I think that Stack Overflow would be great for a teenager like me. Can I get some help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了真正创建一种编程语言,您首先需要一些基础知识。
我强烈建议您拿起一本编程语言语用学,它非常可读(比龙书多得多)并且适合自学。
一旦您准备好开始使用解析器,ANTLR 就是“黄金”标准对于解析器生成器的可用性而言(尽管 flex+bison/yacc 非常有能力)。
You need some grounding first in order to actually create a programming language.
I strongly suggest picking up a copy of Programming Language Pragmatics, which is quite readable (much more so than the Dragon book) and suitable for self study.
Once you are ready to start messing with parsers, ANTLR is the "gold" standard for parser generators in terms of usability (though flex+bison/yacc are quite capable).
我刚刚使用了 Xtext,一个语言开发框架。也许这就是您想要查看的内容。
考虑到 Python,您可能会发现实现 Logo 版本很有帮助。如果您愿意,您现在可以跳过解析/词法分析阶段,并首先提出一个面向对象的版本,以便您在 OOP 技能达到要求的情况下继续前进。稍后你可以将它与一些图形库连接起来来实际绘制一些东西。
除了 Logo 之外,您可能还想看看 L-systems。特别请参阅植物的算法之美以获取灵感。
I just came by Xtext, a language development framework. Perhaps that's something you might want to take a look at.
Considering Python you might find it instructive to implement a version of Logo. If you want, you can skip the parsing/lexing stage for now and come up with a object oriented version first to get you going if your OOP skills are up to it. Later on you can hook it up with some graphics library to actually draw something.
In addition to Logo you might want to check out L-systems. See particularly The Algorithmic Beauty of Plants for inspiration.
就像戏剧一样,我建议从一本关于这个主题的好书开始。我绝对可以推荐 Terence Parr(ANTLR 背后的人,一个通用的解析器生成器)。
Like theatrus, I'd suggest starting with a good book on the subject. I can definitely recommend Language Implementation Patterns by Terence Parr (the man behind ANTLR, a common parser generator).
请参阅 Peter Norvig 的 2 页 Python 中的方案解释器,其中有大量解释。那里还链接了一个更高级的版本,一旦您掌握了更简单的版本,就值得一读。
See Peter Norvig's Scheme interpreter in 2 pages of Python with plenty of explanation. There's also a fancier version linked from there, worth reading once you've grokked the simpler one.