我正在创建一种编程语言。对于这种语言,我创建了一个将其编译为Python的程序。我不需要词法分析器,因为大多数语法都可以用正则表达式转换为Python。Python中的多个正则表达式
这是我到目前为止有:
import re
infile = input()
output = open(infile + ".py","w")
input = open(infile + ".hlx")
# I'm aware the .hlx extension is already taken, but it doesn't really matter.
for line in input:
output.write(re.sub(r'function (\S+) (\S+) =', r'def \1(\2):', line))
for line in input:
output.write(re.sub(r'print(.+)', r'print(\1)', line))
for line in input:
output.write(re.sub(r'call (\S+) (\S+)', r'\1(\2)', line))
# More regexes go here, eventually.
input.close()
output.close()
我不得不把每个正则表达式在一个单独的语句,因为如果我把它们放在一起,它会替换每行3次。
这里的问题是它只执行其中一个正则表达式,这是第一个正则表达式。顺序在这里并不重要,但我仍然需要该程序来执行所有的正则表达式。我将如何做到这一点?
顺便说一句,这是我想在我的语言来代替代码:
function hello input =
print "Hello, ", input, "!"
hello "world"
这里就是我想用Python来取代它的代码:
def hello(input):
print("Hello, " + input + "!")
hello("world")
)'的开头的文件。另外,为什么不将每个're.sub'调用的输出分配给一个变量,以便在必须写入之前,可以在同一行上调用每个're.sub'。 – bunji