https://github.com/valkryst/vparser_cfg
A library to parse and run Context Free Grammars.
https://github.com/valkryst/vparser_cfg
automaton cfg cfg-rules context-free-grammar grammar transition
Last synced: 8 months ago
JSON representation
A library to parse and run Context Free Grammars.
- Host: GitHub
- URL: https://github.com/valkryst/vparser_cfg
- Owner: Valkryst
- License: apache-2.0
- Created: 2017-06-23T17:31:26.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2025-06-10T15:06:09.000Z (9 months ago)
- Last Synced: 2025-06-10T16:33:51.214Z (9 months ago)
- Topics: automaton, cfg, cfg-rules, context-free-grammar, grammar, transition
- Language: Java
- Homepage:
- Size: 128 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README

## Table of Contents
* [Installation](https://github.com/Valkryst/VParser_CFG#installation)
* [Gradle](https://github.com/Valkryst/VParser_CFG#-gradle)
* [Maven](https://github.com/Valkryst/VParser_CFG#-maven)
* [sbt](https://github.com/Valkryst/VParser_CFG#-scala-sbt)
* [Usage](https://github.com/Valkryst/VParser_CFG#usage)
* [Notes](https://github.com/Valkryst/VParser_CFG#usage)
* [Example](https://github.com/Valkryst/VParser_CFG#usage)
## Installation
VParser_CFG is hosted on the [JitPack package repository](https://jitpack.io/#Valkryst/VParser_CFG)
which supports Gradle, Maven, and sbt.
###  Gradle
Add JitPack to your `build.gradle` at the end of repositories.
```
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```
Add VParser_CFG as a dependency.
```
dependencies {
implementation 'com.github.Valkryst:VParser_CFG:2024.03.24'
}
```
###  Maven
Add JitPack as a repository.
``` xml
jitpack.io
https://jitpack.io
```
Add VParser_CFG as a dependency.
```xml
com.github.Valkryst
VParser_CFG
2024.03.24
```
###  Scala SBT
Add JitPack as a resolver.
```
resolvers += "jitpack" at "https://jitpack.io"
```
Add VParser_CFG as a dependency.
```
libraryDependencies += "com.github.Valkryst" % "VParser_CFG" % "2024.03.24"
```
## Usage
1. Create a set of rules.
2. Create a `ContextFreeGrammar` using the rules.
3. Run the `ContextFreeGrammar`.
### Notes
A `ContextFreeGrammar` requires at least one rule, and each rule must begin with
a terminal and be followed by zero or more non-terminals. These are examples
of valid rules:
```
A aA aB aC a
B bA bC b
c
A thisA isB
B aA aC
C validA ruleB c
```
### Example
```java
public class Example {
public static void main(final String[] args) {
/*
* The following set of rules was created using these names:
*
* Balin, Bifur, Bofur, Bombur, Borin, Dain, Dis, Dori, Dwalin,
* Farin, Fili, Floi, Frar, Frerin, Fror, Fundin, Gaiml, Gimli,
* Gloin, Groin, Gror, Ibun, Khim, Kili, Loni, Mim, Nain, Nali,
* Nar, Narvi, Nori, Oin, Ori, Telchar, Thorin, Thrain, Thror
*/
final var rules = new String[]{
"A B C D E F G H I K L M N O P Q R S T U V W X Y Z",
"A a aL aI aR",
"B b bA bI bO",
"C c",
"D d dA dI dO dW dU",
"E e eR eL",
"F f fA fI fL fR fU fO",
"G g gA gI gL gR",
"H h hI hA",
"I i",
"K k kH kI",
"L l lO",
"M m mI",
"N n nA nO",
"O o oI oR",
"P p",
"Q q",
"R r rI rO rV",
"S s",
"T t tE tH",
"U u uR uN",
"V v",
"W w wA",
"X x",
"Y y",
"Z z"
};
final var cfg = new ContextFreeGrammar(rules);
for (int i = 0 ; i < 1000 ; i++) {
String res = cfg.run();
// Exclude low-quality results.
if (res.length() > 3) {
System.out.println(res);
}
}
}
}
```
```
guroro
hfkh
eroi
forv
oror
bunoroi
erorv
gnoi
thlo
glorv
oroi
lori
loroi
hter
wurv
funo
elori
thhi
furi
norv
ungrv
unoi
teri
dher
loro
froi
...
```