Answered step by step
Verified Expert Solution
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 CFLs The grammar must be in Chomsky Normal Form CNF Review carefully the description of CNF from the textbook, Definition
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:
cfgabstar
'terminals':ab
'variables':SVaVbT
'start':S
'rules':
S:'VaVb',TT
T:TT'VaVb'
Va:a
Vb:b
from cyksolver import
asserthonorcode
langabstar ab'abab','abababab' # positive examples
abba'aba','bab' # negative examples
testcykcfgabstar,langabstar
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 tests correct.
Excercise B
How about strings over a b of the form ai bj where ij
How about strings over a b where the number of as is twice the number of bs
# ai bj where ij
cfgij
'terminals':ab
'variables':SVaVbT
'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 as than bs
cfgba
'terminals':ab
'variables':SVaVbT
'start':S
'rules':
S:'VaVb',TT
T:TT'VaVb'
Va:a
Vb:b
#
# sanity check
#
asserthonorcode
langijtest aaabb'aaaaaabbbb','aaaaaabbbb' # positive examples
ab'bbaaa','ababab' # negative examples
resultsummaryij testcykcfgijlangijtest
langbatest baa'aba','baa','bbaaaa','baabaa','babaaa' #posigive examples
abaabb'babaaaa','baaba' # negative examples
resultsummaryba testcykcfgbalangbatest
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started