package pratter
Module PratterSource
Parse expressions with infix, prefix or postfix operators.
To parse expressions from type 'i to type 'o, you need to tell the parser
- how to concatenate two expressions with a function of type
'o -> 'o -> 'o; - how to determine whether a value of
'ishould be considered an operator; - how to parse tokens of
'ithat aren't operators.
The algorithm implemented is an extension of the Pratt parser. The Shunting Yard algorithm could also be used.
Associativity of an operator.
type fixity = | Infix of associativity(*Infix operator like
*)+inx + y.| Prefix(*Prefix operator like
*)!in! x.| Postfix(*Postfix operator like
*)^inx ^.
The fixity of an operator
type 't error = [ | `Op_conflict of 't(*Priority or associativiy conflict between two operators. In
*)`OpConflict o,ois 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.
Parsers from sequences of 'tok to values of type 'out.
val expression :
appl:('b -> 'b -> 'b) ->
token:('a -> 'b) ->
ops:('a -> (fixity * float * 'b) list) ->
('a, 'b) parserexpression appl token ops is a parser from sequences of 'a to values of type 'b. The parser is driven by the operator table ops which determines which tokens are operators. A token t can be used as an operator if ops t isn't empty. Each value (f, p, s) of ops t means that token t can be parsed as token s with fixity f and priority p.
The function appl is used to combine two expressions.
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. Such a parsing could be produced by the parameters ops and appl such that (types of tokens are schematised for simplicity)
ops + = [Infix Left, 1.0, +]andappl x y = @(x, y).