Antlr 如何从解析器规则上下文中获取混合类型列表?

Antlr 如何从解析器规则上下文中获取混合类型列表?,antlr,antlr4,Antlr,Antlr4,我定义了一个解析器规则,它可以是类型a或类型B的列表: g4: sectionContent:(空格*(section |(LINE_LITERAL NEWLINE))空格*) 生成来源:cpp SectionHeaderContext* sectionHeader(); class SectionContentContext : public antlr4::ParserRuleContext { public: SectionContentContext(antlr4

我定义了一个解析器规则,它可以是类型a或类型B的列表:

g4:
sectionContent:(空格*(section |(LINE_LITERAL NEWLINE))空格*)

生成来源:cpp

  SectionHeaderContext* sectionHeader();

  class  SectionContentContext : public antlr4::ParserRuleContext {
  public:
    SectionContentContext(antlr4::ParserRuleContext *parent, size_t invokingState);
    virtual size_t getRuleIndex() const override;
    std::vector<SectionContext *> section();
    SectionContext* section(size_t i);
    std::vector<antlr4::tree::TerminalNode *> WHITESPACE();
    antlr4::tree::TerminalNode* WHITESPACE(size_t i);
    std::vector<antlr4::tree::TerminalNode *> LINE_LITERAL();
    antlr4::tree::TerminalNode* LINE_LITERAL(size_t i);
    std::vector<antlr4::tree::TerminalNode *> NEWLINE();
    antlr4::tree::TerminalNode* NEWLINE(size_t i);

    virtual antlrcpp::Any accept(antlr4::tree::ParseTreeVisitor *visitor) override;

  };

SectionContext\u 1
LINE\u LITERAL\u 2
之间的相对顺序信息非常重要。但是使用antlr,我只能检索所有
LINE\u LITERAL
的列表和另一个
SectionContext
的列表。在派生上下文中看到的所有函数都是方便的方法。它们都在子项列表上工作,子项列表保持标记被识别的状态(按照识别顺序)。例如,
section()
方法迭代子列表并收集所有SectionContext元素。而带有参数的section方法迭代子对象并返回SectionContext的第i次出现


如果要按照所识别的顺序获取子上下文,请使用子列表(C++中的代码> ParStRe::Cuths<代码>)。在谷歌小组讨论中也看到了你的评论。

LINE_LITERAL_1
SectionContext_1
LINE_LITERAL_2
SectionContext_2
LINE_LITERAL_3
SectionContext_3