guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Operator parser


From: Stefan Israelsson Tampe
Subject: Operator parser
Date: Mon, 12 Aug 2013 00:00:21 +0200
User-agent: KMail/4.9.5 (Linux/3.5.0-30-generic; KDE/4.9.5; x86_64; ; )

Hi all,

I realized that prolog have a very cool feature. A Customizable
operator parser. prolog is just one big expression with lot's of
operators that can be redefined at will, also one would expect that
parsing such a monster is a huge task, but it isn't. 

look at
http://logic.cse.unt.edu/tarau/teaching/ProgLang/2009/yfx_operator.pdf

to see my point. I took that and added a port in guile-log in the
guile-log repo under (logic guile-log examples parser operator-parser)
It's just source code right now, nothing executable yet.

The interface is,
#:export (make-opdata add-operator mk-operator-expression)

You make an opdata by
  (define data (make-opdata))

You add an operator / change and operator with
  (add-operator data type name level)

     data  :  an opdata object
     type  :  fx fy xf yf xfx xfy yfx
     name  :  symbol or string representing the operator name
     level :  the operator precedence value.

For anyone wanting to know more about these things refer to
  http://www.swi-prolog.org/pldoc/man?section=operators

To make a match function use mk-operator-expression in e.g.
   
   (mk-operator-expression op-atom data)

       op-atom : a non operator object
       data    : a operators object
        
Sketchy Example, Consider pythons: + - * / ** number variable and 
                                   '( expr )'


(define data    (make-opdata))
(define op-data #f)
(define p-expr  (mk-operator-expression op-atom data))
(set! op-atom 
      (p-or p-number
            p-variable
           (p-seq (p-char #\) 
                   p-expr 
                  (p-char #\))))

(add-operator data yf  '-  30)
(add-operator data yf  '+  30)
(add-operator data xfy '+  10)
(add-operator data xfy '-  10)
(add-operator data xfy '*  20)
(add-operator data xfy '*  20)
(add-operator data fy  '** 40)

And p-expr would match the expressions and output a syntactic tree.
At least this is the idea of the interface.


--------

To note is that this exercise was good in a general way because it
showed a need to streamline and ease the burden of the programmer
for specifying parsing constructs. The problem was that one need to
transfer variables from input to output. Which was mostly by standard
so as in prolog this was automated and the corresponding variables was 
associated with syntax-parameters in stead if one need to use it. It 
looks like it was a boon and the code is no much cleaner if you look
at operator-parser.scm.

So one writes
   (.. out a ... (f in b ...))

in stead of
   (<values> (X XL out N M a ...) (f X XL in N M b ...))

with X XL N M being syntax parameters. Also needed at tail position
   (.. (f in b ...))

means
   (f X XL in N M b ...)

Have fun!






reply via email to

[Prev in Thread] Current Thread [Next in Thread]