SlideShare a Scribd company logo
1 of 7
Download to read offline
write the code for
Part 1 Context-Free Grammars
Create context-free grammars for these languages. The grammar must be specified as a text
file in the format below.
1. (Filename: cfg1.txt) Create a context-free grammar for the language that accepts all odd
length strings over the alphabet T = {a, b} that begin with a and end with b.
2. (Filename: cfg2.txt) Create a context-free grammar for the alphabet T = {x, y, z} that is
equivalent to the regular expression: ^(xy?)*(zz|xyz)+$
Grammar File Format
The grammar file format must be specified using the notation in class. Some rules:
Nonterminals can either be a single capital letter or a longer string (with no spaces). If you
use longer strings, you must separate the elements on the right-hand side of the
production with spaces.
The terminal alphabet T is provided with each problem. Anything that is not a terminal is
assumed to be a nonterminal.
The assignment requires the grammar to be a context-free grammar. This means that the
left-hand side of each production only consists of a single nonterminal.
You may use the shorthand notation using |.
The start symbol must either be S or Start.
You should use E or Empty to represent the empty string.
Example using single letter nonterminals with T = {a, b}:
S -> aS
S -> X
X -> bX
X -> E
Example using longer string nonterminals and shorthand notation with T = { (, ), +, *, 0, 1}:
Start -> BoolExpr
BoolExpr -> BoolExpr BoolOp BoolExpr | ( BoolExpr ) | BoolVal
BoolOp -> + | *
BoolVal -> 0 | 1
Unfortunately, there is no Python simulator for testing your grammars like the DFAs in last
assignments and the Turing machines in Part 2. Your grammars will be graded manually.
using
# Turing machine simulator
# Original Author: Matt Chu (mwc2110@columbia.edu)
import sys
from optparse import OptionParser
COMMENT = "#"
BLANK = "B"
LEFT = "L"
RIGHT = "R"
# Parses command line
def parse_command_line():
parser = OptionParser()
parser.add_option("-d", "--display_mode", dest = "display_mode",
help = "Display mode")
parser.add_option("-t", "--tm_file", dest = "machine_file",
help = "Turing machine file (.tur)")
parser.add_option("-s", "--string", dest = "tape_string",
help = "Initial string")
options, args = parser.parse_args()
if options.machine_file == None:
options.machine_file = input("Enter the name of the machine file: ")
if options.tape_string == None:
options.tape_string = input("Enter the starting string: ")
if options.display_mode == None:
valid = False
while not valid:
print("Select display mode:")
print("1. Step through the program (stops after each step)")
print("2. Display each step (does not stop until the machine halts)")
print("3. Display the final state (does not stop until the machine halts)")
choice = int(input("Your choice: "))
if choice == 1:
options.display_mode = "step"
valid = True
elif choice == 2:
options.display_mode = "show"
valid = True
elif choice == 3:
options.display_mode = "final"
valid = True
else:
print("Invalid choice")
if not options.display_mode in ("step", "show", "final"):
print("Invalid display mode")
sys.exit(-1)
return options.machine_file, options.tape_string, options.display_mode
# Loads machine from file
def load_machine(machine_file):
machine = {}
# Traverse the file
file = open(machine_file, 'r')
lines = file.readlines()
instruction = lines[0].strip().replace(' ','').split(',')
start_state = instruction[0]
accept_state = instruction[1]
for line in lines[1:]:
# Ignore blank lines and comments
if line.strip() == "" or line.strip()[0] == COMMENT:
continue
# Split the line into its pieces
instruction = line.strip().replace(' ','').split(',')
current_state = instruction[0]
current_symbol = instruction[1]
next_state = instruction[2]
write_symbol = instruction[3]
direction = instruction[4].upper()
# Add the (key, value) to the dictionary
key = (current_state, current_symbol)
value = (write_symbol, next_state, direction)
if current_state == accept_state:
print("ERROR! Transitions are not allowed out of accept state")
sys.exit(-1)
if not key in machine:
machine[key] = value
else:
print("ERROR! Duplicate key detected:", key)
sys.exit(-1)
return machine, start_state, accept_state
# Converts the tape into a list
def load_tape(tape_string):
tape = []
for i in range(len(tape_string)):
tape.append(tape_string[i])
if (len(tape) == 0):
tape.append("B")
return tape
# Print the state of the machine
def print_state(tape, machine_position, machine_state, step):
line1 = "" # Stores the tape
line2 = "" # Stores the head pointer
line3 = "" # Stores the current state
for i in range(len(tape)):
line1 += tape[i]
if i == machine_position:
line2 += "^"
line3 += machine_state
else:
line2 += " "
line3 += " "
print(line1 + "n" + line2 + "n" + line3)
if step:
input("")
else:
print ("")
# Execute the machine
def execute(machine, start_state, tape, display_mode):
# Initialize the machine
current_state = start_state
current_position = 0
current_symbol = tape[current_position]
# Print state
if display_mode == "step":
print_state(tape, current_position, current_state, True)
elif display_mode == "show":
print_state(tape, current_position, current_state, False)
# Continue until machine halts (key does not exist)
while (current_state, current_symbol) in machine:
# Look up next move
write_symbol, next_state, direction = machine[(current_state, current_symbol)]
# Write new symbol
tape[current_position] = write_symbol
# Change state
current_state = next_state
# Move
if direction == LEFT:
current_position -= 1
elif direction == RIGHT:
current_position += 1
else:
print("ERROR! Invalid direction:", direction)
sys.exit(-1)
# Check to see if we are at the end of the tape
if current_position == -1:
tape.insert(0, BLANK)
current_position = 0
elif current_position == len(tape):
tape.append(BLANK)
# Update current symbol
current_symbol = tape[current_position]
# Print state
if display_mode == "step":
print_state(tape, current_position, current_state, True)
elif display_mode == "show":
print_state(tape, current_position, current_state, False)
return current_state, tape
# Converts the tape back to a string
def tape_to_string(tape):
# Convert to string
tape_str = ""
for c in tape:
tape_str += c
# Strip off leading and trailing blanks
tape_str = tape_str.lstrip(BLANK)
tape_str = tape_str.rstrip(BLANK)
# If string only contained blanks, return a single blank
if len(tape_str) == 0:
return BLANK
return tape_str
if __name__ == "__main__":
# Parse command line.
machine_file, tape_string, display_mode = parse_command_line()
# Load the program.
machine, start_state, accept_state = load_machine(machine_file)
# Load the initial tape.
tape = load_tape(tape_string)
# Execute the machine on the tape.
final_state, final_tape = execute(machine, start_state, tape, display_mode)
# Print the final tape.
if final_state == accept_state:
acceptStr = "(accepting)"
else:
acceptStr = "(rejecting)"
print("State:", final_state, acceptStr, "Tape:", tape_to_string(final_tape))
Part 2 Turing Machines
Create Turing machines based on the following specifications. The Turing machine must be
expressed as
files that can be used as inputs for the Turing machine simulator described below.
1. (Filename: tm1.txt) Design a Turing machine on the input alphabet {a,b,c} that accepts strings in
the format: "^([ab]*)c$" and results in a final tape string "^c1$". In other words, the
characters before c in the original input string appear on the right side of c at the end of the
program.
You should move the group of characters, not the letter c, on the tape. It rejects strings that are
not in
the format specified above. You can assume that the input string has at most one c.
Ex. input = babc, resulting tape = cbab
input = aabbc, resulting tape = caabb
input = aab rejected
2. (Filename: tm2.txt) Design a Turing machine on the input alphabet {x, y, z} that accepts strings
where the number of xs is greater than the number of ys in the string. The input string will consist
of x
and y only (i.e., no zs).
For this machine, the final tape output does not matter. You will need to modify the tape in order to
complete this exercise.
Turing Machine Simulator
To run the simulator, the file tm.py needs to be in the current directory:
python3 tm.py
When running the simulator, you will be initially prompted three times:
The name of the Turing machine file (file format is described below).
The initial string.
A choice of three display modes: 1 (step, interactively), 2 (step, non-interactive), 3 (display
final state and output only).
In the Turing machine, states are represented by nonnegative integers. The tape alphabet
consists of any desired characters. The character 'B' represents a blank character.
In the file, the first line contains two integers separated by a comma:
initial state, accepting state
Each subsequent line of the file refers to a different transition represented by a comma-
separated list:
current state, current input, next state, new symbol, direction
For instance, the line 0,x,1,y,R means that if the Turing machine is currently in state 0 with
the head reading an x, it will transition to state 1, write a y into the current tape cell, and
then move the head right one cell.
Notes:
The five fields within a line are separated by commas. Spaces in the line are removed and
ignored.
The symbol B represents a blank character.
The direction can either be L for left and R for right.
Only one accepting state (noted on the first line) is permitted. As with Turing machines, you
cannot have transitions out of the accepting state.
The same input pair (current state, current input) cannot appear multiple times in a file.
The simulator halts when there is no transition for the current input pair. Just like real Turing
machines, it is possible for the simulator to go into an infinite loop.
After the required first line, lines that start with # are ignored and can be used for comments.
If you do something wrong (such as type in the filename incorrectly), you will likely get an
uncaught Python exception rather than a clear error message.
When running the simulator using interactive mode (choice 1), you will see the initial state of
the machine. It will look something like this:
xyyzzz
^
0
The first line is the tape. The initial blanks on both sides of the input are not displayed until
necessary. The subsequent lines show where the head is pointing to and the current state.
Press Enter to advance the Turing machine one step. Continue to press Enter until the
simulator halts. You can also press Ctrl-C to stop execution, necessary in infinite loops.
Command line parameters are also available to specify the Turing machine file (-t), display
mode (-d), and tape string (-s). For display mode, there are three options: step (option 1),
show (option 2), and final (option 3).

