I suppose the first step is to choose a language (or at least style of language). An easy one to start with is the style of Forth, where each "word" in the source code is handled in exactly the same way. This makes lexical analysis and parsing nearly trivial.
Another small language to start with is Scheme. The parsing is a bit more involved because it involves recursion, but it's still tractable. You can find various examples of Scheme-in-Python around the web, it's been done lots of times.
Writing a scripting language will probably exceed 'learn[ing] a bit'. I think it's too much. But if you are serious enough, you might want to start by playing around with racc or PLY, YACC bindings for ruby and python, respectively.
If it is just as a learning exercise, I would recommend reading through this tutorial on how to make a s*** javascript interpreter in python. Javascript can be very basic, and it explains things at a very beginner level.
发布评论
评论(4)
我想第一步是选择一种语言(或者至少是语言的风格)。一种容易上手的风格是 Forth 的风格,其中源代码中的每个“单词”都以完全相同的方式处理。这使得词法分析和解析几乎变得微不足道。
另一种可以开始使用的小语言是 Scheme。解析有点复杂,因为它涉及递归,但它仍然很容易处理。您可以在网络上找到各种 Python 方案的示例,这种方法已经被做过很多次了。
I suppose the first step is to choose a language (or at least style of language). An easy one to start with is the style of Forth, where each "word" in the source code is handled in exactly the same way. This makes lexical analysis and parsing nearly trivial.
Another small language to start with is Scheme. The parsing is a bit more involved because it involves recursion, but it's still tractable. You can find various examples of Scheme-in-Python around the web, it's been done lots of times.
编写脚本语言可能会超出“学习一点”的范围。我觉得太多了。但如果你足够认真,你可能想从使用 racc 或 PLY,分别用于 ruby 和 python 的 YACC 绑定。
Writing a scripting language will probably exceed 'learn[ing] a bit'. I think it's too much. But if you are serious enough, you might want to start by playing around with racc or PLY, YACC bindings for ruby and python, respectively.
如果只是作为学习练习,我建议阅读 如何在Python中制作as*** JavaScript解释器。 JavaScript 可以是非常基础的,它可以在非常初级的水平上解释事物。
If it is just as a learning exercise, I would recommend reading through this tutorial on how to make a s*** javascript interpreter in python. Javascript can be very basic, and it explains things at a very beginner level.
画出一个关于你的语言如何工作的有限状态自动机,编写一个语法分析器,画一些图表。继续破解!
draw out a finite state automata of how your language is going to work, write a syntax analyzer, draw some diagrams. Hack on!