Camlp4
Camlp4 is a software system for writing extensible parsers for programming languages.[src] It provides a set of OCaml libraries that are used to define grammars as well as loadable syntax extensions of such grammars.[src] Camlp4 stands for Caml Preprocessor and Pretty-Printer and one of its most important applications was the definition of domain-specific extensions of the syntax of OCaml.[src] Camlp4 was part of the official OCaml distribution which is developed at the INRIA.[src] Its original author is Daniel de Rauglaudre.[src] OCaml version 3.10.0, released in May 2007, introduced a significantly modified and backward-incompatible version of Camlp4.[src] De Rauglaudre maintains a separate backward-compatible version, which has been renamed Camlp5.[src] All of the examples below are for Camlp5 or the previous version of Camlp4 (versions 3.09 and prior).[src] Version 4.08, released in the summer of 2019, was the last official version of this library.[src] It is currently deprecated; instead, it is recommended to use the PPX (PreProcessor eXtensions) libraries.[src]
Camlp4 is a software system for writing extensible parsers for programming languages.[src] It provides a set of OCaml libraries that are used to define grammars as well as loadable syntax extensions of such grammars.[src] Camlp4 stands for Caml Preprocessor and Pretty-Printer and one of its most important applications was the definition of domain-specific extensions of the syntax of OCaml.[src] Camlp4 was part of the official OCaml distribution which is developed at the INRIA.[src] Its original author is Daniel de Rauglaudre.[src] OCaml version 3.10.0, released in May 2007, introduced a significantly modified and backward-incompatible version of Camlp4.[src] De Rauglaudre maintains a separate backward-compatible version, which has been renamed Camlp5.[src] All of the examples below are for Camlp5 or the previous version of Camlp4 (versions 3.09 and prior).[src] Version 4.08, released in the summer of 2019, was the last official version of this library.[src] It is currently deprecated; instead, it is recommended to use the PPX (PreProcessor eXtensions) libraries.[src] let unique = let n = ref 0 in fun () -> incr n; "__pa_memo" ^ string_of_int !n EXTEND GLOBAL: Pcaml.expr; Pcaml.expr: LEVEL "expr1" [ [ "memo"; OPT "|"; pel = LIST1 match_case SEP "|" -> let tbl = unique () in let x = unique () in let result = unique () in try Hashtbl.find $lid:tbl$ $lid:x$ with [ Not_found -> let $lid:result$ = match $lid:x$ with [ $list:pel$ ] in do { Hashtbl.replace $lid:tbl$ $lid:x$ $lid:result$; $lid:result$ } ] >> ] ]; match_case: [ [ p = Pcaml.patt; w = OPT [ "when"; e = Pcaml.expr -> e ]; "->"; e = Pcaml.expr -> (p, w, e) ] ]; END Example of program using this syntax extension: let counter = ref 0 (* global counter of multiplications *) (* factorial with memoization *) let rec fac = memo 0 -> 1 | n when n > 0 -> (incr counter; n * fac (n - 1)) | _ -> invalid_arg "fac" let run n = let result = fac n in let count = !counter in Printf.printf "%i![src] = %i number of multiplications so far = %i\n" n result count let _ = List.iter run [5; 4; 6] The output of the program is as follows, showing that the fac function (factorial) only computes products that were not computed previously: 5![src] = 120 number of multiplications so far = 5 4![src] = 24 number of multiplications so far = 5 6![src] = 720 number of multiplications so far = 6[src]
25 sentences · 25 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Camlp4." encyclopegia.org, 2026, https://encyclopegia.org/wiki/camlp4. Accessed 01 December 2026.
Chicago
"Camlp4." encyclopegia.org. Accessed December 1, 2026. https://encyclopegia.org/wiki/camlp4.
APA
encyclopegia.org. (2026). Camlp4. Retrieved December 1, 2026, from https://encyclopegia.org/wiki/camlp4
Accessed December 1, 2026 · CC BY-SA 4.0 · citation page · JSON
Claims
| Predicate | Value | Source |
|---|---|---|
| release date | 2007 | Wikipedia |
| release date | 2019 | Wikipedia |