More Related Content

Similar to write the code for Part 1 ContextFree Grammars Create con.pdf

INPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docxINPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docxjaggernaoma
 
Theory of automata and formal language lab manual
Theory of automata and formal language lab manualTheory of automata and formal language lab manual
Theory of automata and formal language lab manualNitesh Dubey
 
Python-02| Input, Output & Import
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & ImportMohd Sajjad
 
More instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docxMore instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docxgilpinleeanna
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1Shashwat Shriparv
 
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...Yashpatel821746
 
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...Yashpatel821746
 
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...Yashpatel821746
 
Basic Concepts in Python
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in PythonSumit Satam
 
Input and output basic of c++ programming and escape sequences
Input and output basic of c++ programming and escape sequencesInput and output basic of c++ programming and escape sequences
Input and output basic of c++ programming and escape sequencesssuserf86fba
 
The Input Statement in Core Python .pptx
The Input Statement in Core Python .pptxThe Input Statement in Core Python .pptx
The Input Statement in Core Python .pptxKavitha713564
 
C programming session 01
C programming session 01C programming session 01
C programming session 01Dushmanta Nath
 
Write a C program that uses a recursive function to print a triangle .pdf
Write a C program that uses a recursive function to print a triangle .pdfWrite a C program that uses a recursive function to print a triangle .pdf
Write a C program that uses a recursive function to print a triangle .pdfarsmobiles
 
