unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Operator parser
@ 2013-08-11 22:00 Stefan Israelsson Tampe
  0 siblings, 0 replies; only message in thread
From: Stefan Israelsson Tampe @ 2013-08-11 22:00 UTC (permalink / raw)
  To: guile-user

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!






^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2013-08-11 22:00 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-08-11 22:00 Operator parser Stefan Israelsson Tampe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).