Skip to content

Rekex - grammar rules as algebraic datatypes

License

Notifications You must be signed in to change notification settings

third-bank-of-the-river/rekex

 
 

Repository files navigation

Rekex - grammar rules as algebraic datatypes

A context-free grammar has the form of

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

which looks very much 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 implements this novel idea for Java 17, addressing issues of practical concerns, proving that it is an elegant approach to making parsers.

Read More:


Create by Zhong Yu. I am looking for a Java job; helps appreciated.

About

Rekex - grammar rules as algebraic datatypes

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%