String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,8neutron8
 
Matlab: Procedures And Functions
Matlab: Procedures And FunctionsMatlab: Procedures And Functions
Matlab: Procedures And Functionsmatlab Content
 

Similar to write the code for Part 1 ContextFree Grammars Create con.pdf (20)

Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docxINPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
INPUT AND OUTPUT PROCESSINGPlease note that the material o.docx
 
Theory of automata and formal language lab manual
Theory of automata and formal language lab manualTheory of automata and formal language lab manual
Theory of automata and formal language lab manual
 
Python-02| Input, Output & Import
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & Import
 
More instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docxMore instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docx
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
 
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
 
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
 
Basic Concepts in Python
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in Python
 
Input and output basic of c++ programming and escape sequences
Input and output basic of c++ programming and escape sequencesInput and output basic of c++ programming and escape sequences
Input and output basic of c++ programming and escape sequences
 
Ascii us
Ascii usAscii us
Ascii us
 
CS 5th.pptx
CS 5th.pptxCS 5th.pptx
CS 5th.pptx
 
The Input Statement in Core Python .pptx
The Input Statement in Core Python .pptxThe Input Statement in Core Python .pptx
The Input Statement in Core Python .pptx
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
ECE-PYTHON.docx
ECE-PYTHON.docxECE-PYTHON.docx
ECE-PYTHON.docx
 
