cyk ~main

context-free grammar parser using the CYK algorithm


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

cyk

A context-free grammar parser using the Cocke–Younger–Kasami algorithm.

Build codecov dub dub

Installation

Add cyk to your project with

dub add cyk

Usage

import std.string : split;
import cyk : CYK;

void main() {
    // example parsing a function declaration with arbitrary many parameter
    string[] rules = `
        FunctionDeclaration → ReturnType FunctionName "(" OptionalParameters ")"
        ReturnType → Type
        Type → IntType | FloatingType | "void" | "string"
        IntType → "int" | "long"
        FloatingType → "float" | "double"
        FunctionName → "foo" | "bar" | "baz"
        OptionalParameters → Parameters
        OptionalParameters →
        Parameters → Type ParameterName | Type ParameterName "," Parameters
        ParameterName → "a" | "b" | "c"
    `.split("\n");

    CYK cyk = new CYK(rules, "FunctionDeclaration");

    // check some valid function declarations
    // with the method `bool check(string[])`
    assert(cyk.check("double foo ( )".split));
    assert(cyk.check("string bar ( int a )".split));
    assert(cyk.check("void baz ( int a , float b , long c )".split));

    // check some invalid function declarations
    assert(!cyk.check("double foo ( int )".split));
    assert(!cyk.check("string ( int a )".split));
    assert(!cyk.check("void baz ( int a float b , long c )".split));

    // another simpler example
    string[] rules2 = `
        A → "a"
        B → "b"
        Double → A A | B B
        Different → A B | B A
        Expr → Double ">" Different | Different "<" Double
    `.split("\n");

    CYK cyk2 = new CYK(rules2, "Expr");

    // if all terminals are single chars, you can also use the simpler `bool check(string)` method
    assert(cyk2.check("ab<aa"));
    assert(!cyk2.check("bb<bb"));
}
Authors:
  • Jakob Kogler
Dependencies:
none
Versions:
1.1.0 2021-Jan-16
1.0.2 2021-Jan-16
1.0.1 2021-Jan-16
1.0.0 2021-Jan-16
~main 2021-Jan-16
Show all 5 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 0 downloads this month

  • 14 downloads total

Score:
0.5
Short URL:
cyk.dub.pm