marcomaggi / vicare

A native compiler for Scheme compliant with R6RS
http://marcomaggi.github.com/vicare.html
Other
200 stars 34 forks source link

Wrong handling of non-terminals "prec:" qualifier in LALR generated parsers #58

Open marcomaggi opened 11 years ago

marcomaggi commented 11 years ago

When defining a parser with the library (nausicaa parser-tools lalr) and the grammar below:

(define make-parser
  (lalr.lalr-parser
   (lalr.output-value: #t)
   (lalr.expect: 0)
   (lalr.terminals: '(NUM                ;precedence 0
                      (left: ADD SUB)    ;precedence 1
                      (left: MUL DIV)    ;precedence 2
                      (nonassoc: UADD)   ;precedence 3
                      (nonassoc: USUB))) ;precedence 4
   (lalr.rules:
    '((EXPR

       ;;These are the offending rules.
       (ADD EXPR (prec: UADD)) : (list $1 $2)
       (SUB EXPR (prec: USUB)) : (list $1 $2)

       (EXPR ADD EXPR)         : (list $2 $1 $3)
       (EXPR SUB EXPR)         : (list $2 $1 $3)
       (EXPR MUL EXPR)         : (list $2 $1 $3)
       (EXPR DIV EXPR)         : (list $2 $1 $3)
       (NUM)                   : $1)))))

the rules defining the unary + and unary - operators do not handle correctly the prec: qualifier. This results in the sequence:

SUB ONE MUL TWO

to be parsed as (- (* 1 2)) rather than (* (- 1) 2)).

marcomaggi commented 11 years ago

Notice that the same unary operators definition works fine when used in the parser generated by tests/make-lalr-calc.sps.