antlr - 处理antlr 3中的隐藏 channel

标签 antlr hidden channel

我正在编写用于将一种语言翻译成另一种语言的 ANTRL 语法,但是关于使用 HIDDEN channel 的文档非常稀缺。我在任何地方都找不到例子。我唯一找到的是 www.antlr.org 上的常见问题解答,它告诉您如何访问隐藏 channel ,但没有告诉您如何最好地使用此功能。目标语言是 Java。

在我的语法文件中,我像这样传递空格和注释:

// Send runs of space and tab characters to the hidden channel.        
WHITESPACE 
    :   (SPACE | TAB)+ { $channel = HIDDEN; }
    ;

// Single-line comments begin with --
SINGLE_COMMENT 
    :    ('--' COMMENT_CHARS NEWLINE) {
            $channel=HIDDEN;
        }
    ;

fragment COMMENT_CHARS 
    :   ~('\r' | '\n')*
    ;

// Treat runs of newline characters as a single NEWLINE token.
NEWLINE 
    :   ('\r'? '\n')+ { $channel = HIDDEN; }
    ;

在我的成员部分,我定义了一个方法,用于将隐藏的 channel token 写入我的输出 StringStream...

@members {

private int savedIndex = 0;

void ProcessHiddenChannel(TokenStream input) {      
    List<Token> tokens = ((CommonTokenStream)input).getTokens(savedIndex, input.index());
    for(Token token: tokens) {
        if(token.getChannel() == token.HIDDEN_CHANNEL) {
            output.append(token.getText());

        }
    }
    savedIndex = input.index();
}
}

现在要使用它,我必须在语法中的每个标记之后调用该方法。

myParserRule
        :       MYTOKEN1 { ProcessHiddenChannel(input); }
                MYTOKEN2 { ProcessHiddenChannel(input); }
        ;

肯定有更好的方法吗?

编辑:这是输入语言的示例:

-- -----------------------------------------------------------------
--
--
--  Name                Description
--  ==================================
--  IFM1/183         Freq Spectrum Inversion
--                     
-- -----------------------------------------------------------------

PROCEDURE IFM1/183

TITLE "Freq Spectrum Inversion";

HELP
      Freq Spectrum Inversion

ENDHELP;

PRIVILEGE CTRL;

WINDOW MANDATORY;

INPUT

   $Input : @NO_YES
   DEFAULT select %YES when /IFMS1/183.VALUE = %NO;
                  %NO otherwise
           endselect
   PROMPT "Spec Inv";

   $Forced_Cmd : BOOLEAN
   Default FALSE
   Prompt  "Forced Commanding";

DEFINE
   &RetCode   : @PSTATUS := %OK;
   &msg       : STRING;
   &Input     : BOOLEAN;

REQUIRE AVAILABLE(/IFMS1)
        MSG "IFMS1 not available";

REQUIRE /IFMS1/001.VALUE = %MON_AND_CTRL
        MSG "IFMS1 not in control mode";

BEGIN  -- Procedure Body --

    &msg := "IFMS1/183 -> " + toString($Input) + " : "; 

-- pre-check

   IF /IFMS1/183.VALUE = $Input
      AND $Forced_Cmd = FALSE THEN
      EXIT (%OK, MSG &msg + "already set");
   ENDIF;

-- command

   IF $Input =  %YES THEN &Input:= TRUE;
    ELSE  &Input:= FALSE;
   ENDIF;

   SET &RetCode := SEND IFMS1.FREQPLAN
        ( $FreqSpecInv := &Input);
   IF &RetCode <> %OK THEN
      EXIT (&RetCode, MSG &msg + "command failed");
   ENDIF;

-- verify

   SET &RetCode := VERIFY /IFMS1/183.VALUE = $Input TIMEOUT '10';
   IF &RetCode <> %OK THEN
      EXIT (&RetCode, MSG &msg + "verification failed");
   ELSE
      EXIT (&RetCode, MSG &msg + "verified");
   ENDIF;

END

最佳答案

考虑继承 CommonTokenStream 并将您的子类的一个实例提供给 ANTLR。根据您提供的代码示例,我怀疑您可能有兴趣查看版本 3 中提供的过滤器和重写选项。

另外,看看这个其他的 related stack overflow question .

关于antlr - 处理antlr 3中的隐藏 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4203416/

相关文章:

Netty 的 `sync` 与 `syncUninterruptibly`

go - Housie 计划陷入僵局。生产者-消费者模式

go - channel 和 WaitGroup 进入死锁

parsing - ANTLR4:空白处理

compiler-construction - 编译器 : what is the best way to fill the symbol table?

java - context.getText() 排除 ANTLR4 中的空格

iphone - 隐藏 super View 而不隐藏其 subview

compiler-construction - ANTLR 中的 bool 和算术表达式语法

c++ - 从共享库中剥离符号似乎不起作用

html - CSS3 parent 的可见性 : hover transition hides child's visibility:visible transition after a delay