有没有办法使用Scanner类来读取语法?
我们说我们有这个档案。
是否有任何逻辑方式可以让我创建一个类对象并填充它的属性和操作。 我尝试使用useDelimiter(&#34 ;;"),但返回完整的类而不将它分开,我应该重置吗?什么是对待它的实用方法。
CLASS Team
ATTRIBUTES
name: String
OPERATIONS
nb_players() : Integer,
trainer() : String
;
CLASS AUDIENCE
ATTRIBUTES
name : String
OPERATIONS
;
我已经创建了对象,例如我的类Class有这些属性
public final String Identifier;
public final Class_Content class_content;
class_content包含属性和操作(一种递归的处理方式)
答案 0 :(得分:1)
有没有办法使用Scanner类来读取语法?
你几乎肯定不想读语法。您很可能 1 想要读取符合语法的文件。
通过简单的调用或调用import math
shapeName = input ("Please enter the shape you want to calculate the volume of:").upper()
myList = []
def volumeCube (side):
Volume = 0
Volume = side**3
print ("The Volume of the cube is", Volume)
myList.append("The volume of the cube was "+ str(Volume))
return;
def volumePyramid (baseSideLength,height):
Volume = 0
Volume = round((1/3)*(baseSideLength**2)*height,1)
print ("The volume of the pyramid is", Volume)
myList.append("The volume of the pyramid was "+ str(Volume))
return;
def volumeEllipsoid (radius1,radius2,radius3):
Volume = 0
Volume = round((4/3)*math.pi*radius1*radius2*radius3,1)
print ("The volume of the ellipsoid is", Volume)
myList.append("The volume of the ellipsoid was " + str(Volume))
return;
def printArray ():
for word in myList:
print(word)
while shapeName != "QUIT":
while shapeName in ["CUBE","PYRAMID","ELLIPSOID","QUIT"]:
if shapeName == "CUBE":
side = int(input("Please enter the length of the sides:"))
volumeCube(side)
shapeName = input("Please enter the shape you want to calculate the volume of:").upper()
elif shapeName == "PYRAMID":
baseSideLength = int(input("Please enter the lengths of the side of the base:"))
height = int(input("Please enter the height of the pyramid:"))
volumePyramid(baseSideLength, height)
shapeName = input("Please enter the shape you want to calculate the volume of:").upper()
elif shapeName == "ELLIPSOID":
radius1 = int(input("Please enter the first radius:"))
radius2 = int(input("Please enter the second radius:"))
radius3 = int(input("Please enter the third radius:"))
volumeEllipsoid(radius1, radius2, radius3)
shapeName = input("Please enter the shape you want to calculate the volume of:").upper()
elif shapeName == "QUIT" :
print ("\nYou have come to the end of the session. \nThe volumes calculated for each shape are shown below:")
printArray()
exit()
else:
print ("Error")
shapeName = input("Please enter the shape you want to calculate the volume of:").upper()
else:
print ("\nyou have come to the end of the session. \nYou have not performed any volume calculations.")
,您无法读取符合固有递归语法的文本。你需要构造一个解析器。
您给出的示例看起来不需要递归语法来描述它。但是,我认为编写或生成解析器可能是最好的解决方案。
如果你想写一个,网上有教程/例子:
如果要生成一个,可以学习和使用解析器生成器工具,如JavaCC或AntLR。
1 - 但是如果您确实想要实际读取语法(G),那么问题基本相同。语法必须用某种语言表达,并且该语言具有语法(GG)。您需要使用语法GG的解析器来读取/解析语法G.例如,EBNF表示法具有简单的语法,因此要读取以EBNF表示的语法,您需要EBNF的解析器。