glr-test.scm 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. ":";exec snow -- "$0" "$@"
  2. ;;;
  3. ;;;; Tests for the GLR parser generator
  4. ;;;
  5. ;;
  6. ;; @created "Fri Aug 19 11:23:48 EDT 2005"
  7. ;;
  8. (package* glr-test/v1.0.0
  9. (require: lalr/v2.4.0))
  10. (define (syntax-error msg . args)
  11. (display msg (current-error-port))
  12. (for-each (cut format (current-error-port) " ~A" <>) args)
  13. (newline (current-error-port))
  14. (throw 'misc-error))
  15. (define (make-lexer words)
  16. (let ((phrase words))
  17. (lambda ()
  18. (if (null? phrase)
  19. '*eoi*
  20. (let ((word (car phrase)))
  21. (set! phrase (cdr phrase))
  22. word)))))
  23. ;;;
  24. ;;;; Test 1
  25. ;;;
  26. (define parser-1
  27. ;; Grammar taken from Tomita's "An Efficient Augmented-Context-Free Parsing Algorithm"
  28. (lalr-parser
  29. (driver: glr)
  30. (expect: 2)
  31. (*n *v *d *p)
  32. (<s> (<np> <vp>)
  33. (<s> <pp>))
  34. (<np> (*n)
  35. (*d *n)
  36. (<np> <pp>))
  37. (<pp> (*p <np>))
  38. (<vp> (*v <np>))))
  39. (define *phrase-1* '(*n *v *d *n *p *d *n *p *d *n *p *d *n))
  40. (define (test-1)
  41. (parser-1 (make-lexer *phrase-1*) syntax-error))
  42. ;;;
  43. ;;;; Test 2
  44. ;;;
  45. (define parser-2
  46. ;; The dangling-else problem
  47. (lalr-parser
  48. (driver: glr)
  49. (expect: 1)
  50. ((nonassoc: if then else e s))
  51. (<s> (s)
  52. (if e then <s>)
  53. (if e then <s> else <s>))))
  54. (define *phrase-2* '(if e then if e then s else s))
  55. (define (test-2)
  56. (parser-2 (make-lexer *phrase-2*) syntax-error))
  57. (define (assert-length l n test-name)
  58. (display "Test '")
  59. (display test-name)
  60. (display (if (not (= (length l) n)) "' failed!" "' passed!"))
  61. (newline))
  62. (assert-length (test-1) 14 1)
  63. (assert-length (test-2) 2 2)