如何让词法分析器从第二行读取 Python 代码

How to make my Python code read from the second line for my lexer

我正忙于构建一个小型编译器来读取文件、查找关键字然后执行关键字指定的操作。我有一个问题,它每次都从头开始读取文件,但如果没有嵌套的 if 语句,我找不到解决这个问题的方法。

swift.py:

from sys import *
import re

tokens = ["PRINT"]

def open_file(filename):
    with open (filename, "r") as filecontents:
        data = filecontents.read().replace('\n', ' ')
return data

def lex(filecontents):
    words = filecontents.split(" ")
    filecontents = list(filecontents)
    word = []
    states = 0
    statesRun = False
    statesBool = True
    string = ""
    stringAmount = 0
    toks = ""
    i = 0.0
    for i in range(len(words)):
        if words[int(i)].upper() == tokens[0].upper():
            word.append("PRINT")
            for char in filecontents:
                toks += char
                if char == "\"":
                    if states == 0: 
                        statesRun = True
                        if char == "\"" and statesBool == False:
                            states = 1
                            string += char
                            statesRun = False
                        statesBool = False
                    elif states == 1:
                        statesRun = False
                if statesRun:
                    string += char
            stringAmount += 1
            word.append("STRING:" + string)
            string = ""
            statesBool = True
            statesRun = False
            states = 0
    return word

def parse(toks):
    i = 0
    while(i < len(toks)):
        if toks[i].upper() + " " + toks[i+1][0:6].upper() == "PRINT STRING":
            print(toks[i+1][7:])
            i+=2

class core():
    data = open_file(argv[1])
    toks = lex(data)
    parse(toks)

core()

test.swift:

print "Hello"
print "jobs"

在cmd中输入:

python swift.py test.swift

我研究过编程语言、编译器、解释器、解析器、词法分析器和语法。我基于这个 youtube 系列的代码(第 1 - 2 集) episode 2

感谢 Markku K,现在可以使用了!

from sys import *
import re

lines = []
tokens = ["PRINT"]

def open_file(filename):
    data = open(filename, "r")
    for line in data.readlines():
        lines.append(line.replace('\n', ''))
    with open (filename, "r") as filecontents:
        data = filecontents.read().replace('\n', ' ')
    return data

def lex(filecontents):
    words = filecontents.split(" ")
    filecontents = list(filecontents)
    word = []
    states = 0
    statesRun = False
    statesBool = True
    string = ""
    stringAmount = 0
    toks = ""
    i = 0.0
    z = 0
    for i in range(len(words)):
        if words[int(i)].upper() == tokens[0].upper():
            word.append("PRINT")
            for char in lines[z]:
                toks += char
                if char == "\"":
                    if states == 0: 
                        statesRun = True
                        if char == "\"" and statesBool == False:
                            states = 1
                            string += char
                            statesRun = False
                        statesBool = False
                    elif states == 1:
                        statesRun = False
                if statesRun:
                    string += char
            stringAmount += 1
            word.append("STRING:" + string)
            string = ""
            statesBool = True
            statesRun = False
            states = 0
            z += 1
    return word

def parse(toks):
    i = 0
    while(i < len(toks)):
        if toks[i].upper() + " " + toks[i+1][0:6].upper() == "PRINT STRING":
            print(toks[i+1][7:])
            i+=2

def run():
    data = open_file(argv[1])
    toks = lex(data)
    parse(toks)

run()