# Need a bit of help with an instaparse grammar

**URL:** https://clojureverse.org/t/need-a-bit-of-help-with-an-instaparse-grammar/965
**Category:** How to?
**Created:** [November 23, 2017, 8:26pm UTC](https://clojureverse.org/t/need-a-bit-of-help-with-an-instaparse-grammar/965 "2017-11-23T20:26:01Z")
**Posts on this page:** 1
**Showing post:** 4

<div class="post-metadata">

### Author: ![JeremS](https://clojureverse.org/user_avatar/clojureverse.org/jerems/32/3646_2.png) [@JeremS](https://clojureverse.org/u/JeremS)
#### Post date: [November 24, 2017, 12:49pm UTC](https://clojureverse.org/t/need-a-bit-of-help-with-an-instaparse-grammar/965/4 "2017-11-24T12:49:13Z")

</div>

Hey guys thank you for the reply. The ordered choice operator works !

As a conclusion to this thread, here is some code using @floop solution and another suggested to me by Alex Engelberg on the instaparse channel of the clojure slack.

Here is @floop’s solution using PEG’s ordered choice operator in the `doc` rule:

```auto
(insta/defparser ex-with-choice
  "
  doc = (tag / text)*
  text = #'[^@]*'
  tag = <'@'> #'[a-z]\\w*' text-block*
  text-block = <'{'> inner-text <'}'>
  <inner-text> = #'[^}]*'
  ")

```

Alex’s solution uses PEG’s negative lookahead operator:

```auto
(insta/defparser ex-with-lookahead
  "
  doc = (text | tag)*
  text = #'[^@]*'
  tag = <'@'> #'[a-z]\\w*' text-block* !text-block
  text-block = <'{'> inner-text <'}'>
  <inner-text> = #'[^}]*'
  ")

```

The solutions seem to yield the same results

```auto

(= (ex-with-choice "some text @tag1{inner text}{inner text 2} some text 2 @tag2 other text")
   (ex-with-lookahead "some text @tag1{inner text}{inner text 2} some text 2 @tag2 other text"))
;=> true

```

Which is:

```auto
(ex-with-choice "some text @tag1{inner text}{inner text 2} some text 2 @tag2 other text")

;=>
[:doc
 [:text "some text "]
 [:tag "tag1" [:text-block "inner text"] [:text-block "inner text 2"]]
 [:text " some text 2 "]
 [:tag "tag2"]
 [:text " other text"]]

```

The PEG extensions in instaparse are very useful indeed.

Thx again. Cheers,

---

_[View the full topic](https://clojureverse.org/t/need-a-bit-of-help-with-an-instaparse-grammar/965)._
