test-lr-associativity-04.scm 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ;;; test-lr-associativity-04.scm --
  2. ;;
  3. ;;Show how to use associativity.
  4. ;;
  5. (load "common-test.scm")
  6. (define (doit . tokens)
  7. (let ((parser (lalr-parser
  8. (expect: 0)
  9. (N (left: A)
  10. (left: M))
  11. (E (N) : $1
  12. (E A E) : (list $1 $2 $3)
  13. (E A E A E) : (list (list $1 $2 $3) $4 $5)
  14. (E M E) : (list $1 $2 $3)
  15. (E M E M E) : (list $1 $2 (list $3 $4 $5))
  16. (E A E M E) : (list $1 $2 $3 $4 $5)
  17. (E M E A E) : (list $1 $2 $3 $4 $5)
  18. ))))
  19. (parser (make-lexer tokens) error-handler)))
  20. ;;; --------------------------------------------------------------------
  21. ;;; Single operator.
  22. (check
  23. (doit (make-lexical-token 'N #f 1))
  24. => 1)
  25. (check
  26. (doit (make-lexical-token 'N #f 1)
  27. (make-lexical-token 'A #f '+)
  28. (make-lexical-token 'N #f 2))
  29. => '(1 + 2))
  30. (check
  31. (doit (make-lexical-token 'N #f 1)
  32. (make-lexical-token 'M #f '*)
  33. (make-lexical-token 'N #f 2))
  34. => '(1 * 2))
  35. ;;; --------------------------------------------------------------------
  36. ;;; Precedence.
  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. => '(1 + (2 * 3)))
  44. (check
  45. (doit (make-lexical-token 'N #f 1)
  46. (make-lexical-token 'M #f '*)
  47. (make-lexical-token 'N #f 2)
  48. (make-lexical-token 'A #f '+)
  49. (make-lexical-token 'N #f 3))
  50. => '((1 * 2) + 3))
  51. ;;; --------------------------------------------------------------------
  52. ;;; Associativity.
  53. (check
  54. (doit (make-lexical-token 'N #f 1)
  55. (make-lexical-token 'A #f '+)
  56. (make-lexical-token 'N #f 2)
  57. (make-lexical-token 'A #f '+)
  58. (make-lexical-token 'N #f 3))
  59. => '((1 + 2) + 3))
  60. (check
  61. (doit (make-lexical-token 'N #f 1)
  62. (make-lexical-token 'M #f '*)
  63. (make-lexical-token 'N #f 2)
  64. (make-lexical-token 'M #f '*)
  65. (make-lexical-token 'N #f 3))
  66. => '((1 * 2) * 3))
  67. ;;; end of file