CRONOS-19 / lalr-scm

Automatically exported from code.google.com/p/lalr-scm
0 stars 0 forks source link

on right associativity and the LR driver #9

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Running the following under Guile and lalr-scm non-Snow distribution:

(define (display-result v)
  (if v
      (begin
        (display "==> ")
        (display v)
        (newline))))

(define eoi-token
  (make-lexical-token '*eoi* #f #f))

      (define (doit . tokens)
    (define calc-parser
      (lalr-parser
;;;    (output: assoc "proof-associativity.yy.scm")
       (expect: 0)

       (N (left: A) (right: M) (nonassoc: U))

       (E       (N)     : $1
            (E A E)         : (list $1 '+ $3)
            (E M E)         : (list $1 '* $3)
            (A E (prec: U)) : (list 'U $2))))

    (let* ((lexer       (lambda ()
                  (if (null? tokens)
                      eoi-token
                    (let ((t (car tokens)))
                      (set! tokens (cdr tokens))
                      t))))
           (error-handler   (lambda args #f)))
      (display-result (calc-parser lexer error-handler))))

      ;;right-associativity
      (doit (make-lexical-token 'N #f 1)
        (make-lexical-token 'M #f '*)
        (make-lexical-token 'N #f 2)
        (make-lexical-token 'M #f '*)
        (make-lexical-token 'N #f 3)
        eoi-token)

I get:

==> ((1 * 2) * 3)

but tokens of category M are right associative, so I expected:

==> (1 * (2 * 3))

Original issue reported on code.google.com by mrc....@gmail.com on 12 Aug 2009 at 3:30

GoogleCodeExporter commented 9 years ago
Further inspection seems to reveal that the associativity of M is checked only 
when
the rule is something like:

(E M E M E)

rather than a simple "(E M E)"; without the long rule "(resolve-conflict sym 
rule)"
is never invoked with SYM equal to RULE.

While I can understand this, and there is a way to turn on associativity, I am 
not
sure that this is the better interface. Anyway, IMHO it should be documented.

Original comment by mrc....@gmail.com on 21 Aug 2009 at 3:03