Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

Commit

Permalink
Ah, right associative..
Browse files Browse the repository at this point in the history
  • Loading branch information
Håkan Råberg committed Apr 12, 2013
1 parent 839e85f commit 85af3f5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -205,7 +205,7 @@ Mímir now also contains an [experimental parser](https://github.com/hraberg/mim
See [`mimir.test.parse`](https://github.com/hraberg/mimir/blob/master/test/mimir/test/parse.clj) for examples (but an absolute lack of proper tests). Many things doesn't work properly yet, and the theoretical foundations are shaky to say the least. It doesn't support left-recursion - and a few things are broken. I'm currently backing off to read a few papers, so the references list will hopefully be updated in a few days, once I understand more about what I don't understand.

```clojure
(def right-recursive
(def right-recursive ;; Note: right associative.
(create-parser ;; This returns a parser function.
{:suppress-tags true} ;; Options, can also be given when invoking, see below.

Expand Down
19 changes: 12 additions & 7 deletions test/mimir/test/parse.clj
Expand Up @@ -37,19 +37,19 @@
;; | Decimal
;; Decimal → '0' | ... | '9'

;; Should arguebly use choice instead of #{}
(def peg-expression (create-parser
{:suppress-tags true}

:additive #{[:multitive #"[+-]" :additive]
:multitive} op
:multitive #{[:primary #"[*/]" :multitive]
:primary} op
:primary #{["(" :additive ")"]
:decimal}
:additive (choice [:multitive #"[+-]" :additive]
:multitive) op
:multitive (choice [:primary #"[*/]" :multitive]
:primary) op
:primary (choice ["(" :additive ")"]
:decimal)
:decimal #"[0-9]+" read-string))

;; This gets wrong precedence, regardless of using choice / OrderedSet or not. So something else.
;; This also seems right associtive?
;; Should return 33.
(peg-expression "1-2/(3-4)+5*6")

Expand Down Expand Up @@ -111,6 +111,11 @@
(right-recursive "1-2/(3-4)+5*6")
;; Gives -27, should be 33.

;; Here's a simpler tree that fails:
;; Gives -4, should be 2.
(right-recursive "1-2+3")
;; Actually, the slides says: "Note: This grammar is right-associative." !

;; But the tree is much nicer, and could hint to the answer:
(right-recursive "1-2/(3-4)+5*6" :grammar-actions false :suppress-tags false)

Expand Down

0 comments on commit 85af3f5

Please sign in to comment.