Write a C program that uses a recursive function to print a triangle .pdf
Write a C program that uses a recursive function to print a triangle .pdfWrite a C program that uses a recursive function to print a triangle .pdf
Write a C program that uses a recursive function to print a triangle .pdf
 
String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,
 
1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx
 
Matlab: Procedures And Functions
Matlab: Procedures And FunctionsMatlab: Procedures And Functions
Matlab: Procedures And Functions
 

More from aaryanentp

ZZZZ Best durumunda denetilerin toplad kant trlerini ve d.pdf
ZZZZ Best durumunda denetilerin toplad kant trlerini ve d.pdfZZZZ Best durumunda denetilerin toplad kant trlerini ve d.pdf
ZZZZ Best durumunda denetilerin toplad kant trlerini ve d.pdfaaryanentp
 
Zahra a supervisor at Artworx Inc observes two of her emp.pdf
Zahra a supervisor at Artworx Inc observes two of her emp.pdfZahra a supervisor at Artworx Inc observes two of her emp.pdf
Zahra a supervisor at Artworx Inc observes two of her emp.pdfaaryanentp
 
Zachary Fox does not make any voluntary deductions that impa.pdf
Zachary Fox does not make any voluntary deductions that impa.pdfZachary Fox does not make any voluntary deductions that impa.pdf
Zachary Fox does not make any voluntary deductions that impa.pdfaaryanentp
 
Youve surely seen a sevensegment display Its a device s.pdf
Youve surely seen a sevensegment display Its a device s.pdfYouve surely seen a sevensegment display Its a device s.pdf
Youve surely seen a sevensegment display Its a device s.pdfaaryanentp
 
Your selected organizations strategy is to be included Beg.pdf
Your selected organizations strategy is to be included Beg.pdfYour selected organizations strategy is to be included Beg.pdf
Your selected organizations strategy is to be included Beg.pdfaaryanentp
 
Your company wishes to expand internationally and needs to f.pdf
Your company wishes to expand internationally and needs to f.pdfYour company wishes to expand internationally and needs to f.pdf
Your company wishes to expand internationally and needs to f.pdfaaryanentp
 
You will choose a company familiar to you or that youre int.pdf
You will choose a company familiar to you or that youre int.pdfYou will choose a company familiar to you or that youre int.pdf
You will choose a company familiar to you or that youre int.pdfaaryanentp
 
You want to save 2000 today for retirement in 40 years You.pdf
You want to save 2000 today for retirement in 40 years You.pdfYou want to save 2000 today for retirement in 40 years You.pdf
You want to save 2000 today for retirement in 40 years You.pdfaaryanentp
 
You have recently joined the accounting firm Comtois Benoit.pdf
You have recently joined the accounting firm Comtois Benoit.pdfYou have recently joined the accounting firm Comtois Benoit.pdf
You have recently joined the accounting firm Comtois Benoit.pdfaaryanentp
 
You have three alternatives to invest A1 Stocks A2 Bond.pdf
You have three alternatives to invest A1 Stocks A2  Bond.pdfYou have three alternatives to invest A1 Stocks A2  Bond.pdf
You have three alternatives to invest A1 Stocks A2 Bond.pdfaaryanentp
 
You have helped authors develop a number of figures and tabl.pdf
You have helped authors develop a number of figures and tabl.pdfYou have helped authors develop a number of figures and tabl.pdf
You have helped authors develop a number of figures and tabl.pdfaaryanentp
 
You know that most people use someones birthday as the 4di.pdf
You know that most people use someones birthday as the 4di.pdfYou know that most people use someones birthday as the 4di.pdf
You know that most people use someones birthday as the 4di.pdfaaryanentp
 
