Grammar
From Gallium
Grammars take parts of Camlp4 Extensible Parser system.
Grammars are extensible trough Grammar Extensions, that mutate the internal structure in order to recognize the extended language.
Make a new grammar
Internally there is an OCaml that hold the grammar structure. However for a safer manipulation, grammars are encapsulated (and hide) in an OCaml module.
EXTEND statements takes a module to extend, such a module should respect the Camlp4.Sig.Grammar.Static signature.
The Camlp4 implementation provides the functor Camlp4.Struct.Grammar.Static to do that. The hard part here is to define a lexer module that respect the Camlp4.Sig.Lexer signature.
module Token = ...;; module Lexer = ...;; module Gram = Camlp4.Struct.Grammar.Static Lexer;; let expr = Gram.Entry.mk "expression";; ... EXTEND Gram ... END;;
For those that wants to keep the same lexer and token type (that are quite general), it's simpler.
open Camlp4.PreCast;; module MyGram = MakeGram Lexer;; let expr = MyGram.Entry.mk "expression";; ... EXTEND MyGram ... END;;
Most of the time one just want to reuse the same implementation for that module
Differences with <=3.09
There was two styles classical and functorised grammars.
Functorized ones where extended with GEXTEND instead of EXTEND, witch no longer exists in >=3.10.
Now all grammars are functorized.