123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- ;;; test-glr-associativity.scm
- ;;
- ;;With the GLR parser both the terminal precedence and the non-terminal
- ;;associativity are not respected; rather they generate two child
- ;;processes.
- ;;
- (load "common-test.scm")
- (define parser
- (lalr-parser
- (driver: glr)
- (expect: 0)
- (N LPAREN RPAREN
- (left: + -)
- (right: * /)
- (nonassoc: uminus))
- (output (expr) : $1)
- (expr (expr + expr) : (list $1 '+ $3)
- (expr - expr) : (list $1 '- $3)
- (expr * expr) : (list $1 '* $3)
- (expr / expr) : (list $1 '/ $3)
- (- expr (prec: uminus)) : (list '- $2)
- (N) : $1
- (LPAREN expr RPAREN) : $2)))
- (define (doit . tokens)
- (parser (make-lexer tokens) error-handler))
- ;;; --------------------------------------------------------------------
- ;;Remember that the result of the GLR driver is a list of parses, not a
- ;;single parse.
- (check
- (doit (make-lexical-token 'N #f 1))
- => '(1))
- (check
- (doit (make-lexical-token 'N #f 1)
- (make-lexical-token '+ #f '+)
- (make-lexical-token 'N #f 2))
- => '((1 + 2)))
- (check
- (doit (make-lexical-token 'N #f 1)
- (make-lexical-token '* #f '*)
- (make-lexical-token 'N #f 2))
- => '((1 * 2)))
- (check
- (doit (make-lexical-token '- #f '-)
- (make-lexical-token 'N #f 1))
- => '((- 1)))
- (check
- (doit (make-lexical-token '- #f '-)
- (make-lexical-token '- #f '-)
- (make-lexical-token 'N #f 1))
- => '((- (- 1))))
- (check
- (doit (make-lexical-token 'N #f 1)
- (make-lexical-token '+ #f '+)
- (make-lexical-token '- #f '-)
- (make-lexical-token 'N #f 2))
- => '((1 + (- 2))))
- ;;; --------------------------------------------------------------------
- (check
- ;;left-associativity
- (doit (make-lexical-token 'N #f 1)
- (make-lexical-token '+ #f '+)
- (make-lexical-token 'N #f 2)
- (make-lexical-token '+ #f '+)
- (make-lexical-token 'N #f 3))
- => '(((1 + 2) + 3)))
- (check
- ;;right-associativity
- (doit (make-lexical-token 'N #f 1)
- (make-lexical-token '* #f '*)
- (make-lexical-token 'N #f 2)
- (make-lexical-token '* #f '*)
- (make-lexical-token 'N #f 3))
- => '(((1 * 2) * 3)
- (1 * (2 * 3))))
- (check
- ;;precedence
- (doit (make-lexical-token 'N #f 1)
- (make-lexical-token '+ #f '+)
- (make-lexical-token 'N #f 2)
- (make-lexical-token '* #f '*)
- (make-lexical-token 'N #f 3))
- => '(((1 + 2) * 3)
- (1 + (2 * 3))))
- ;;; end of file
|