compilation_course_compiler/cpq.py

35 lines
812 B
Python
Raw Normal View History

2024-03-26 19:02:54 +00:00
import sly
2024-03-30 11:38:42 +00:00
import sys
2024-03-26 19:02:54 +00:00
# for ease of reading, the compiler is broken down to multiple files
# parser.py for the parser
# lexer.py for the lexer
# and helper.py for helper functions(such as print_err)
from lexer import Lexer
from parser import Parser
from helper import print_err
2024-04-04 06:56:28 +00:00
# print signature
2024-03-30 11:38:42 +00:00
print_err('Aviv Romem')
2024-03-26 19:02:54 +00:00
2024-04-04 06:56:28 +00:00
# invalid usage
2024-03-30 11:38:42 +00:00
if len(sys.argv) != 2 or not sys.argv[1].endswith('.ou'):
print('USAGE: python cpq.py <file-name>.ou')
exit(1)
2024-03-26 19:02:54 +00:00
2024-04-04 06:56:28 +00:00
# load file and parser
2024-03-30 11:38:42 +00:00
file = sys.argv[1]
output = file[:-3] + '.qud'
f = open(file, 'r')
text = f.read()
f.close()
2024-03-26 19:02:54 +00:00
lexer = Lexer()
parser = Parser()
2024-03-30 11:38:42 +00:00
file = sys.argv[1]
2024-04-04 06:56:28 +00:00
# parse and write file if valid
2024-03-26 19:02:54 +00:00
parser.parse(lexer.tokenize(text))
2024-03-30 11:38:42 +00:00
if not parser.had_errors:
f = open(output, 'w')
f.writelines([l + '\n' for l in parser.lines])
f.close()