GithubHelp home page GithubHelp logo

mmheydari97 / automata-cyk Goto Github PK

View Code? Open in Web Editor NEW
1.0 0.0 2.0 146 KB

Implementation of cyk algorithm with python. It is a parsing algorithm for context-free grammars represented in Chomsky normal form.

License: Apache License 2.0

Python 48.29% TeX 51.71%
automta-theory cyk-algorithm cky-algorithm cyk cky

automata-cyk's Introduction

CYK Algorithm

In computer science, the Cocke–Younger–Kasami algorithm (alternatively called CYK, or CKY) is a parsing algorithm for context-free grammars, named after its inventors, John Cocke, Daniel Younger and Tadao Kasami.It employs bottom-up parsing and dynamic programming.

The standard version of CYK operates only on context-free grammars given in Chomsky normal form (CNF). However any context-free grammar may be transformed to a CNF grammar expressing the same language.

The importance of the CYK algorithm stems from its high efficiency in certain situations. Using Big O notation, the worst case running time of CYK is O ( n^3 ⋅ | G | ), where n is the length of the parsed string and | G | is the size of the CNF grammar G. This makes it one of the most efficient parsing algorithms in terms of worst-case asymptotic complexity, although other algorithms exist with better average running time in many practical scenarios. read more

Pseudo code

First we store the variables producing each character of input as a terminal in any production rule of grammar. then for each substring of input with length 2, we consider produced combination of terminals in right side of production rule and store the left side in the table. then we iteratively fill other cells with the results of previous iterations. finally if the last cell that holds variables which are able to produce the input, contains start symbol, we can conclude that there exists a way to produce that input with the grammar given.

Begin
      for ( i = 1 to n do )
      Vi1 { A | A → a is a production where ith symbol of x is a }

      for ( j = 2 to n do )
           for ( i = 1 to n - j + 1 do )
           Begin
                 Vij = ϕ
                 For k = 1 to j - 1 do
                 Vij = Vij ∪ { A | A → BC is a production where B is in Vik and C is in V(i + k)(j - k) }
           End
End

read more

example

You can find interactive example here.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details

automata-cyk's People

Contributors

mmheydari97 avatar

Stargazers

 avatar

Forkers

ahoho imamsyabana

automata-cyk's Issues

cyk algorithm doesn't add all symbols to the cyk-table

This part of code is wrong:

automata-cyk/cyk_main.py

Lines 96 to 98 in 13723da

for ro in row:
if ro in var1:
table[i][j].add(var0[var1.index(ro)])

Code checks if 'ro' exists in var and if so adds symbol to table cell. But there may be more productions with 'ro' symbols in it, so loop is needed.

Possible solution:

for idx, v in enumerate(var1):
    if v in row:
        table[i][j].add(var0[idx])

It will check all matching productions, not only one.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.