You have come onto the unit after report You are required t.pdf
You have come onto the unit after report You are required t.pdfYou have come onto the unit after report You are required t.pdf
You have come onto the unit after report You are required t.pdfaaryanentp
 
you have a portfolio consisting solely of Stock A and Stock .pdf
you have a portfolio consisting solely of Stock A and Stock .pdfyou have a portfolio consisting solely of Stock A and Stock .pdf
you have a portfolio consisting solely of Stock A and Stock .pdfaaryanentp
 
You are designing an application that allows users to input .pdf
You are designing an application that allows users to input .pdfYou are designing an application that allows users to input .pdf
You are designing an application that allows users to input .pdfaaryanentp
 
You are required to present a research report in which you h.pdf
You are required to present a research report in which you h.pdfYou are required to present a research report in which you h.pdf
You are required to present a research report in which you h.pdfaaryanentp
 
You are at a professional association reception and you are.pdf
You are at a professional association reception and you are.pdfYou are at a professional association reception and you are.pdf
You are at a professional association reception and you are.pdfaaryanentp
 
You are a senior manager in an industry of your choosing Se.pdf
You are a senior manager in an industry of your choosing Se.pdfYou are a senior manager in an industry of your choosing Se.pdf
You are a senior manager in an industry of your choosing Se.pdfaaryanentp
 
yi bir etki lm stratejisi SElerin alar etrafndaki potan.pdf
yi bir etki lm stratejisi SElerin alar etrafndaki potan.pdfyi bir etki lm stratejisi SElerin alar etrafndaki potan.pdf
yi bir etki lm stratejisi SElerin alar etrafndaki potan.pdfaaryanentp
 
write the precis of the following passage and give suitable .pdf
write the precis of the following passage and give suitable .pdfwrite the precis of the following passage and give suitable .pdf
write the precis of the following passage and give suitable .pdfaaryanentp
 

More from aaryanentp (20)

ZZZZ Best durumunda denetilerin toplad kant trlerini ve d.pdf
ZZZZ Best durumunda denetilerin toplad kant trlerini ve d.pdfZZZZ Best durumunda denetilerin toplad kant trlerini ve d.pdf
ZZZZ Best durumunda denetilerin toplad kant trlerini ve d.pdf
 
Zahra a supervisor at Artworx Inc observes two of her emp.pdf
Zahra a supervisor at Artworx Inc observes two of her emp.pdfZahra a supervisor at Artworx Inc observes two of her emp.pdf
Zahra a supervisor at Artworx Inc observes two of her emp.pdf
 
Zachary Fox does not make any voluntary deductions that impa.pdf
Zachary Fox does not make any voluntary deductions that impa.pdfZachary Fox does not make any voluntary deductions that impa.pdf
Zachary Fox does not make any voluntary deductions that impa.pdf
 
Youve surely seen a sevensegment display Its a device s.pdf
Youve surely seen a sevensegment display Its a device s.pdfYouve surely seen a sevensegment display Its a device s.pdf
Youve surely seen a sevensegment display Its a device s.pdf
 
Your selected organizations strategy is to be included Beg.pdf
Your selected organizations strategy is to be included Beg.pdfYour selected organizations strategy is to be included Beg.pdf
Your selected organizations strategy is to be included Beg.pdf
 
Your company wishes to expand internationally and needs to f.pdf
Your company wishes to expand internationally and needs to f.pdfYour company wishes to expand internationally and needs to f.pdf
Your company wishes to expand internationally and needs to f.pdf
 
You will choose a company familiar to you or that youre int.pdf
You will choose a company familiar to you or that youre int.pdfYou will choose a company familiar to you or that youre int.pdf
You will choose a company familiar to you or that youre int.pdf
 
You want to save 2000 today for retirement in 40 years You.pdf
You want to save 2000 today for retirement in 40 years You.pdfYou want to save 2000 today for retirement in 40 years You.pdf
You want to save 2000 today for retirement in 40 years You.pdf
 
You have recently joined the accounting firm Comtois Benoit.pdf
You have recently joined the accounting firm Comtois Benoit.pdfYou have recently joined the accounting firm Comtois Benoit.pdf
You have recently joined the accounting firm Comtois Benoit.pdf
 
