Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Writing in Chomsky Normal Form ( CNF ) You will be asked to write grammars for CFL ' s . The grammar must be in

Writing in Chomsky Normal Form (CNF)
You will be asked to write grammars for CFL's. The grammar must be in Chomsky Normal Form (CNF). Review carefully the description of CNF from the textbook, Definition 2.8.
Writing in CNF is tricky, so first write your grammar without worrying about CNF, and then convert it to CNF. The book has a proceedure for doing this, but it might be easier to do it taking ideas from the conversion procedure, but not following it strictly.
The dictionary for the grammar has a set for terminals, a set for variables, a set for rules and a slot for the start vaiable. Terminals must be single characters, lower case letters or digits. The Variables must start with an uppercase letter, and can be more than one character, but the following characters must be lowercase letters or a digits. The rules diction as variables as keys and a list of strings as values. Note that there are no spaces in the rules.
Note that all variables must be listed in the variables property or else the object parsing the grammar will assert and error. As a reminder, the variable given as start cannot appear on the right hand side of a rule, and is the only variable allowed to have the empty string on the right hand side of a rule.
The empty string is given as "".
I give an example for the language (ab)*:
cfg_ab_star ={
'terminals':{'a','b'},
'variables':{'S','Va','Vb','T'},
'start':'S',
'rules':{
'S':['','VaVb','TT'],
'T':['TT','VaVb'],
'Va':['a'],
'Vb':['b']
}
}
from cyk_solver import *
assert_honor_code()
lang_ab_star =[['','ab','abab','abababab'], # positive examples
['a','b','ba','aba','bab']] # negative examples
test_cyk(cfg_ab_star,lang_ab_star)
OK: correct accepts ||
OK: correct accepts |ab|
OK: correct accepts |abab|
OK: correct accepts |abababab|
OK: correctly rejects |a|
OK: correctly rejects |b|
OK: correctly rejects |ba|
OK: correctly rejects |aba|
OK: correctly rejects |bab|
PASS: all 9 tests correct.
Excercise B
How about strings over a, b of the form a^i b^j where 2i=3j
How about strings over a, b where the number of a's is twice the number of b's.
# a^i b^j where 2i=3j
cfg_2i3j ={
'terminals':{'a','b'},
'variables':{'S','Va','Vb','T'},
'start':'S',
'rules':{
'S':['','VaVb','TT'],
'T':['TT','VaVb'],
'Va':['a'],
'Vb':['b']
}
}
# strings in a and b where there are twice the number of a's than b's.
cfg_b2a ={
'terminals':{'a','b'},
'variables':{'S','Va','Vb','T'},
'start':'S',
'rules':{
'S':['','VaVb','TT'],
'T':['TT','VaVb'],
'Va':['a'],
'Vb':['b']
}
}
#
# sanity check
#
assert_honor_code()
lang_2i3j_test =[['aaabb','aaaaaabbbb','aaaaaabbbb'], # positive examples
['ab','bbaaa','ababab']] # negative examples
result_summary['2i3j']= test_cyk(cfg_2i3j,lang_2i3j_test)
lang_b2a_test =[['baa','aba','baa','bbaaaa','baabaa','babaaa'], #posigive examples
['a','b','aa','bb','babaaaa','baaba']] # negative examples
result_summary['b2a']= test_cyk(cfg_b2a,lang_b2a_test)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Programming The Perl DBI Database Programming With Perl

Authors: Tim Bunce, Alligator Descartes

1st Edition

1565926994, 978-1565926998

More Books

Students also viewed these Databases questions

Question

What are Decision Trees?

Answered: 1 week ago

Question

What is meant by the Term Glass Ceiling?

Answered: 1 week ago