OCAMLYACC(1)

πŸ“¦
version 5.5.0 Β· 2 versions
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, centos-stream 10, debian trixie, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ‘‘ OCAMLYACC β€” the OCaml parser generator, bestie πŸ’…

okay so picture this: it's ocamlyacc(1), from the General Commands Manual, and she is literally the parser generator for OCaml. she takes your grammar and turns it into an actual parser. no cap. ✨


πŸ¦‹ SYNOPSIS πŸ¦‹

ocamlyacc [ -b prefix ] [ -q ] [ -v ] [ -version ] [ -vnum ]
filename.mly

🌸 DESCRIPTION 🌸

so the ocamlyacc(1) command produces a parser from a LALR(1) context-free grammar specification with attached semantic actions β€” yes, in the style of yacc(1), she's giving classic but make it OCaml πŸ’–

say your input file is grammar.mly. run ocamlyacc on it and boom: you get OCaml code for a parser in the file grammar.ml, plus its interface in the file grammar.mli. iconic behaviour tbh.

the generated module defines one parsing function per entry point in the grammar. these functions have the same names as the entry points. main character energy, matching names and everything πŸ‘‘

and what do those parsing functions take? listen:

  • a lexical analyzer β€” a function from lexer buffers to tokens
  • a lexer buffer

and they return the semantic attribute of the corresponding entry point. that's the whole vibe.

quick lore drop: lexical analyzer functions are usually generated from a lexer specification by the ocamllex(1) program. lexer buffers are an abstract data type implemented in the standard library module Lexing. and tokens are values from the concrete type token, defined in the interface file grammar.mli produced by ocamlyacc(1). it's all connected, iykyk 🧚


✨ OPTIONS ✨

the ocamlyacc(1) command recognizes the following options, and we're going through every single one because we don't skip πŸ’…

  • -b prefix β€” name the output files prefix.ml, prefix.mli, prefix.output, instead of doing the default naming convention thing. custom names, your rules πŸŽ€
  • -q β€” ngl bestie this option has no effect. that's it. that's the whole option. she's just there 🫢
  • --strict β€” reject grammars with conflicts. no conflict-having in this house, periodt 🚫
  • -v β€” generate a description of the parsing tables AND a report on conflicts resulting from ambiguities in the grammar. the description gets put in the file grammar.output. receipts!! πŸ”₯
  • -version β€” print version string and exit. bye πŸ‘‹
  • -vnum β€” print short version number and exit. shorter bye πŸ’¨
  • - β€” read the grammar specification from standard input. the default output file names in that case are stdin.ml and stdin.mli. fed straight from stdin, we love a flexible queen 🌈
  • -- file β€” process file as the grammar specification, even if its name starts with a dash (-) character.
⚠️ heads up: this option must be the last one on the command line. not negotiable, it goes last, that's the rule 😭

πŸ’œ SEE ALSO πŸ’œ

  • ocamllex(1) β€” her bestie fr fr
  • The OCaml user's manual, chapter "Lexer and parser generators" πŸ“–

and that's the page!! πŸŽ€ generated straight from the General Commands Manual, OCAMLYACC(1), and dated OCAMLYACC(1) β€” wait no, that's the footer, which is also OCAMLYACC(1). anyway, go build a parser bestie, you're gonna ate πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from