Library
Module
Module type
Parameter
Class
Class type
Transform strings of tokens and mixfix operators into full binary trees. Operators are characterised by their associativity and their fixity.
To parse expressions from type 'i
to type 'o
, you need to tell the parser
'o -> 'o -> 'o
; this function can be seen as the concatenation of two binary trees (and in that case, the input of the parser is a string of leaves);'i
should be considered an operator;'i
that aren't operators.The algorithm implemented is an extension of the Pratt parser. The Shunting Yard algorithm could also be used.
type 't error = [
| `Op_conflict of 't
Priority or associativiy conflict between two operators. In `OpConflict o
, o
is an operator which generates a conflict.
| `Too_few_arguments
More arguments are expected. It is raised for instance on partial application of operators, such as x +
; or when an empty input is given to the parser.
]
Errors that can be encountered while parsing a stream of terms.
module Operators : sig ... end
Values of that type are parsers from sequences of 'tok
to values of type 'out
.
val expression :
appl:('b -> 'b -> 'b) ->
token:('a -> 'b) ->
ops:('a, 'b) Operators.t ->
('a, 'b) parser
expression appl token ops
is a parser from sequences of 'a
to structured values of type 'b
. The parser is driven by the operator parser ops
which determines which tokens are operators. Tokens that aren't operators are parsed using the token
function.
If tokens are seen as leaves of binary trees, the function appl
is the concatenation of two binary trees. If tokens are seen as terms, appl
is the application.
For instance, assuming that +
is declared infix and we're working with numbers, it can transform 3 + 5 × 2
encoded as the stream of terms 3, +,
5, ×, 2
into the binary tree @(@(×,@(@(+,3),5)),2)
where @
denotes application nodes.