Grammar Files

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.

    Grammar outline

    A Ocamlyacc grammar file has four main sections, shown here with the appropriate delimiters:

    Symbols, Terminal and Nonterminal

    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.

    Syntax of grammar rules

    A Ocamlyacc grammar rule has the following general form:

    Recursive Rules

    A rule is called recursive when its result nonterminal appears also on its right hand side.

    Defineing Language Semantics

    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.

    Tracking Locations

    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.

    Nonterminal Symbols

    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.

    Ocamlyacc Declarations

    The Ocamlyacc declarations section of a Ocamlyacc grammar defines the symbols used in formulating the grammar and the data types of semantic values.

comments powered by Disqus