Note: project under heavy development!
UCFS is an Universal Context-Free Solver: a tool to solve problems related to context-free and regular language intersection. Examples of such problems:
- Parsing
- Context-free path querying (CFPQ)
- Context-free language reachability (CFL-R)
├── solver -- base ucfs logic
├── benchmarks -- comparison with antlr4
├── generator -- parser and ast node classes generator
├── examples -- examples of grammars
└── test-shared -- test cases
└── src
└── test
└── resources -- grammars' description and inputs
UCFS is based on Generalized LL (GLL) parsing algorithm modified to handle language specification in form of Recursive State Machines (RSM-s) and input in form of arbitratry directed edge-labelled graph. Basic ideas described here.
Kotlin DSL for describing context-free grammars.
Example for A* grammar
EBNF
A = "a"
S = A*
DSL
class AStar : Grammar() {
var A = Term("a")
val S by Nt().asStart(many(A))
}
val S by Nt()
Non-terminals must be fields of the grammar class. Make sure to declare using delegation by Nt()
!
Start non-terminal set with method setStart(nt)
. Or in initialization with Nt method asStart
.
Can be set only once for grammar.
val A = Term("a")
val B = Term(42)
Terminal is a generic class. Can store terminals of any type. Terminals are compared based on their content.
They can be declared as fields of a grammar class or directly in productions.
Example for Dyck language
EBNF
S = S1 | S2 | S3 | ε
S1 = '(' S ')' S
S2 = '[' S ']' S
S3 = '{' S '}' S
DSL
class DyckGrammar : Grammar() {
val S by Nt().asStart()
val Round by Nt("(" * S * ")")
val Quadrat by Nt("[" * S * "]")
val Curly by Nt("{" * S * "}")
init {
//recursive nonterminals initialize in `init` block
S /= S * (Round or Quadrat or Curly) or Epsilon
}
}
$A \Longrightarrow B \hspace{4pt} \overset{def}{=} \hspace{4pt} AbyNt(B)$
Epsilon -- constant terminal with behavior corresponding to the
DSL provides access to the RSM corresponding to the grammar using the getRsm
method.
The algorithm for RSM construction is based on Brzozowski derivations.