# How does Menhir's option() work?

**URL:** <https://discuss.ocaml.org/t/how-does-menhirs-option-work/13491>\
**Category:** Learning\
**Tags:** menhir\
**Created:** [November 24, 2023, 12:47pm UTC](https://discuss.ocaml.org/t/how-does-menhirs-option-work/13491 "2023-11-24T12:47:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vdolgov](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/vdolgov/32/3430_2.png) [@vdolgov](https://discuss.ocaml.org/u/vdolgov)\
**Post date:** [November 24, 2023, 12:47pm UTC](https://discuss.ocaml.org/t/how-does-menhirs-option-work/13491/1 "2023-11-24T12:47:09Z")

</div>

I’m writing an Objective-C parser with Menhir and cannot figure out why the following rule

```plaintext
| CARET; t = option(LPAREN s=IDENT ASTERISK? RPAREN { s })
LPAREN l = separated_list(COMMA, t=IDENT ASTERISK* s=IDENT { (t, s) }) RPAREN
LBLOCK; b = statement* RBLOCK { Block (t, l, b) }

```

matches

```plaintext
^(NSString *)(ClassA *objA, ClassB *objB) {

}

```

and doesn’t match

```plaintext
^(ClassA *objA, ClassB *objB) {

}

```

Can you please point out the error in my understanding of `option()`?

---

<div class="post-metadata">

**Author:** ![K\_N](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/k_n/32/2793_2.png) [@K\_N](https://discuss.ocaml.org/u/K_N)\
**Post date:** [November 24, 2023, 1:32pm UTC](https://discuss.ocaml.org/t/how-does-menhirs-option-work/13491/2 "2023-11-24T13:32:37Z")

</div>

It _should_ work as you wrote it. But it’s kind of hard to understand without a bit more context. When you say doesn’t match, is it that you get a syntax error or is the phrase parsed by some other rule (which would be a conflict that was arbitrarily resolved in favor of that rule ?).  
And if you print your tokens as they are lexed, do you see them in the right order ?

---

<div class="post-metadata">

**Author:** ![K\_N](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/k_n/32/2793_2.png) [@K\_N](https://discuss.ocaml.org/u/K_N)\
**Post date:** [November 24, 2023, 1:59pm UTC](https://discuss.ocaml.org/t/how-does-menhirs-option-work/13491/3 "2023-11-24T13:59:46Z")

</div>

Actually scratch that, I tested the rule and indeed there is a conflict, when seing the first LPAREN,  
the parser cannot decide which rule should apply (unfold the option, or skip it and start the rest of the rule).

---

<div class="post-metadata">

**Author:** ![ancolie](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ancolie/32/6189_2.png) [@ancolie](https://discuss.ocaml.org/u/ancolie)\
**Post date:** [November 24, 2023, 3:20pm UTC](https://discuss.ocaml.org/t/how-does-menhirs-option-work/13491/4 "2023-11-24T15:20:48Z")

</div>

Not tested but it looks like a case where using `ioption` could help ?

---

<div class="post-metadata">

**Author:** ![K\_N](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/k_n/32/2793_2.png) [@K\_N](https://discuss.ocaml.org/u/K_N)\
**Post date:** [November 24, 2023, 3:28pm UTC](https://discuss.ocaml.org/t/how-does-menhirs-option-work/13491/5 "2023-11-24T15:28:58Z")

</div>

Indeed it works with ioption (for a grammar with just the single rule in the original question).

---

<div class="post-metadata">

**Author:** ![vdolgov](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/vdolgov/32/3430_2.png) [@vdolgov](https://discuss.ocaml.org/u/vdolgov)\
**Post date:** [November 24, 2023, 4:18pm UTC](https://discuss.ocaml.org/t/how-does-menhirs-option-work/13491/6 "2023-11-24T16:18:35Z")

</div>

Yes, in this case inlining solves the problem. Thanks for your prompt help 🙇‍♂️
