""" vtm2yvtm.py Convert *.vtm to *.yvtm -txxx => tape: "xxx" -bx => blank: x -sxxx => start: xxx accept: [] reject: [] #xxx => #xxxx Most lines in the files are rules. Preface all of them with machine: , then convert state, symbol, new_state, new_symbol, R | L to - [ state, symbol, new_state, new_symbol, right | left ] usage: $ python vtm2yvtm.py < filename.vtm > filename.yvtm Tested with python 3.7. Jim Mahoney | cs.marlboro.college | Oct 2019 | MIT License """ from sys import stdin, stdout import re stdout.write('# \n') stdout.write('# -- vtm2vtym -- \n') stdout.write('# \n') stdout.write('\n') has_blank = has_tape = has_start = has_machine = False for line in stdin: line = line.strip() match_blank = re.match(r'.*-b(.)', line) if match_blank: blank = match_blank.group(1) stdout.write('blank: "{}"\n'.format(blank)) has_blank = True match_tape = re.match(r'.*-t([^ ]+)', line) if match_tape: tape = match_tape.group(1) stdout.write('tape: "{}"\n'.format(tape)) has_tape = True match_start = re.match(r'.*-s([^ ]+)', line) if match_start: start = match_start.group(1) stdout.write('start: "{}"\n'.format(start)) has_start = True if line and not line[0] == '#': if not has_machine: stdout.write('\n') stdout.write('machine: \n') stdout.write('\n') has_machine = True line = line.replace('L', 'left') line = line.replace('R', 'right') line = ' - [ ' + line + ']' stdout.write(line + '\n') stdout.write('\n') if not has_blank: stdout.write('blank: "_"\n') if not has_start: stdout.write('start: ""\n') if not has_tape: stdout.write('tape: ""\n') stdout.write('accept: []\n') stdout.write('reject: []\n') stdout.write('\n')