有没有使用 ANTLR 或类似语言实现的简单语言?
我正在尝试构建一种简单的解释语言以用于学习目的。我读过无数关于 ANTLR 和 JavaCC 的理论和教程,但我不知道如何真正让它做一些有用的事情。
我通过“把东西拆开然后重新组合起来”来学得最好,那么,是否有任何在 ANTLR 或类似工具的帮助下实现的简单语言的工作示例?
像下面这样的东西可能会很好:
x = 1
if x == 1
print "true"
I'm trying to build a simple interpreted language for learning purposes. I've read countless theory and tutorials on ANTLR and JavaCC, but I can't figure out how to actually make it do something useful.
I learn best by "taking something apart and putting it back together again", so, are there any working examples of simple languages implemented with the help of tools such as ANTLR, or similar?
Something like the following might be nice:
x = 1
if x == 1
print "true"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
注意:您可以使用另一种方法并查看 XText 来生成所有给你的代码。
来自 XText 文档:
生成所有部件后,您可以根据需要分析它们(并测试它们)。
替代文本 http://www.eclipse.org/ Xtext/documentation/0_7_2/images/getting-started-editor.png
Note: you could use another approach and check out XText for generating all the code for you.
From the XText documentation:
Once all the parts generated, you can analyze them (and test them) as you want.
alt text http://www.eclipse.org/Xtext/documentation/0_7_2/images/getting-started-editor.png
为什么不为与您的示例相匹配的非常简单的语言定义语法并尝试实现它?使用 ANTLR 创建解析树,然后弄清楚如何“评估”该树。首先,不必担心声明或数据类型(每个值都是整数)并支持 3 种语句类型:赋值、if 语句和 print。
Why don't you define a grammar for a really simple language which matches your example and try to implement it? Use ANTLR to create the parse tree, and then figure out how to "evaluate" the tree. To start with, don't bother with declarations or data types (every value is an integer) and support 3 statement types: assignment, if statement and
print
.[无耻插件] 为什么不买我的语言实现模式书呢?我拥有组合语言所需的所有组件,包括多个口译员等......
[shameless plug] Why not buy my Language Implementation Patterns book? I have all the pieces you need to put together a language including multiple interpreters etc...
你可能可以在 Google Code 和 Github 上找到很多小语言。
这是几年前我在 ANTLR 中作为课堂项目完成的一种玩具语言:
http://code.google.com/p/bcis/
You can probably find lots of small languages on Google Code and Github.
Here's a toy language that I did in ANTLR as a class project a few years ago:
http://code.google.com/p/bcis/
Scott "JavaDude" Stanchfield 关于 ANTLR3 的视频教程系列非常不错。
当然,还有 StackOverflow,其中有大量 ANTLR 内容。例如:
Scott "JavaDude" Stanchfield's video tutorial series on ANTLR3 is pretty good.
And of course, there is StackOverflow, which has tons of ANTLR content. For example: