test-glr-associativity.scm 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. ;;; test-glr-associativity.scm
  2. ;;
  3. ;;With the GLR parser both the terminal precedence and the non-terminal
  4. ;;associativity are not respected; rather they generate two child
  5. ;;processes.
  6. ;;
  7. (load "common-test.scm")
  8. (define parser
  9. (lalr-parser
  10. (driver: glr)
  11. (expect: 0)
  12. (N LPAREN RPAREN
  13. (left: + -)
  14. (right: * /)
  15. (nonassoc: uminus))
  16. (output (expr) : $1)
  17. (expr (expr + expr) : (list $1 '+ $3)
  18. (expr - expr) : (list $1 '- $3)
  19. (expr * expr) : (list $1 '* $3)
  20. (expr / expr) : (list $1 '/ $3)
  21. (- expr (prec: uminus)) : (list '- $2)
  22. (N) : $1
  23. (LPAREN expr RPAREN) : $2)))
  24. (define (doit . tokens)
  25. (parser (make-lexer tokens) error-handler))
  26. ;;; --------------------------------------------------------------------
  27. ;;Remember that the result of the GLR driver is a list of parses, not a
  28. ;;single parse.
  29. (check
  30. (doit (make-lexical-token 'N #f 1))
  31. => '(1))
  32. (check
  33. (doit (make-lexical-token 'N #f 1)
  34. (make-lexical-token '+ #f '+)
  35. (make-lexical-token 'N #f 2))
  36. => '((1 + 2)))
  37. (check
  38. (doit (make-lexical-token 'N #f 1)
  39. (make-lexical-token '* #f '*)
  40. (make-lexical-token 'N #f 2))
  41. => '((1 * 2)))
  42. (check
  43. (doit (make-lexical-token '- #f '-)
  44. (make-lexical-token 'N #f 1))
  45. => '((- 1)))
  46. (check
  47. (doit (make-lexical-token '- #f '-)
  48. (make-lexical-token '- #f '-)
  49. (make-lexical-token 'N #f 1))
  50. => '((- (- 1))))
  51. (check
  52. (doit (make-lexical-token 'N #f 1)
  53. (make-lexical-token '+ #f '+)
  54. (make-lexical-token '- #f '-)
  55. (make-lexical-token 'N #f 2))
  56. => '((1 + (- 2))))
  57. ;;; --------------------------------------------------------------------
  58. (check
  59. ;;left-associativity
  60. (doit (make-lexical-token 'N #f 1)
  61. (make-lexical-token '+ #f '+)
  62. (make-lexical-token 'N #f 2)
  63. (make-lexical-token '+ #f '+)
  64. (make-lexical-token 'N #f 3))
  65. => '(((1 + 2) + 3)))
  66. (check
  67. ;;right-associativity
  68. (doit (make-lexical-token 'N #f 1)
  69. (make-lexical-token '* #f '*)
  70. (make-lexical-token 'N #f 2)
  71. (make-lexical-token '* #f '*)
  72. (make-lexical-token 'N #f 3))
  73. => '(((1 * 2) * 3)
  74. (1 * (2 * 3))))
  75. (check
  76. ;;precedence
  77. (doit (make-lexical-token 'N #f 1)
  78. (make-lexical-token '+ #f '+)
  79. (make-lexical-token 'N #f 2)
  80. (make-lexical-token '* #f '*)
  81. (make-lexical-token 'N #f 3))
  82. => '(((1 + 2) * 3)
  83. (1 + (2 * 3))))
  84. ;;; end of file