[fpc-pascal] parser combinator library
Bernd
prof7bit at gmail.com
Fri Feb 24 22:00:10 CET 2017
Hello,
Eight years ago someone asked whether there is a parser combinator library
for free pascal, nothing like that existed at that time and also does not
seem to exist up to the present day.
While I was reading about parser combinators in functional programming
languages (during my 42nd attempt to learn Haskell) I thought to myself why
not try to implement something like that in Object Pascal, just so see how
far we can push the boundaries of this imperative object oriented language.
This is what I have come up with so far:
https://github.com/prof7bit/fpc_parser_combinators
Since we don't have lambdas I choose the next closest approach to emulate
them with object instances instead. This leads to a lot of boiler plate in
the definition of the elementary parsers and combinators but fortunately it
can all be hidden away in the library and the usage of the combinators
looks quite neat:
// define the grammar
EXPR := Num or _PARENS;
MULFUNC := Sym('mul') and EXPR and EXPR;
ADDFUNC := Sym('add') and EXPR and EXPR;
INNER := MULFUNC or ADDFUNC or Num;
PARENS := Sym('(') and INNER and Sym(')');
Please also note the unorthodox usage of and/or operators to invoke the
combinators :-)
Please post improvements or variations of this theme, especially I am
interested in how to properly build up a syntax tree in the most generic
and reusable manner, this is something I have not yet completely understood
(because I am myself still quite unfamiliar with this whole parsing
business) currently all my parsers only return arrays of strings that I can
optionally post-process with an optional hook function after a parser has
completed.
Bernd
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20170224/6af97077/attachment.html>
More information about the fpc-pascal
mailing list