Ocamlyacc takes as input a context-free grammar specification and produces a Ocaml-language function that recognizes correct instances of the grammar. The Ocamlyacc grammar input file conventionally has a name ending in .mly. See Invoking Ocamlyacc.
A Ocamlyacc grammar file has four main sections, shown here with the appropriate delimiters:
Symbols in Ocamlyacc grammars represent the grammatical classifications of the language. A terminal symbol (also known as a token type) represents a class of syntactically equivalent tokens. A nonterminal symbol stands for a class of syntactically equivalent groupings.
A Ocamlyacc grammar rule has the following general form:
A rule is called recursive when its result nonterminal appears also on its right hand side.
The grammar rules for a language determine only the syntax. The semantics are determined by the semantic values associated with various tokens and groupings, and by the actions taken when various groupings are recognized.
Though grammar rules and semantic actions are enough to write a fully functional parser, it can be useful to process some additionnal informations, especially symbol locations.
The Ocamlyacc declarations section of a Ocamlyacc grammar defines the symbols used in formulating the grammar and the data types of semantic values. All token type must be declared. Nonterminal symbols must be declared if you need to specify which data type to use for the semantic value.
The Ocamlyacc declarations section of a Ocamlyacc grammar defines the symbols used in formulating the grammar and the data types of semantic values.