Files
comp-syntax-gu-mlt/python/minitranslator.py
Aarne Ranta 415932482e example exam
2023-05-24 09:12:35 +02:00

26 lines
702 B
Python

# minimal translator that reads one line from stdio and translates from Eng to Swe with Minilang
# presupposes 'make minilang'
import pgf
# change these three to translate with other grammars and languages
absmodule = "MicroLang"
fromname = absmodule + "Eng"
toname = absmodule + "Swe"
def main():
# read in the grammar, set up to and from languages
grammar = pgf.readPGF(absmodule + ".pgf")
fromlang = grammar.languages[fromname]
tolang = grammar.languages[toname]
# read a line of input, parse in "from" language, linearize in "to" language
line = input("")
parseresult = fromlang.parse(line)
prob,tree = parseresult.__next__()
print(tolang.linearize(tree))
main()