DrRacket BNF语法

时间:2016-04-10 14:34:35

标签: numbers racket bnf

我写了这样的BNF语法:

#lang pl

#| BNF for the LE language:
   <LE> ::= <num>
          | <null>
|#

(define-type LE
  [Num Number]
)

但我不确定如何检查此代码是否良好... 如何检查球拍我们唯一可以使用它的空值和数字?

我认为是这样的:

(test 5)

但是

(test '())

也在工作,我没有在我的BNF中设置List

(如果这段代码不好 - 我会很高兴看到一些BNF示例并检查......)

1 个答案:

答案 0 :(得分:1)

如果没有测试,我建议您尝试以下程序:

#lang pl

#| BNF for the LE language:
   <LE> ::= <num>
          | <null>
|#

(define-type LE
  [Num Number]
  [Nul Null]
  [Kons LE LE])

(: test : LE -> LE)
(define (test x)
   x)

(test (Num 5))        ; this is accepted since 5 is a Number
(test (Nul '())
(test (Kons (Num 1) (Num 2)))
; (test (Num "foo"))  ; this provokes an error (as it should)

请注意(: test : LE -> LE)声明test函数的类型。由于在(test '())中空列表与LE类型不匹配,因此您应该收到错误。

编辑:示例已更新为使用(Num 5)而不只是5

编辑2:添加了Kons

相关问题