简体   繁体   English

在Erlang中创建AST节点

[英]Creating an AST node in Erlang

I am playing about with Erlang and I am trying to write a simple arithmetic parser. 我正在玩Erlang,我正在尝试编写一个简单的算术解析器。

I want to try and parse the following expression: 我想尝试解析以下表达式:

((12+3)-4)

I want to parse the expression into a stack of AST nodes. 我想将表达式解析为AST节点的堆栈。 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#: 在解析这个表达式时,我首先要为(12 + 3)表达式创建一个二进制表达式,它在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. 我对Erlang很陌生,我想知道如何在Erlang中创建这样的结构,我可以放在一个List上,我将它用作表达式的堆栈。

Can anyone suggest how to create such a tree like structure? 任何人都可以建议如何创建这样的树状结构? Would a function be a good fit? 功能是否合适?

In functional language like Erlang it is far simpler. 在像Erlang这样的函数式语言中,它简单得多。 Just make it 做吧

{'+', 12, 3}

In more abstract way 以更抽象的方式

A = 12,
B = 3,
OP = '+',
{OP, A, B}.

Also, have a look to the erl_parse.erl module in the stdlib application. 另外,请查看stdlib应用程序中的erl_parse.erl模块。

Reading from to the mkop function: mkop函数读取:

mkop(L, {Op,Pos}, R) -> {op,Pos,Op,L,R}.                                        

mkop({Op,Pos}, A) -> {op,Pos,Op,A}.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM