fold-lang / pratt

Top down operator precedence parser (also known as Pratt parser) implementation for OCaml. (Unreleased)
ISC License
11 stars 1 forks source link

How to stop? #2

Closed rizo closed 9 years ago

rizo commented 10 years ago

Decide how the parsing ends.

rizo commented 10 years ago

Currently the parser stops by checking if the current token is the end token. Using end token handler may be a better solution but with current handler typing its unnecessarily complicated.

rizo commented 9 years ago

Fixed in 3996866.

The solution adds explicit Start and End token handlers and includes them in the final AST by defining the root expression.

-> x = -a + b * c + d
 = (root (= (var x) (+ (+ (- (var a)) (* (var b) (var c))) (var d))))

The default action of End as an infix handler is to return the expression (note: It will be further abstracted as a postfix handler). On the other hand, if interpreted as an atomic handler it denotes an empty expressions which, in case of an empty input, is perfectly fine, but in other cases it means that the expression is incomplete. This is shown in the following example:

-> 2 +
 = (root (+ 2 (end)))

The consequences of this interpretation is that the consistency validation must be performed as a separate step.