2017-02-10 78 views
2

我正在創建一種編程語言。對於這種語言,我創建了一個將其編譯爲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") 
+1

)'的開頭的文件。另外,爲什麼不將每個're.sub'調用的輸出分配給一個變量,以便在必須寫入之前,可以在同一行上調用每個're.sub'。 – bunji

回答

1

執行一個全部替換在一個循環中,一個接一個地循環。我也建議有一個單獨的數據結構,這會使進一步的擴展更容易的正則表達式及其替代:如果你願意,你需要`求(打開的文件多次迭代

conversions = (
    (r'function (\S+) (\S+) =', r'def \1(\2):'), 
    (r'print(.+)',    r'print(\1)' ), 
    (r'call (\S+) (\S+)',  r'\1(\2)' ), 
) 

for line in input: 
    for (pattern, sub) in conversions: 
     line = re.sub(pattern, sub, line) 
    output.write(line) 
+0

這工作。謝謝。 – SyscalineGaming

+0

無論如何,你是否也知道如何以某種順序執行正則表達式?例如:做這個正則表達式,然後做這個,然後做這個... – SyscalineGaming

+0

它們按照元組中出現的順序應用。 – DyZ