我必须将词法分析器的Sebesda编程语言概念(第4章,第2节)中的代码转换为python。这是我到目前为止的内容:
# Character classes #
LETTER = 0
DIGIT = 1
UNKNOWN = 99
# Token Codes #
INT_LIT = 10
IDENT = 11
ASSIGN_OP = 20
ADD_OP= 21
SUB_OP = 22
MULT_OP = 23
DIV_OP = 24
LEFT_PAREN = 25
RIGHT_PAREN = 26
charClass = ''
lexeme = ''
lexLen = 0
token = ''
nextToken = ''
### lookup - function to lookup operators and parentheses ###
### and return the token ###
def lookup(ch):
def left_paren():
addChar()
globals()['nextToken'] = LEFT_PAREN
def right_paren():
addChar()
globals()['nextToken'] = RIGHT_PAREN
def add():
addChar()
globals()['nextToken'] = ADD_OP
def subtract():
addChar()
globals()['nextToken'] = SUB_OP
def multiply():
addChar()
globals()['nextToken'] = MULT_OP
def divide():
addChar()
globals()['nextToken'] = DIV_OP
options = {')': right_paren, '(': left_paren, '+': add,
'-': subtract, '*': multiply , '/': divide}
if ch in options.keys():
options[ch]()
else:
addChar()
### addchar- a function to add next char to lexeme ###
def addChar():
#lexeme = globals()['lexeme']
if(len(globals()['lexeme']) <=98):
globals()['lexeme'] += nextChar
else:
print("Error. Lexeme is too long")
### getChar- a function to get the next Character of input and determine its character class ###
def getChar():
globals()['nextChar'] = globals()['contents'][0]
if nextChar.isalpha():
globals()['charClass'] = LETTER
elif nextChar.isdigit():
globals()['charClass'] = DIGIT
else:
globals()['charClass'] = UNKNOWN
globals()['contents'] = globals()['contents'][1:]
## getNonBlank() - function to call getChar() until it returns a non whitespace character ##
def getNonBlank():
while nextChar.isspace():
getChar()
## lex- simple lexical analyzer for arithmetic functions ##
def lex():
globals()['lexLen'] = 0
getNonBlank()
def letterfunc():
globals()['lexeme'] = ''
addChar()
getChar()
while(globals()['charClass'] == LETTER or globals()['charClass'] == DIGIT):
addChar()
getChar()
globals()['nextToken'] = IDENT
def digitfunc():
globals()['lexeme'] = ''
addChar()
getChar()
while(globals()['charClass'] == DIGIT):
addChar()
getChar()
globals()['nextToken'] = INT_LIT
def unknownfunc():
globals()['lexeme'] = ''
lookup(nextChar)
getChar()
lexDict = {LETTER: letterfunc, DIGIT: digitfunc, UNKNOWN: unknownfunc}
if charClass in lexDict.keys():
lexDict[charClass]()
print('The next token is: '+ str(globals()['nextToken']) + ' The next lexeme is: ' + globals()['lexeme'])
with open('input.txt') as input:
contents = input.read()
getChar()
lex()
while contents:
lex()
我正在使用字符串sum + 1 / 33
作为样本输入字符串。据我了解,在顶层对getChar()的第一次调用将characterClass设置为LETTER,并设置contents
为um + 1 / 33
。
然后,程序进入while循环并调用lex()
。lex()
依次将单词sum累积到中lexeme
。当里面的while循环letterfunc
遇到第一个空格字符时,它将中断并退出lex()
由于contents
不为空,因此程序将再次在顶级循环通过while循环。这次,getNonBlank()
内部调用lex()
“将空格填满,contents
并且重复与以前相同的过程。
我遇到错误的地方是最后一个词素。有人告诉我,这globals()['contents'][0]
超出了范围getChar()
。我并不期望找到一个困难的错误,但是我尝试手动跟踪它,但似乎无法发现问题。任何帮助将不胜感激。
仅仅是因为在成功读取了最后3
一个输入字符串后,该digitfunc
函数又重复了一次getchar
。但是在那一刻content
已经用光了并且为空,所以contents[0]
通过了缓冲区的末尾,因此出现了错误。
解决方法是,如果在表达式的最后一个字符之后添加换行符或空格,则当前代码不会出现此问题。
这样做的原因是,当最后一个字符为char时,UNKNOWN
您会立即从lex返回并由于content
为空而退出循环,但是如果您正在处理数字或符号,则在getchar
不测试输入结束的情况下循环调用。顺便说一句,如果您输入的字符串以正确的括号结尾,那么您的词法分析器会吃掉它而忘记显示它找到了它。
因此,您至少应该:
在getchar中测试输入的结束:
def getchar():
if len(contents) == 0:
# print "END OF INPUT DETECTED"
globals()['charClass'] = UNKNOWN
globals()['nextChar'] = ''
return
...
如果剩下一个,则显示最后一个令牌:
...
while contents:
lex()
lex()
控制是否存在词素(输入结束时可能会发生奇怪的事情)
...
if charClass in lexDict.keys():
lexDict[charClass]()
if lexeme != '':
print('The next token is: '+ str(globals()['nextToken']) +
' The next lexeme is: >' + globals()['lexeme'] + '<')
但是您对globals的使用是不好的。在函数中使用全局变量的惯用法是在使用之前声明它:
a = 5
def setA(val):
global a
a = val # sets the global variable a
但是Python的全局变量具有代码异味。您能做的最好的事情就是将解析器正确封装在一个类中。对象比全局对象好
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句