带有尽可能少的括号的漂亮打印表达式?
我的问题:在没有多余括号的情况下漂亮地打印表达式的最简洁方法是什么?
我有以下 lambda 表达式的表示形式:
Term ::= Fun(String x, Term t)
| App(Term t1, Term t2)
| Var(String x)
按照约定,App
是左关联的,即 ab c
被解释为 (ab) c
和函数物体尽可能向右拉伸,即 λ x。 x y
被解释为λ x。 (xy)。
我有一个做得很好的解析器,但现在我想要一台漂亮的打印机。这是我目前拥有的(伪 scala):
term match {
case Fun(v, t) => "(λ %s.%s)".format(v, prettyPrint(t))
case App(s, t) => "(%s %s)".format(prettyPrint(s), prettyPrint(t))
case Var(v) => v
}
上面的打印机总是在表达式周围放置 (
)
(原子变量除外)。因此,对于 Fun(x, App(Fun(y, x), y))
它产生
(λ x.((λ y.x) y))
我想要
λ x.(λ y.x) y
My Question: What is the cleanest way to pretty print an expression without redundant parentheses?
I have the following representation of lambda expressions:
Term ::= Fun(String x, Term t)
| App(Term t1, Term t2)
| Var(String x)
By convention App
is left associative, that is a b c
is interpreted as (a b) c
and function bodies stretch as far to the right as possible, that is, λ x. x y
is interpreted as λ x. (x y)
.
I have a parser that does a good job, but now I want a pretty printer. Here's what I currently have (pseudo scala):
term match {
case Fun(v, t) => "(λ %s.%s)".format(v, prettyPrint(t))
case App(s, t) => "(%s %s)".format(prettyPrint(s), prettyPrint(t))
case Var(v) => v
}
The above printer always puts (
)
around expressions (except for atomic variables). Thus for Fun(x, App(Fun(y, x), y))
it produces
(λ x.((λ y.x) y))
I would like to have
λ x.(λ y.x) y
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里,我将使用一个简单的中缀表达式语法,其结合性和优先级由以下语法定义,其运算符按优先级升序列出
给定一个抽象语法树(AST),我们将 AST 转换为仅包含必要括号的字符串,如下面的伪代码所述。当我们递归地下降树以确定何时需要括号时,我们检查相对优先级和关联性。请注意,所有用括号括住表达式的决定都必须在父节点中做出。
Here I'll use a simple grammar for infix expressions with the associativity and precedence defined by the following grammar whose operators are listed in ascending order of precedence
Given an abstract syntax tree (AST) we convert the AST to a string with only the necessary parenthesis as described in the pseudocode below. We examine relative precedence and associativity as we recursively descend the tree to determine when parenthesis are necessary. Note that all decisions to wrap parentheses around an expression must be made in the parent node.
是不是只要检查App的参数类型就可以了?
我不知道如何用 scala 编写这个..
Isn't it that you just have to check the types of the arguments of App?
I'm not sure how to write this in scala..