# Help an ocamlyacc beginner

**URL:** <https://discuss.ocaml.org/t/help-an-ocamlyacc-beginner/4459>\
**Category:** Learning\
**Created:** [September 28, 2019, 7:54pm UTC](https://discuss.ocaml.org/t/help-an-ocamlyacc-beginner/4459 "2019-09-28T19:54:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![phstrauss](https://avatars.discourse-cdn.com/v4/letter/p/4da419/32.png) [@phstrauss](https://discuss.ocaml.org/u/phstrauss)\
**Post date:** [September 28, 2019, 7:54pm UTC](https://discuss.ocaml.org/t/help-an-ocamlyacc-beginner/4459/1 "2019-09-28T19:54:45Z")

</div>

Hello ocamlers!

For a side project, I need a config file format both readable in ocaml and C or C++.  
For now I’ve settled on using Libconfig : [https://hyperrealm.github.io/libconfig/](https://hyperrealm.github.io/libconfig/)  
It’s a C built library for parsing a custom but very versatile config file format.

The BNF grammar of this file format is documented at page 49 of the following .PDF : [http://www.strauss-engineering.ch/tmp/libconfig.pdf](http://www.strauss-engineering.ch/tmp/libconfig.pdf)

BTW, under this URI : [http://www.strauss-engineering.ch/tmp/](http://www.strauss-engineering.ch/tmp/)  
you’ll find :

- libconfig doco : libconfig.pdf
- first attempt at a lexer: config\_lexer.mll
- first attempt at a parser : config\_parser.mly (\*)
- first attempt at ocaml typing of the parser result: configuration.mli
- libconfig configuration files examples : \*.cfg
- parser\_test.ml : a basic test for the parser, using a generic printing lib.
- build\_parser.sh : a build script

I can’t figure out how to type the parser result using a standard variant type.  
I’m totally noob about GADT’s and polymorphic variant BTW.

Can someone help with the ocaml typing? Thanks in advance.

(\*) the ocamlyacc parser is a subset of libconfig one’s: I’ve dropped the heterogenous list support on the ocaml side.

best regards!

---

<div class="post-metadata">

**Author:** ![octachron](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@octachron](https://discuss.ocaml.org/u/octachron)\
**Post date:** [September 28, 2019, 8:14pm UTC](https://discuss.ocaml.org/t/help-an-ocamlyacc-beginner/4459/2 "2019-09-28T20:14:17Z")

</div>

What is the issue more specifically? From your message, it sounds like you get a type checking error, which one? Normally, a big enough variant type should suffice for most parsing needs.

---

<div class="post-metadata">

**Author:** ![SkySkimmer](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/skyskimmer/32/1672_2.png) [@SkySkimmer](https://discuss.ocaml.org/u/SkySkimmer)\
**Post date:** [September 28, 2019, 8:22pm UTC](https://discuss.ocaml.org/t/help-an-ocamlyacc-beginner/4459/3 "2019-09-28T20:22:07Z")

</div>

The spec says `Group` is `setting list` not `value list`.  
Additional nitpicking: I would parse hex encoded values directly to their interpretation, also I prefer shorter names for types (no `_t` or `_kind_t`) and for the string value constructor.

So I would do

```ocaml
type scalar =
| Bool of bool
| Integer of int
| Integer64 of int64
| Float of float
| String of string

type value =
| Scalar of scalar
| Array of scalar list
| Group of setting list

and setting = string * value

type t = setting list

```

---

<div class="post-metadata">

**Author:** ![SkySkimmer](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/skyskimmer/32/1672_2.png) [@SkySkimmer](https://discuss.ocaml.org/u/SkySkimmer)\
**Post date:** [September 28, 2019, 8:23pm UTC](https://discuss.ocaml.org/t/help-an-ocamlyacc-beginner/4459/4 "2019-09-28T20:23:12Z")

</div>

Also in the future instead of a directory url give a .zip url, it’s easier to download (my naive wget grabbed a bunch of extra stuff from your server before I killed it ;))

---

<div class="post-metadata">

**Author:** ![phstrauss](https://avatars.discourse-cdn.com/v4/letter/p/4da419/32.png) [@phstrauss](https://discuss.ocaml.org/u/phstrauss)\
**Post date:** [September 28, 2019, 8:29pm UTC](https://discuss.ocaml.org/t/help-an-ocamlyacc-beginner/4459/5 "2019-09-28T20:29:30Z")

</div>

./build\_parser.sh

46 states, 1240 transitions, table size 5236 bytes  
4 rules never reduced

File “config\_parser.mly”, line 68, characters 36-51:  
Error: This expression has type Configuration.setting list  
but an expression was expected of type Configuration.value\_kind\_t list  
Type Configuration.setting = string \* Configuration.value\_kind\_t  
is not compatible with type Configuration.value\_kind\_t  
line 68 of the .mly:

group:  
| LBRACE RBRACE { [] }  
line 68–\> | LBRACE setting\_list RBRACE { List.rev $2 }  
;

---

<div class="post-metadata">

**Author:** ![phstrauss](https://avatars.discourse-cdn.com/v4/letter/p/4da419/32.png) [@phstrauss](https://discuss.ocaml.org/u/phstrauss)\
**Post date:** [September 28, 2019, 10:28pm UTC](https://discuss.ocaml.org/t/help-an-ocamlyacc-beginner/4459/6 "2019-09-28T22:28:10Z")

</div>

the previous msg from myself was for octachron.

Thanks SkySkimmer, didn’t know the “type … _and_ …”, that was the piece missing.

Now I’ve got something that build fine but totally mute, it output nothing yet, stay tuned for more adventure 🙂

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [September 29, 2019, 12:22am UTC](https://discuss.ocaml.org/t/help-an-ocamlyacc-beginner/4459/7 "2019-09-29T00:22:58Z")

</div>

I’m sure this answer isn’t going to be useful, but just in case:

It’s possible that you’re spending a lot of effort, where if you were to choose a different config-file format, you could avoid almost all of it. [OTOH, perhaps you’re forced to use this format, in which case this comment is useless.]

If you can choose another format, you might choose INI-files, or YAML, or even (esp!) JSON; in each of these cases, there’s well-developed libraries in most languages for this format, including in Ocaml.

Just a thought.

P.S. I find JSON to be really nice, and esp. b/c the ubiquity of the format means it’ll be vanishingly rare for a language to not have a JSON interface library.

---

<div class="post-metadata">

**Author:** ![phstrauss](https://avatars.discourse-cdn.com/v4/letter/p/4da419/32.png) [@phstrauss](https://discuss.ocaml.org/u/phstrauss)\
**Post date:** [September 29, 2019, 11:20am UTC](https://discuss.ocaml.org/t/help-an-ocamlyacc-beginner/4459/8 "2019-09-29T11:20:42Z")

</div>

Sure JSON would do fine, but I want to limit the number of runtime lib dependencies somehow.
