1
votes

I write a BNF grammar like this:

#lang pl

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

(define-type LE
  [Num Number]
)

but I am not sure how to check if this code are good... how to check in racket the the only thing that we can use its null and numbers?

I think something like that:

(test 5)

but

(test '())

working too, and I am not setting List in my BNF

(if this code are not good - I will be happy for some BNF example and checking...)

1
Since the language pl is not part of the main distribution, you will need to give some more information. Is the pl language from Bremner's course? (cs.unb.ca/~bremner/teaching/cs3613/racket-setup ). If so which version of Racket is it for?soegaard

1 Answers

1
votes

Without testing I suggest trying the following program:

#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)

Note that (: test : LE -> LE) declares the types of the test function. Since in (test '()) the empty list does not match the LE type, you ought to get an error.

EDIT: The examples has been update to use (Num 5) and not just 5.

EDIT 2: Added Kons