|
1 |
| -# CYK-algorithm |
2 |
| -Java implementation of the CYK algorithm |
| 1 | +# CYK Algorithm |
| 2 | + |
| 3 | +## A Java implementation of the CYK-Algorithm. |
| 4 | + |
| 5 | +The CYK-Algorithm can be used to check if a word can be derived from a CFG (context-sensitive grammar). |
| 6 | + |
| 7 | +You only need you grammar to be in the CNF (Chomsky normal form) format. This Java application will parse an external grammar file and then output the result visualized in a table. |
| 8 | + |
| 9 | +## Grammar File |
| 10 | + |
| 11 | +A grammar file will have the following structure. |
| 12 | + |
| 13 | +``` |
| 14 | +S -> Starting Symbol |
| 15 | +a b -> Terminals |
| 16 | +S A B E C X Y Z -> Non-Terminals |
| 17 | +S YB XA * -> 4th and all following lines are production rules |
| 18 | +E YB XA -> You can add multiple to one terminal if you seperate them by a space |
| 19 | +A a YE XC -> This reads as A -> a | YE | XC |
| 20 | +B b XE YZ |
| 21 | +C AA |
| 22 | +X b |
| 23 | +Y a |
| 24 | +Z BB |
| 25 | +``` |
| 26 | + |
| 27 | +After you compiled the .java file you can simply run it via |
| 28 | + |
| 29 | +``` |
| 30 | +java CYK <GrammarFile> <Word> |
| 31 | +``` |
| 32 | + |
| 33 | +Sample output for the supplied grammar above: |
| 34 | + |
| 35 | +``` |
| 36 | +$ java CYK grammar.txt abbbabaa |
| 37 | +Word: abbbabaa |
| 38 | +
|
| 39 | +G = ({a, b}, {S, A, B, E, C, X, Y, Z}, P, S) |
| 40 | +
|
| 41 | +With Productions P as: |
| 42 | +A -> a | YE | XC |
| 43 | +B -> b | XE | YZ |
| 44 | +C -> AA |
| 45 | +E -> YB | XA |
| 46 | +S -> YB | XA | * |
| 47 | +X -> b |
| 48 | +Y -> a |
| 49 | +Z -> BB |
| 50 | +
|
| 51 | +Applying CYK-Algorithm: |
| 52 | +
|
| 53 | ++-------+-------+-------+-------+-------+-------+-------+-------+ |
| 54 | +| a | b | b | b | a | b | a | a | |
| 55 | ++-------+-------+-------+-------+-------+-------+-------+-------+ |
| 56 | +| A,Y | B,X | B,X | B,X | A,Y | B,X | A,Y | A,Y | |
| 57 | ++-------+-------+-------+-------+-------+-------+-------+-------+ |
| 58 | +| E,S | Z | Z | E,S | E,S | E,S | C | |
| 59 | ++-------+-------+-------+-------+-------+-------+-------+ |
| 60 | +| B | - | B | B | A | A | |
| 61 | ++-------+-------+-------+-------+-------+-------+ |
| 62 | +| Z | Z | Z | E,S | C | |
| 63 | ++-------+-------+-------+-------+-------+ |
| 64 | +| B | - | B | A | |
| 65 | ++-------+-------+-------+-------+ |
| 66 | +| Z | Z | E,S | |
| 67 | ++-------+-------+-------+ |
| 68 | +| B | B | |
| 69 | ++-------+-------+ |
| 70 | +| E,S | |
| 71 | ++-------+ |
| 72 | +
|
| 73 | +The word abbbabaa is an element of the CFG G and can be derived from it. |
| 74 | +``` |
0 commit comments