我对程序感到困惑。由于某种原因,该程序不会为我提供输入信息。 因此,我正在尝试通过使用内置的/集成的/集成的/集成的/自行研发的/内置的/内置的/内置的/内置的/内置的/集成的/集成的/集成的/集成的/集成的/集成的/集成的/集成的/集成的/集成的/集成的/集成的/集成的/集成的/集成的/集成的/集成的/集成的/集成的/集成的/集成的/集成的/集成的/集成的/集成的/集成的一个很小的,典型的语言说明就可以了。
代码如下:
def lexan():
global mitr
try:
return(next(mitr))
except StopIteration:
return('')
def match(lexem):
global lookahead
if lexem == lookahead:
lookahead = lexan()
else:
print('Match Syntax Error ')
print('Required - ' + lexem + 'Received - ' + lookahead)
SystemExit()
def prog():
global lookahead
let_in_end()
while lookahead == 'let':
typeDict.clear()
valueDict.clear()
let_in_end()
def decl_list(id):
global lookahead
decl(id)
while lookahead != 'in':
id = lookahead
decl(id)
def let_in_end():
global lookahead
match('let')
decl_list(lookahead)
match('in')
expectedType = typeOf(lookahead)
match('(')
num = expr(expectedType)
match(')')
match('end')
match(';')
print(num)
def decl(id):
global lookahead
if id in invalidIDs:
print('System Error. Invalid ID')
SystemExit()
valueDict[id] = None
typeDict[id] = None
lookahead = lexan()
match(':')
typeDict[id] = typeOf(lookahead)
match('=')
valueDict[id] = expr(typeDict[id])
if type(valueDict[id]) != typeDict[id]:
print('Expression and type Mismatch Declaration')
SystemExit()
match(';')
def id_list():
global lookahead
# ***check that id hasn't been declared yet in dictionary (type and id_list as key and value?)***
# ***if not add to dictionary else Syntax error exit********
# print(lookahead)
if id():
# worked
lookahead = lexan()
else:
print("Syntax Errors 1")
exit(1)
if match(","):
# print("FOUND ,")
id_list()
def expr(): # using variable v, from example in notes
global lookahead
v = term()
tempList = ["+", "-"]
while (lookahead in tempList):
if match("+"):
# print("FOUND +")
v += term()
elif match("-"):
# print("FOUND -")
v -= term()
else:
print("Syntax Errors 3")
exit(1)
return v
def typeOf(type):
global lookahead
if type == 'int':
lookahead = lexan()
return int
elif type == 'real':
lookahead = lexan()
return float
else:
print('Must define type as real or int')
SystemExit()
def expr(exp_type):
global lookahead
if lookahead == 'if':
match('if')
boolResult = cond()
lookahead = lexan()
match('then')
thenExp = expr(exp_type)
match('else')
elseExp = expr(exp_type)
if boolResult:
return thenExp
else:
return elseExp
else:
termValue = term(exp_type)
while lookahead == '+' or lookahead == '-':
if lookahead == '+':
match('+')
termValue = termValue + term(exp_type)
else:
match('-')
termValue = termValue - term(exp_type)
return termValue
def term(exp_type):
global lookahead
tempVal = factor(exp_type)
while lookahead == '*' or lookahead == '/':
if lookahead == '*':
match('*')
tempVal = tempVal * (factor(exp_type))
else:
match('/')
tempVal = tempVal / factor(exp_type)
return tempVal
def factor(exp_type):
global lookahead
if lookahead == '(':
match('(')
tempValue = expr(exp_type)
match(')')
return tempValue
elif lookahead in valueDict.keys():
id = lookahead
lookahead = lexan()
if typeDict[id] == exp_type:
return valueDict[id]
else:
print('Expected Value Mismatch in Factor')
SystemExit()
elif lookahead.find('.') != -1:
tempValue = float(lookahead)
lookahead = lexan()
return tempValue
elif lookahead.isdigit():
tempValue = int(lookahead)
lookahead = lexan()
return tempValue
elif lookahead == 'int' or lookahead == 'real':
typeConvert = typeOf(lookahead)
lookahead = lexan()
if lookahead in valueDict.keys():
if typeConvert == int:
match('(')
tempVar = int(valueDict[lookahead])
lookahead = lexan()
match(')')
return tempVar
elif typeConvert == float:
match('(')
tempVar = float(valueDict[lookahead])
lookahead = lexan()
match(')')
return tempVar
else:
print('Type conversion error')
SystemExit()
else:
print('Error. Couldnt find ID')
SystemExit()
else:
print('Factoring Error')
SystemExit()
import sys
file = open('data.tiny', 'r')
wlist = file.read().split()
mitr = iter(wlist)
lookahead = lexan()
invalidIDs = ["let", "in", "end", "if", "then", "else", "real", "int"]
operators = ['+', '-', '*', '/']
typeDict = {}
valueDict = {}
prog()
The main issue that whenever we pass on the arguments I get multiple errors such as expect -; but got - x. I want to utilize the file name "data.tiny" and containing the information below:
猫测试。tiny:
real m , n = 4.0 , 3.0 ;
let
int x = 5 ;
in
int ( x + x * x )
end ;
let
real m , pi = 10.0 , 3.1416 ;
in
real ( pi * m * m )
end ;
let
real x , y = m + n , m - n ;
in
real ( x * y )
end
预期产量
30
314.6
7.0
再举一个例子:
real y = 3.0 ;
let
int x = 7 ;
in
real ( ( real ( x ) + y ) / ( real ( x ) - y ) )
end;
let
x = 8 ;
in ( x + y )
end ;
预期输出
2.5
错误
但是我遇到一个错误,看来我在逻辑上有些混乱。每当我设置test.tiny时,下面的方法都不会出现问题:
输入
let x : int = 5 ;
in
int ( x + x * x )
end ;
let r : real = 10.0 ;
pi : real = 3.14156 ;
in
real ( pi * r * r )
end ;
let a : int = 3 ;
b : real = 0.5 ;
c : real = b * b ;
in
real ( if a > 5 then b + 1.1 else c )
end ;
输出
30
314.156
0.25
我不确定如果有人可以提供一些有用的帮助,那么可能是潜在的问题。