DrRacket BNF语法

问题描述 投票:1回答:1

我写了这样的BNF语法:

#lang pl

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

(define-type LE
  [Num Number]
)

但我不知道如何检查这段代码是否良好...如何检查球拍我们唯一可以使用它的null和数字?

我想是这样的:

(test 5)

(test '())

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

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

numbers racket bnf
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.