If you want to translate keywords, then (while you definitely need the right parser, since otherwise avoiding any line changes, comments & c becomes a nightmare), the task is quite simple. For example, since you mentioned Python:
import cStringIO import keyword import token import tokenize samp = '''\ for x in range(8): if x%2: y = x while y>0: print y, y -= 3 print ''' translate = {'for': 'per', 'if': 'se', 'while': 'mentre', 'print': 'stampa'} def toks(tokens): for tt, ts, src, erc, ll in tokens: if tt == token.NAME and keyword.iskeyword(ts): ts = translate.get(ts, ts) yield tt, ts def main(): rl = cStringIO.StringIO(samp).readline toki = toks(tokenize.generate_tokens(rl)) print tokenize.untokenize(toki) main()
I hope it’s obvious how to generalize this to “translate” any Python source and into any language (I supply only a very partial Italian translation of keywords). This emits:
per x in range (8 ): se x %2 : y =x mentre y >0 : stampa y , y -=3 stampa
(strange, albeit correct spaces, but this can be quite easily fixed). As an Italian speaker, I can say that it is terrible to read, but it corresponds to the course on any "translation of a programming language" at your request. Worse, NON keywords such as range are left without translation (according to your specifications) - of course, you do not have to limit your translation to keywords only (easily remove the if that does it above ;-).
Alex martelli
source share