test-glr-script-expression.scm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. ;;; test-lr-script-expression.scm --
  2. ;;
  3. ;;Parse scripts, each line an expression.
  4. ;;
  5. (load "common-test.scm")
  6. (define (doit . tokens)
  7. (let ((parser (lalr-parser (expect: 0)
  8. (driver: glr)
  9. (N O C T (left: A) (left: M) (nonassoc: U))
  10. (script (lines) : (reverse $1))
  11. (lines (lines line) : (cons $2 $1)
  12. (line) : (list $1))
  13. (line (T) : #\newline
  14. (E T) : $1
  15. (error T) : (list 'error-clause $2))
  16. (E (N) : $1
  17. (E A E) : ($2 $1 $3)
  18. (E M E) : ($2 $1 $3)
  19. (A E (prec: U)) : ($1 $2)
  20. (O E C) : $2))))
  21. (parser (make-lexer tokens) error-handler)))
  22. ;;; --------------------------------------------------------------------
  23. ;;; Correct input
  24. (check
  25. (doit (make-lexical-token 'T #f #\newline))
  26. => '((#\newline)))
  27. (check
  28. (doit (make-lexical-token 'N #f 1)
  29. (make-lexical-token 'T #f #\newline))
  30. => '((1)))
  31. (check
  32. (doit (make-lexical-token 'N #f 1)
  33. (make-lexical-token 'A #f +)
  34. (make-lexical-token 'N #f 2)
  35. (make-lexical-token 'T #f #\newline))
  36. => '((3)))
  37. (check
  38. (doit (make-lexical-token 'N #f 1)
  39. (make-lexical-token 'A #f +)
  40. (make-lexical-token 'N #f 2)
  41. (make-lexical-token 'M #f *)
  42. (make-lexical-token 'N #f 3)
  43. (make-lexical-token 'T #f #\newline))
  44. => '((9) (7)))
  45. (check
  46. (doit (make-lexical-token 'N #f 10)
  47. (make-lexical-token 'M #f *)
  48. (make-lexical-token 'N #f 2)
  49. (make-lexical-token 'A #f +)
  50. (make-lexical-token 'N #f 3)
  51. (make-lexical-token 'T #f #\newline))
  52. => '((23)))
  53. (check
  54. (doit (make-lexical-token 'O #f #\()
  55. (make-lexical-token 'N #f 1)
  56. (make-lexical-token 'A #f +)
  57. (make-lexical-token 'N #f 2)
  58. (make-lexical-token 'C #f #\))
  59. (make-lexical-token 'M #f *)
  60. (make-lexical-token 'N #f 3)
  61. (make-lexical-token 'T #f #\newline))
  62. => '((9)))
  63. (check
  64. (doit (make-lexical-token 'O #f #\()
  65. (make-lexical-token 'N #f 1)
  66. (make-lexical-token 'A #f +)
  67. (make-lexical-token 'N #f 2)
  68. (make-lexical-token 'C #f #\))
  69. (make-lexical-token 'M #f *)
  70. (make-lexical-token 'N #f 3)
  71. (make-lexical-token 'T #f #\newline)
  72. (make-lexical-token 'N #f 4)
  73. (make-lexical-token 'M #f /)
  74. (make-lexical-token 'N #f 5)
  75. (make-lexical-token 'T #f #\newline))
  76. => '((9 4/5)))
  77. ;;; --------------------------------------------------------------------
  78. (check
  79. ;;Successful error recovery.
  80. (doit (make-lexical-token 'O #f #\()
  81. (make-lexical-token 'N #f 1)
  82. (make-lexical-token 'A #f +)
  83. (make-lexical-token 'N #f 2)
  84. (make-lexical-token 'M #f *)
  85. (make-lexical-token 'N #f 3)
  86. (make-lexical-token 'T #f #\newline)
  87. (make-lexical-token 'N #f 4)
  88. (make-lexical-token 'M #f /)
  89. (make-lexical-token 'N #f 5)
  90. (make-lexical-token 'T #f #\newline))
  91. => '())
  92. (check
  93. ;;Unexpected end of input.
  94. (doit (make-lexical-token 'N #f 1)
  95. (make-lexical-token 'A #f +)
  96. (make-lexical-token 'N #f 2))
  97. => '())
  98. (check
  99. ;;Unexpected end of input.
  100. (doit (make-lexical-token 'N #f 1)
  101. (make-lexical-token 'A #f +)
  102. (make-lexical-token 'T #f #\newline))
  103. => '())
  104. ;;; end of file