首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >javacc标记的使用说明

javacc标记的使用说明
EN

Stack Overflow用户
提问于 2017-04-07 17:21:56
回答 1查看 114关注 0票数 0

我想区分多个令牌。

看看我的代码。

代码语言:javascript
复制
TOKEN :
{
  < LOOPS :
    < BEAT >
  | < BASS >
  | < MELODY > 
  >
| < #BEAT : "beat" >
| < #BASS : "bass" >
| < #MELODY : "melody" >
}

void findType():
{Token loops;}
{
loops = < LOOPS >
{ String type = loops.image; }

我想使用findType ()函数来查找类型。

当输入被“节拍”时,我如何获取返回正确的输出?

EN

回答 1

Stack Overflow用户

发布于 2017-04-10 17:20:53

您想要做的是添加一个return语句,如下所示:

代码语言:javascript
复制
String findType():
{Token loops;}
{
    loops = < LOOPS >
    {
      String type = loops.image;
      return type;
    }
}

请记住,您已经将方法中的返回值定义从void更改为String

然后,从你的main:

代码语言:javascript
复制
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;
      }
    }

它会生成如下输出:

代码语言:javascript
复制
Reading from standard input...
Enter loop:bass
Type is: bass
Reading from standard input...
Enter loop:beat
Type is: beat
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43274401

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档