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

时间:2015-03-09 15:40:57

标签: python file compiler-construction 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 seris的代码(第1 - 2集) episode 2

1 个答案:

答案 0 :(得分:0)

现在感谢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()