zhong-j-yu / rekex

PEG parser generator for Java 17 - grammar as algebraic datatypes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rekex

PEG parser generator for Java 17

grammar as algebraic datatypes

A context-free grammar has the form of

A  = A1 | A2
A1 = B C
...

which looks exactly like definitions of algebraic datatypes. This is by no coincidence - both formalisms reflect an underlying model with which we build complex concepts from constituents.

Given this correspondence, we could express a context-free grammar entirely as datatypes in a programming language. For example, in Java 17,

sealed interface A permits A1, A2{}

record A1(B b, C c) implements A{}

Such datatypes can be transliterated into a grammar, which is fed to a particular parser generator to build a parser. During the parsing process, constructors of datatypes are invoked, eventually outputting a parse tree in the very same datatypes.

PegParser<A> parser = PegParser.of(A.class);

A a = parser.matchFull(input);

Rekex is a PEG parser generator that implements this novel idea for Java 17. It is the simplest, most intuitive way for writing parsers.

Read More:


Create by Zhong Yu

About

PEG parser generator for Java 17 - grammar as algebraic datatypes

License:Apache License 2.0


Languages

Language:Java 100.0%