在 Erlang 中创建 AST 节点
我正在玩 Erlang,我正在尝试编写一个简单的算术解析器。
我想尝试解析以下表达式:
((12+3)-4)
我想将表达式解析为一堆 AST 节点。在解析这个表达式时,我首先会为 (12+3) 表达式创建一个二进制表达式,它在 C# 中看起来像这样:
var binaryStructure = new BinaryStructure();
binaryStructure.Left = IntegerLiteralExpression(12);
binaryStructure.Right = IntegerLiteralExpression(4);
binaryStructure.Operator = binaryExpression.Operator != BinaryOperatorType.Addition;
我对 Erlang 很陌生,我想知道如何创建一个像这样的结构在 Erlang 中,我可以将其放在一个列表中,将其用作表达式堆栈。
谁能建议如何创建这样的树状结构?函数是否合适?
I am playing about with Erlang and I am trying to write a simple arithmetic parser.
I want to try and parse the following expression:
((12+3)-4)
I want to parse the expression into a stack of AST nodes. When parsing this expression, I would first of all create a binary expression for the (12+3) expression which would look something like this in C#:
var binaryStructure = new BinaryStructure();
binaryStructure.Left = IntegerLiteralExpression(12);
binaryStructure.Right = IntegerLiteralExpression(4);
binaryStructure.Operator = binaryExpression.Operator != BinaryOperatorType.Addition;
I am quite new to Erlang and I am wondering how I would go about creating a structure like this in Erlang that I can place on a List that I would use as the stack of expressions.
Can anyone suggest how to create such a tree like structure? Would a function be a good fit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在像 Erlang 这样的函数式语言中,它要简单得多。只是
以更抽象的方式
In functional language like Erlang it is far simpler. Just make it
In more abstract way
另外,请查看
stdlib
应用程序中的erl_parse.erl
模块。从
mkop
函数读取:Also, have a look to the
erl_parse.erl
module in thestdlib
application.Reading from to the
mkop
function: