From: Stefan Israelsson Tampe <stefan.itampe@gmail.com>
To: guile-user@gnu.org
Subject: Operator parser
Date: Mon, 12 Aug 2013 00:00:21 +0200 [thread overview]
Message-ID: <12630275.qHFy9oxqCv@warperdoze> (raw)
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 other threads:[~2013-08-11 22:00 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=12630275.qHFy9oxqCv@warperdoze \
--to=stefan.itampe@gmail.com \
--cc=guile-user@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).