[KDB +/Q]:将q解析树解析为q表达式(字符串) [英] [KDB+/Q]: Deparse q parse tree into q expression (string)

查看:112
本文介绍了[KDB +/Q]:将q解析树解析为q表达式(字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们将deparse 1 定义为对q的本机parse操作,以便满足以下条件:

Let's define deparse1 as inverse operation to q's native parse, so that the following holds:

q)aStringQExpression~deparse parse aStringQExpression
1b

问题

deparse函数的定义是什么,以便上述内容确实有效?

What's the definition of deparse function so that the above indeed works?

例如,在下面的更新表达式中,我们知道"a*0^b+c-d"表达式对应于(*;`a;(^;0;(+;`b;(-;`c;`d))))解析树:

For example, in the below update expression, we know that "a*0^b+c-d" expression corresponds to (*;`a;(^;0;(+;`b;(-;`c;`d)))) parse tree:

q)-3!parse "update col:a*0^b+c-d from t"
"(!;`t;();0b;(,`col)!,(*;`a;(^;0;(+;`b;(-;`c;`d)))))"

因此设想的deparse函数应返回:

So the envisaged deparse function should return:

q)deparse "(*;`a;(^;0;(+;`b;(-;`c;`d))))"
"a*0^b+c-d"
q)deparse "(!;`t;();0b;(,`col)!,(*;`a;(^;0;(+;`b;(-;`c;`d)))))"
"update col:a*0^b+c-d from t"

动机/背景/用例: 可以说,用人眼(从左到右),内联表达式可以更快地 grok ),而不是深层嵌套的解析树.尽管在后台,我的代码正在以编程方式编辑解析树,但对于调试或表示而言,将生成的解析树方便地转换为内联表达式字符串很有用.

Motivation/Background/Use case: Inline expressions are arguably faster to grok by human eye (left-to-right) than deeply nested parse trees. Although in the background my code is editing the parse tree programatically, it is useful for debugging or presentation to conveniently convert that resulting parse tree into inline expression string.

1 此处描述的类似功能: http://adv-r.had.co.nz/Expressions.html#parsing-and-deparsing

1 Similar functionality described here: http://adv-r.had.co.nz/Expressions.html#parsing-and-deparsing

推荐答案

此来自Github的unparse存储库解决了该问题.很棒:

This unparse repository from Github solves the problem. Amazing:

q).unparse.unparse parse "update col:a*0^b+c-d from t"
"update col:(a*(0^(b+(c-d)))) from t"

q).unparse.unparse parse "a*0^b+c-d"
"(a*(0^(b+(c-d))))"

这篇关于[KDB +/Q]:将q解析树解析为q表达式(字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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