You have three alternatives to invest A1 Stocks A2 Bond.pdf
You have three alternatives to invest A1 Stocks A2  Bond.pdfYou have three alternatives to invest A1 Stocks A2  Bond.pdf
You have three alternatives to invest A1 Stocks A2 Bond.pdf
 
You have helped authors develop a number of figures and tabl.pdf
You have helped authors develop a number of figures and tabl.pdfYou have helped authors develop a number of figures and tabl.pdf
You have helped authors develop a number of figures and tabl.pdf
 
You know that most people use someones birthday as the 4di.pdf
You know that most people use someones birthday as the 4di.pdfYou know that most people use someones birthday as the 4di.pdf
You know that most people use someones birthday as the 4di.pdf
 
You have come onto the unit after report You are required t.pdf
You have come onto the unit after report You are required t.pdfYou have come onto the unit after report You are required t.pdf
You have come onto the unit after report You are required t.pdf
 
you have a portfolio consisting solely of Stock A and Stock .pdf
you have a portfolio consisting solely of Stock A and Stock .pdfyou have a portfolio consisting solely of Stock A and Stock .pdf
you have a portfolio consisting solely of Stock A and Stock .pdf
 
You are designing an application that allows users to input .pdf
You are designing an application that allows users to input .pdfYou are designing an application that allows users to input .pdf
You are designing an application that allows users to input .pdf
 
You are required to present a research report in which you h.pdf
You are required to present a research report in which you h.pdfYou are required to present a research report in which you h.pdf
You are required to present a research report in which you h.pdf
 
You are at a professional association reception and you are.pdf
You are at a professional association reception and you are.pdfYou are at a professional association reception and you are.pdf
You are at a professional association reception and you are.pdf
 
You are a senior manager in an industry of your choosing Se.pdf
You are a senior manager in an industry of your choosing Se.pdfYou are a senior manager in an industry of your choosing Se.pdf
You are a senior manager in an industry of your choosing Se.pdf
 
yi bir etki lm stratejisi SElerin alar etrafndaki potan.pdf
yi bir etki lm stratejisi SElerin alar etrafndaki potan.pdfyi bir etki lm stratejisi SElerin alar etrafndaki potan.pdf
yi bir etki lm stratejisi SElerin alar etrafndaki potan.pdf
 
write the precis of the following passage and give suitable .pdf
write the precis of the following passage and give suitable .pdfwrite the precis of the following passage and give suitable .pdf
write the precis of the following passage and give suitable .pdf
 

Recently uploaded

Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSean M. Fox
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17Celine George
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMELOISARIVERA8
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi RajagopalEADTU
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhleson0603
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....Ritu480198
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesAmanpreetKaur157993
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...Gary Wood
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptxPoojaSen20
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaEADTU
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...EduSkills OECD
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital ManagementMBA Assignment Experts
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptxPoojaSen20
 

Recently uploaded (20)

Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 

