Chapter 3. Format of the input file

The ocamllex input file consists of four sections; header, definitions, rules and trailer section:


	(* header section *)
	{ header }

	(* definitions section *)
	let ident = regexp
	let ...	

	(* rules section *)
	rule entrypoint [arg1... argn] = parse
	  | pattern { action }
 	  | ...
 	  | pattern { action }
	and entrypoint [arg1... argn] = parse
  	  ...
	and ...

	(* trailer section *)
	{ trailer }

Comments are delimited by (* and *), as in Caml.

The header and rules sections are necessary while definitions and trailer sections are optional.

The header and trailer sections are enclosed in curly braces and they contain arbitrary Caml code. At the beginning of the output file, the header text is copied as is while the trailer text is copied at the end of the output file. For example, you can code open directives and some auxiliary funtions in the header section.

The definitions section contains declarations of simple ident definitions to simplify the scanner specification. Ident definitions have the form:


	let ident = regexp
	let ...	

The "ident" must be valid identifiers for Caml values (starting with a lowercase letter). For example,


	let digit = ['0'-'9']
	let id = ['a'-'z']['a'-'z' '0'-'9']*

defines "digit" to be a regular expression which matches a single digit, and "id" to be a regular expression which matches a letter followed by zero-or-more letters-or-digits. A subsequent reference to


	digit+ "." digit*

is identical to


	['0'-'9']+ "." ['0'-'9']*

and matches one-or-more digits followed by a '.' followed by zero-or-more digits.

The rules section of the ocamllex input contains a series of entrypoints of the form:


	rule entrypoint [arg1... argn] = parse
	  | pattern { action }
 	  | ...
 	  | pattern { action }
	and ...

The first | (bar) after parse is optional.

Each entrypoint consists of a series of pattern-action:


	  | pattern { action }

where the action must be enclosed in curly braces.

See below for a further description of patterns and actions.