解析S表达式 [英] Parsing S Expression

查看:201
本文介绍了解析S表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下定义构成了来自Yorgey教授的 S表达式 course

data Atom = N Integer | I Ident派生Show





数据SExpr =一个Atom | Comb [SExpr]
派生Show



完整的数据类型应该在Haskell中用于以下内容?



(bar(foo)3 5 874)

我相信它会是这样的:

  Comb 
[A(Ibar)
,A(N 3)
,A(N 5)
,A(N 874)b,Comb
[A(Ifoo)
]
]

每当你遇到一个左括号时,你会开始一个新的 Comb 表达式,所以(foo) Comb [A(Ifoo)] foo 仅仅是 A(Ifoo)


Given the following definitions that make up an S Expression from Prof. Yorgey's course:

data Atom = N Integer | I Ident deriving Show

and

data SExpr = A Atom | Comb [SExpr] deriving Show

What should the full data type be (in Haskell) for the following?

(bar (foo) 3 5 874)

解决方案

I believe it would be something like

Comb
    [ A (I "bar")
    , Comb
        [ A (I "foo")
        ]
    , A (N 3)
    , A (N 5)
    , A (N 874)
    ]

Whenever you encounter an open parenthesis you would start a new Comb expression, so (foo) is Comb [A (I "foo")] while foo is simply A (I "foo").

这篇关于解析S表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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