# -*- coding: utf-8 -*-
"""7_grammer_Finite_Automaton_regular_expression_Deductive_Chart

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1Stlrj0FG7sYmRRju4ug1GcWQZ0_LTCv9

#a. Define grammar using nltk. Analyze a sentence using the same.
"""

#for IDLE


import nltk

# Download required data
nltk.download('punkt')
nltk.download('punkt_tab')

from nltk import tokenize

# Corrected grammar
grammar1 = nltk.CFG.fromstring("""
S -> VP
VP -> V NP
NP -> Det N | N
Det -> 'that'
N -> 'flight'
V -> 'Book'
""")

sentence = "Book that flight"

# Tokenize
tokens = tokenize.word_tokenize(sentence)
print("Tokens:", tokens)

# Parser
parser = nltk.ChartParser(grammar1)

# Parse and draw tree
for tree in parser.parse(tokens):
    print(tree)
    tree.draw()   

import nltk
nltk.download('punkt_tab')
from nltk import tokenize
grammar1 = nltk.CFG.fromstring("""
S -> VP
VP -> VP NP
NP -> Det
Det -> 'that'
NP -> singular Noun
NP -> 'flight'
VP -> 'Book'
""")
sentence = "Book that flight"

# Tokenize the sentence once
all_tokens = tokenize.word_tokenize(sentence)
print(f"Tokenized sentence: {all_tokens}")

# Create a parser and parse the sentence
parser = nltk.ChartParser(grammar1)
print("\nParsing results:")
for tree in parser.parse(all_tokens):
    print(tree)

parser = nltk.ChartParser(grammar1)
for tree in parser.parse(all_tokens):
    print(tree)
    # tree.draw()  # Removed this line as it causes a TclError in this environment

"""#b. Accept the input string with Regular expression of Finite Automaton: 101+.

"""

def FA(s):
    if len(s)<3:
        return "Rejected"
    if s[0]=='1':
        if s[1]=='0':
            if s[2]=='1':
                for i in range(3,len(s)):
                    if s[i]!='1':
                        return "Rejected"
                return "Accepted"
            return "Rejected"
        return "Rejected"
    return "Rejected"

inputs=['1','10101','101','10111','01010','100','','10111101','1011111']
for i in inputs:
    print(FA(i))





"""#c. Accept the input string with Regular expression of FA: (a+b)*bba."""





def FA(s):
    size=0
    for i in s:
        if i=='a' or i=='b':
            size+=1
        else:
            return "Rejected"
    if size>=3:
        if s[size-3]=='b':
            if s[size-2]=='b':
                if s[size-1]=='a':
                    return "Accepted"
                return "Rejected"
            return "Rejected"
        return "Rejected"
    return "Rejected"
inputs=['bba', 'ababbba', 'abba','abb', 'baba','bbb','']
for i in inputs:
    print(FA(i))




"""#Implementation of Deductive Chart Parsing using context free
grammar and a given sentence.
"""

#for idle
import nltk

# Download required data
nltk.download('punkt')
nltk.download('punkt_tab')   # ⭐ FIX

from nltk import tokenize

grammar1 = nltk.CFG.fromstring("""
S -> NP VP
PP -> P NP
NP -> Det N | Det N PP | 'I'
VP -> V NP | VP PP
Det -> 'a' | 'my'
N -> 'bird' | 'balcony'
V -> 'saw'
P -> 'in'
""")

sentence = "I saw a bird in my balcony"

tokens = tokenize.word_tokenize(sentence)

parser = nltk.ChartParser(grammar1)

for tree in parser.parse(tokens):
    print(tree)
    tree.draw()   # ✅ Diagram will open

import nltk
nltk.download('punkt_tab')
from nltk import tokenize
grammar1 = nltk.CFG.fromstring("""
S -> NP VP
PP -> P NP
NP -> Det N | Det N PP | 'I'
VP -> V NP | VP PP
Det -> 'a' | 'my'
N -> 'bird' | 'balcony'
V -> 'saw'
P -> 'in'
""")
sentence = "I saw a bird in my balcony"

# Tokenize the sentence once
all_tokens = tokenize.word_tokenize(sentence)
print(f"Tokenized sentence: {all_tokens}")

# Create a parser and parse the sentence
parser = nltk.ChartParser(grammar1)
print("\nParsing results:")
for tree in parser.parse(all_tokens):
    print(tree)
    # tree.draw() # Commented out to avoid TclError in environments without a display