write the code for Part 1 ContextFree Grammars Create con.pdf

  • 1. write the code for Part 1 Context-Free Grammars Create context-free grammars for these languages. The grammar must be specified as a text file in the format below. 1. (Filename: cfg1.txt) Create a context-free grammar for the language that accepts all odd length strings over the alphabet T = {a, b} that begin with a and end with b. 2. (Filename: cfg2.txt) Create a context-free grammar for the alphabet T = {x, y, z} that is equivalent to the regular expression: ^(xy?)*(zz|xyz)+$ Grammar File Format The grammar file format must be specified using the notation in class. Some rules: Nonterminals can either be a single capital letter or a longer string (with no spaces). If you use longer strings, you must separate the elements on the right-hand side of the production with spaces. The terminal alphabet T is provided with each problem. Anything that is not a terminal is assumed to be a nonterminal. The assignment requires the grammar to be a context-free grammar. This means that the left-hand side of each production only consists of a single nonterminal. You may use the shorthand notation using |. The start symbol must either be S or Start. You should use E or Empty to represent the empty string. Example using single letter nonterminals with T = {a, b}: S -> aS S -> X X -> bX X -> E Example using longer string nonterminals and shorthand notation with T = { (, ), +, *, 0, 1}: Start -> BoolExpr BoolExpr -> BoolExpr BoolOp BoolExpr | ( BoolExpr ) | BoolVal BoolOp -> + | * BoolVal -> 0 | 1 Unfortunately, there is no Python simulator for testing your grammars like the DFAs in last assignments and the Turing machines in Part 2. Your grammars will be graded manually. using # Turing machine simulator # Original Author: Matt Chu (mwc2110@columbia.edu) import sys from optparse import OptionParser COMMENT = "#" BLANK = "B" LEFT = "L" RIGHT = "R" # Parses command line
  • 2. def parse_command_line(): parser = OptionParser() parser.add_option("-d", "--display_mode", dest = "display_mode", help = "Display mode") parser.add_option("-t", "--tm_file", dest = "machine_file", help = "Turing machine file (.tur)") parser.add_option("-s", "--string", dest = "tape_string", help = "Initial string") options, args = parser.parse_args() if options.machine_file == None: options.machine_file = input("Enter the name of the machine file: ") if options.tape_string == None: options.tape_string = input("Enter the starting string: ") if options.display_mode == None: valid = False while not valid: print("Select display mode:") print("1. Step through the program (stops after each step)") print("2. Display each step (does not stop until the machine halts)") print("3. Display the final state (does not stop until the machine halts)") choice = int(input("Your choice: ")) if choice == 1: options.display_mode = "step" valid = True elif choice == 2: options.display_mode = "show" valid = True elif choice == 3: options.display_mode = "final" valid = True else: print("Invalid choice") if not options.display_mode in ("step", "show", "final"): print("Invalid display mode") sys.exit(-1) return options.machine_file, options.tape_string, options.display_mode # Loads machine from file def load_machine(machine_file): machine = {} # Traverse the file file = open(machine_file, 'r') lines = file.readlines()
  • 3. instruction = lines[0].strip().replace(' ','').split(',') start_state = instruction[0] accept_state = instruction[1] for line in lines[1:]: # Ignore blank lines and comments if line.strip() == "" or line.strip()[0] == COMMENT: continue # Split the line into its pieces instruction = line.strip().replace(' ','').split(',') current_state = instruction[0] current_symbol = instruction[1] next_state = instruction[2] write_symbol = instruction[3] direction = instruction[4].upper() # Add the (key, value) to the dictionary key = (current_state, current_symbol) value = (write_symbol, next_state, direction) if current_state == accept_state: print("ERROR! Transitions are not allowed out of accept state") sys.exit(-1) if not key in machine: machine[key] = value else: print("ERROR! Duplicate key detected:", key) sys.exit(-1) return machine, start_state, accept_state # Converts the tape into a list def load_tape(tape_string): tape = [] for i in range(len(tape_string)): tape.append(tape_string[i]) if (len(tape) == 0): tape.append("B") return tape # Print the state of the machine def print_state(tape, machine_position, machine_state, step): line1 = "" # Stores the tape line2 = "" # Stores the head pointer line3 = "" # Stores the current state for i in range(len(tape)): line1 += tape[i]
  • 4. if i == machine_position: line2 += "^" line3 += machine_state else: line2 += " " line3 += " " print(line1 + "n" + line2 + "n" + line3) if step: input("") else: print ("") # Execute the machine def execute(machine, start_state, tape, display_mode): # Initialize the machine current_state = start_state current_position = 0 current_symbol = tape[current_position] # Print state if display_mode == "step": print_state(tape, current_position, current_state, True) elif display_mode == "show": print_state(tape, current_position, current_state, False) # Continue until machine halts (key does not exist) while (current_state, current_symbol) in machine: # Look up next move write_symbol, next_state, direction = machine[(current_state, current_symbol)] # Write new symbol tape[current_position] = write_symbol # Change state current_state = next_state # Move if direction == LEFT: current_position -= 1 elif direction == RIGHT: current_position += 1 else: print("ERROR! Invalid direction:", direction) sys.exit(-1) # Check to see if we are at the end of the tape if current_position == -1: tape.insert(0, BLANK) current_position = 0
  • 5. elif current_position == len(tape): tape.append(BLANK) # Update current symbol current_symbol = tape[current_position] # Print state if display_mode == "step": print_state(tape, current_position, current_state, True) elif display_mode == "show": print_state(tape, current_position, current_state, False) return current_state, tape # Converts the tape back to a string def tape_to_string(tape): # Convert to string tape_str = "" for c in tape: tape_str += c # Strip off leading and trailing blanks tape_str = tape_str.lstrip(BLANK) tape_str = tape_str.rstrip(BLANK) # If string only contained blanks, return a single blank if len(tape_str) == 0: return BLANK return tape_str if __name__ == "__main__": # Parse command line. machine_file, tape_string, display_mode = parse_command_line() # Load the program. machine, start_state, accept_state = load_machine(machine_file) # Load the initial tape. tape = load_tape(tape_string) # Execute the machine on the tape. final_state, final_tape = execute(machine, start_state, tape, display_mode) # Print the final tape. if final_state == accept_state: acceptStr = "(accepting)" else: acceptStr = "(rejecting)" print("State:", final_state, acceptStr, "Tape:", tape_to_string(final_tape)) Part 2 Turing Machines Create Turing machines based on the following specifications. The Turing machine must be expressed as files that can be used as inputs for the Turing machine simulator described below.
  • 6. 1. (Filename: tm1.txt) Design a Turing machine on the input alphabet {a,b,c} that accepts strings in the format: "^([ab]*)c$" and results in a final tape string "^c1$". In other words, the characters before c in the original input string appear on the right side of c at the end of the program. You should move the group of characters, not the letter c, on the tape. It rejects strings that are not in the format specified above. You can assume that the input string has at most one c. Ex. input = babc, resulting tape = cbab input = aabbc, resulting tape = caabb input = aab rejected 2. (Filename: tm2.txt) Design a Turing machine on the input alphabet {x, y, z} that accepts strings where the number of xs is greater than the number of ys in the string. The input string will consist of x and y only (i.e., no zs). For this machine, the final tape output does not matter. You will need to modify the tape in order to complete this exercise. Turing Machine Simulator To run the simulator, the file tm.py needs to be in the current directory: python3 tm.py When running the simulator, you will be initially prompted three times: The name of the Turing machine file (file format is described below). The initial string. A choice of three display modes: 1 (step, interactively), 2 (step, non-interactive), 3 (display final state and output only). In the Turing machine, states are represented by nonnegative integers. The tape alphabet consists of any desired characters. The character 'B' represents a blank character. In the file, the first line contains two integers separated by a comma: initial state, accepting state Each subsequent line of the file refers to a different transition represented by a comma- separated list: current state, current input, next state, new symbol, direction For instance, the line 0,x,1,y,R means that if the Turing machine is currently in state 0 with the head reading an x, it will transition to state 1, write a y into the current tape cell, and then move the head right one cell. Notes: The five fields within a line are separated by commas. Spaces in the line are removed and ignored. The symbol B represents a blank character. The direction can either be L for left and R for right. Only one accepting state (noted on the first line) is permitted. As with Turing machines, you cannot have transitions out of the accepting state. The same input pair (current state, current input) cannot appear multiple times in a file.
  • 7. The simulator halts when there is no transition for the current input pair. Just like real Turing machines, it is possible for the simulator to go into an infinite loop. After the required first line, lines that start with # are ignored and can be used for comments. If you do something wrong (such as type in the filename incorrectly), you will likely get an uncaught Python exception rather than a clear error message. When running the simulator using interactive mode (choice 1), you will see the initial state of the machine. It will look something like this: xyyzzz ^ 0 The first line is the tape. The initial blanks on both sides of the input are not displayed until necessary. The subsequent lines show where the head is pointing to and the current state. Press Enter to advance the Turing machine one step. Continue to press Enter until the simulator halts. You can also press Ctrl-C to stop execution, necessary in infinite loops. Command line parameters are also available to specify the Turing machine file (-t), display mode (-d), and tape string (-s). For display mode, there are three options: step (option 1), show (option 2), and final (option 3).