Coder Social home page Coder Social logo

gaetanserre / formal-language-algorithms Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 66 KB

Algorithms for formal language written with Python 3 with nltk

License: GNU General Public License v3.0

Python 100.00%
formal-language ll-parser nltk cyk-algorithm

formal-language-algorithms's Introduction

Formal Language Algorithms

Algorithms for formal language written with Python 3 with nltk

Requirements

Run pip3 install -r requirements.txt

Usage

Run python3 grammar_algos.py -f [path of the file containing the grammar] and follow instructions on the terminal

Algorithms implemented

Template of grammar

1.

E -> T EP
EP -> '+' T EP | 'ε'
T -> F TP
TP -> '*' F TP | 'ε'
F -> '(' E ')' | 'id'

Output for First algorithm:

The First sets for this grammar are: 
First(E) = {(, id}
First(EP) = {ε, +}
First(T) = {(, id}
First(TP) = {ε, *}
First(F) = {(, id}

Output for Follow algorithm:

The Follow sets for this grammar are: 
Follow(E) = {$, )}
Follow(EP) = {$, )}
Follow(T) = {$, ), +}
Follow(TP) = {$, ), +}
Follow(F) = {$, ), *, +}

Output for LL parser (need to be revised):

The LL table for this grammar is: 
LL(E, '$') = {}   LL(E, '+') = {}   LL(E, '*') = {}   LL(E, '(') = {E -> T EP}   LL(E, ')') = {}   LL(E, 'id') = {E -> T EP}   
LL(EP, '$') = {EP -> 'ε'}   LL(EP, '+') = {EP -> '+' T EP}   LL(EP, '*') = {}   LL(EP, '(') = {}   LL(EP, ')') = {EP -> 'ε'}   LL(EP, 'id') = {}   
LL(T, '$') = {}   LL(T, '+') = {}   LL(T, '*') = {}   LL(T, '(') = {T -> F TP}   LL(T, ')') = {}   LL(T, 'id') = {T -> F TP}   
LL(TP, '$') = {TP -> 'ε'}   LL(TP, '+') = {TP -> 'ε'}   LL(TP, '*') = {TP -> '*' F TP}   LL(TP, '(') = {}   LL(TP, ')') = {TP -> 'ε'}   LL(TP, 'id') = {}   
LL(F, '$') = {}   LL(F, '+') = {}   LL(F, '*') = {}   LL(F, '(') = {F -> '(' E ')'}   LL(F, ')') = {}   LL(F, 'id') = {F -> 'id'} 

2.

S -> NP VP
PP -> P NP
NP -> Det N | NP PP
VP -> V NP | VP PP
Det -> 'the'
N -> 'kids' | 'box' | 'floor'
V -> 'opened'
P -> 'on'

Output for CYK algorithm with the word "the kids opened the box on the floor":

CYK algorithm for 'the kids opened the box on the floor' :

[[Det], [NP], 'ø', 'ø', [S], 'ø', 'ø', [S]]
['ø', [N], 'ø', 'ø', 'ø', 'ø', 'ø', 'ø']
['ø', 'ø', [V], 'ø', [VP], 'ø', 'ø', [VP]]
['ø', 'ø', 'ø', [Det], [NP], 'ø', 'ø', [NP]]
['ø', 'ø', 'ø', 'ø', [N], 'ø', 'ø', 'ø']
['ø', 'ø', 'ø', 'ø', 'ø', [P], 'ø', [PP]]
['ø', 'ø', 'ø', 'ø', 'ø', 'ø', [Det], [NP]]
['ø', 'ø', 'ø', 'ø', 'ø', 'ø', 'ø', [N]]
'the kids opened the box on the floor' is in the grammar

formal-language-algorithms's People

Contributors

gaetanserre avatar

Watchers

 avatar  avatar

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.