具有多个规则或令牌实例的操作

时间:2013-10-12 02:11:14

标签: java antlr antlr3

我有以下语法规则:

pacman
  : section section map section
  ;

我想为此规则编写一个Java操作,以不同方式处理三个不同的section。这三个不同实例的句柄是什么?


这是伪造的代码,显示我想要的内容:

pacman
  : section section map section
  {
    processFirstSection($section[0]);
    if(checkSecondSection($section[1]))
    {
      //The if statement isn't important itself;
      //the point is that I do completely different
      //things with each section
      handleThirdSection($section[2]);
    }
  }
  ;

1 个答案:

答案 0 :(得分:0)

pacman
  : section1=section section2=section map section3=section
  {
    processFirstSection($section1);
    if(checkSecondSection($section2))
    {
      handleThirdSection($section3);
    }
  }

一般来说,规则

 rule : name=Token otherName=rule

允许您使用Token访问令牌$name,使用rule访问规则$otherName