我想区分多个令牌。
看看我的代码。
TOKEN :
{
< LOOPS :
< BEAT >
| < BASS >
| < MELODY >
>
| < #BEAT : "beat" >
| < #BASS : "bass" >
| < #MELODY : "melody" >
}
void findType():
{Token loops;}
{
loops = < LOOPS >
{ String type = loops.image; }我想使用findType ()函数来查找类型。
当输入被“节拍”时,我如何获取返回正确的输出?
发布于 2017-04-10 17:20:53
您想要做的是添加一个return语句,如下所示:
String findType():
{Token loops;}
{
loops = < LOOPS >
{
String type = loops.image;
return type;
}
}请记住,您已经将方法中的返回值定义从void更改为String。
然后,从你的main:
ExampleGrammar parser = new ExampleGrammar(System.in);
while (true)
{
System.out.println("Reading from standard input...");
System.out.print("Enter loop:");
try
{
String type = ExampleGrammar.findType();
System.out.println("Type is: " + type);
}
catch (Exception e)
{
System.out.println("NOK.");
System.out.println(e.getMessage());
ExampleGrammar.ReInit(System.in);
}
catch (Error e)
{
System.out.println("Oops.");
System.out.println(e.getMessage());
break;
}
}它会生成如下输出:
Reading from standard input...
Enter loop:bass
Type is: bass
Reading from standard input...
Enter loop:beat
Type is: beathttps://stackoverflow.com/questions/43274401
复制相似问题