; -*- Hen -*-


(require-extension lalr)
(define brep-parser
  (lalr-parser

   ;; --- Options 
   ;; output a parser, called brep-parser, in a separate file - brep.yy.scm, 
   (output:    brep-parser "brep.yy.scm")
   ;; output the LALR table to brep.grm.out
   (out-table: "brep.grm.out")
   ;; there should be no conflict
   (expect:    5)

   ;; --- token definitions
   (ID NUM = LPAREN RPAREN NEWLINE COMMA STRING FUNCTION
       (left: + -)
       (left: * /)
       (nonassoc: uminus))

   ;; --- rules
   (line     (assign NEWLINE)        : $1
             (expr   NEWLINE)        : $1
	     (NEWLINE)               : #f
             (error  NEWLINE)        : #f)

   (assign   (ID = expr)             : (add-binding $1 $3))

;;   (defn   (FUNC LPAREN args RPAREN LBRACE expr RBRACE)             : (add-binding $1 $3))

   (expr     (expr + expr)           : (+ $1 $3)
             (expr - expr)           : (- $1 $3)
             (expr * expr)           : (* $1 $3)
             (expr / expr)           : (/ $1 $3)
             (- expr (prec: uminus)) : (- $2)
             (ID)                    : (get-binding $1)
             (ID LPAREN args RPAREN) : (invoke-func $1 $3)
             (NUM)                   : $1
             (LPAREN expr RPAREN)    : $2)

   (args     ()                      : '()
             (expr arg-rest)         : (cons $1 $2)
             (STRING arg-rest)       : (cons $1 $2))

   (arg-rest (COMMA expr arg-rest)   : (cons $2 $3)
             (COMMA STRING arg-rest) : (cons $2 $3)
             ()                      : '())

   ))