Erlang尝试评估字符串 [英] Erlang trying to evaluate a string

查看:110
本文介绍了Erlang尝试评估字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试动态评估Erlang条款

I'm trying to dynamically evalutate Erlang terms

启动Erlang

basho-catah% erl
Erlang R16B03 (erts-5.10.4) [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V5.10.4  (abort with ^G)

创建一个词

1> {a,[b,c,d]}.
{a,[b,c,d]}

尝试使用相同的术语进行扫描

Try to scan in the same term

2> {ok, Tokens, _ } = erl_scan:string("{a,[b,c,d]}").
{ok,[{'{',1},
     {atom,1,a},
     {',',1},
     {'[',1},
     {atom,1,b},
     {',',1},
     {atom,1,c},
     {',',1},
     {atom,1,d},
     {']',1},
     {'}',1}],
    1}


3> Tokens.
[{'{',1},
 {atom,1,a},
 {',',1},
 {'[',1},
 {atom,1,b},
 {',',1},
 {atom,1,c},
 {',',1},
 {atom,1,d},
 {']',1},
 {'}',1}]

但是它无法解析该标记化的字符串.

But it can't parse that tokenized string.

4> Foo = erl_parse:parse(Tokens).
{error,{1,erl_parse,["syntax error before: ","'{'"]}}

有什么想法我在做什么错吗?

Any ideas what I'm doing wrong?

推荐答案

您使用了错误的函数,并且还没有遇到一个警告.

You're using the wrong function, and there's also a caveat you haven't encountered.

首先,使用的功能是erl_parse:parse_term/1.我实际上无法找到erl_parse:parse/1的文档,因此我怀疑它已被弃用(并且很可能用于解析抽象语法树,而不是令牌).

First, the function you should be using is erl_parse:parse_term/1. I'm not actually able to find documentation for erl_parse:parse/1, so I suspect it's deprecated (and most likely used for parsing abstract-syntax trees, not tokens).

其次,要使erl_parse:parse_term/1正常工作,您必须 在您的术语中包含终止点字符. erl_scan:string/1会很乐意将您提供的所有内容都转换为令牌,但是没有终结符erl_parse:parse_term/1期望会得到更多.

Second, for erl_parse:parse_term/1 to work, you must include the terminating dot character in your term. erl_scan:string/1 will happily convert whatever you give it into tokens, but without the terminator erl_parse:parse_term/1 expects to receive more.

因此,请在shell中尝试以下操作:

So, try the following in a shell:

{ok, Tokens, _} = erl_scan:string("{a,[b,c,d]}.").
erl_parse:parse_term(Tokens).

这篇关于Erlang尝试评估字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