From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Israelsson Tampe Newsgroups: gmane.lisp.guile.user Subject: Operator parser Date: Mon, 12 Aug 2013 00:00:21 +0200 Message-ID: <12630275.qHFy9oxqCv@warperdoze> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7Bit X-Trace: ger.gmane.org 1376258447 23052 80.91.229.3 (11 Aug 2013 22:00:47 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 11 Aug 2013 22:00:47 +0000 (UTC) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Mon Aug 12 00:00:50 2013 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1V8dgw-0005vT-By for guile-user@m.gmane.org; Mon, 12 Aug 2013 00:00:50 +0200 Original-Received: from localhost ([::1]:48625 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V8dgv-0004pX-Qq for guile-user@m.gmane.org; Sun, 11 Aug 2013 18:00:49 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:48082) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V8dgg-0004pD-7N for guile-user@gnu.org; Sun, 11 Aug 2013 18:00:40 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V8dgb-0006t0-Aa for guile-user@gnu.org; Sun, 11 Aug 2013 18:00:34 -0400 Original-Received: from mail-lb0-x22d.google.com ([2a00:1450:4010:c04::22d]:63185) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V8dgb-0006sj-2n for guile-user@gnu.org; Sun, 11 Aug 2013 18:00:29 -0400 Original-Received: by mail-lb0-f173.google.com with SMTP id 10so4360593lbf.18 for ; Sun, 11 Aug 2013 15:00:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id:user-agent:mime-version :content-transfer-encoding:content-type; bh=yLpVMCeAaEbJ2cuHAR5cWlFu+Dp/bdHiQEDSef7EFGU=; b=zb/wxGtWBqlHSmo+5XsH5TIGGSr19hKjzEYKix+YPGWEfLUdX6Jq7/cE3v0TyHpglu yZShan68OTgeLojuI3dLCsmuoihx4byZPIYAcXhZRC/lBFtRTjErqHxXWexP0342/vy1 6RonHkJJkOoTt+tc88lioRW4BiRMvLnrw5TDTsrvKAzfoaOQ5ifccXJd6FMP2PRNfBso mJxD110YJZjAVRLoiVEdlheB9nSgdLHOl1JAthtn0lYovwUBDg4CUTFQ8ttn/rt21iET smmmum2ctDLCpPH7ABwMQDkzhtlb6ZO+UrT7oiBvP+MHEUiQAbrTvpTAOfzRQo6vmPWN c4hw== X-Received: by 10.112.11.136 with SMTP id q8mr8399370lbb.94.1376258427536; Sun, 11 Aug 2013 15:00:27 -0700 (PDT) Original-Received: from warperdoze.localnet (1-1-1-39a.veo.vs.bostream.se. [82.182.254.46]) by mx.google.com with ESMTPSA id k6sm10677339lae.9.2013.08.11.15.00.24 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 11 Aug 2013 15:00:26 -0700 (PDT) User-Agent: KMail/4.9.5 (Linux/3.5.0-30-generic; KDE/4.9.5; x86_64; ; ) X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:4010:c04::22d X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:10621 Archived-At: 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 ( (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!