|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(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)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(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 |
|
|
|
(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 |
|
|
|
(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 |
|
|
|
(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)))) |
|
|
|
|
|
|