如何获取 Antlr 解析器规则以从默认通道和隐藏通道读取

发布于 2024-11-02 19:31:23 字数 529 浏览 1 评论 0原文

我在隐藏通道中使用正常的空白分隔,但我有一个规则,我想包含任何空白以供以后处理,但我发现的任何示例都需要一些非常奇怪的手动编码。

是否没有简单的选项可以从多个通道读取内容,例如从头开始将空格放置在那里的选项。

前任。这是 WhiteSpace 词法分析器规则

WS  :   ( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {$channel=HIDDEN;}
    ;

这是我的规则,我想在其中包含空格

raw :   '{'? (~('{'))*;

基本上,这是一个捕获所有规则,用于捕获与要由另一个模式处理的其他规则不匹配的任何内容,因此我需要原始流。

我希望有一个 {$channel==DEFAULT || $channel==HIDDEN} 语法示例,但找不到任何示例。

我的目标是 C#,但如果需要,我可以重写 Java 示例。

I use the normal whitespace separation into the hidden channel but I have one rule where I would like to include any whitespace for later processing but any example I have found requires some very strange manual coding.

Is there no easy option to read from multiple channels like the option to put the whitespace there from the beginning.

Ex. this is the WhiteSpace lexer rule

WS  :   ( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {$channel=HIDDEN;}
    ;

And this is my rule where I would like to include whitespace

raw :   '{'? (~('{'))*;

Basically it's a catch all rule to capture any content that does not match other rules to be processed by another pattern and therefore I need the original stream.

I was hoping for a {$channel==DEFAULT || $channel==HIDDEN} syntax example but cannot find any.

My target will be C# but I can rewrite Java examples if required.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

药祭#氼 2024-11-09 19:31:23

AFAIK,那是不可能的。但是,您可以扩展 UnbufferedTokenStream 以在解析期间更改channel。您不能使用CommonTokenStream,因为它缓冲可变数量的令牌(并且缓冲区中可能存在位于错误通道上的令牌!)。请注意,您至少需要 ANTLR 3.3:在以前的版本中,尚未包含 UnbufferedTokenStream

假设您想要解析(并显示)小写或大写字母。大写字母被放在 HIDDEN 通道上,因此默认情况下,只会解析小写字母。然而,当解析器偶然发现小写的“q”时,我们想要更改为HIDDEN通道。在 HIDDEN 通道上进行解析后,我们希望 "Q" 将我们再次带回 DEFAULT_CHANNEL

因此,在解析源代码 "aAbBcqCdDQeE" 时,首先是 "a""b""c"打印,然后更改通道,然后打印 "C""D",然后再次更改通道,最后 "e" 被打印到控制台。

这是执行此操作的 ANTLR 语法:

ChannelDemo.g

grammar ChannelDemo;

@parser::members {
  private void handle(String letter) {
    if("Q".equals(letter)) {
      ((ChangeableChannelTokenStream)input).setChannel(Token.DEFAULT_CHANNEL);
    }
    else if("q".equals(letter)) {
      ((ChangeableChannelTokenStream)input).setChannel(HIDDEN);
    }
    else {
      System.out.println(letter);
    }
  }
}

parse
  :  any* EOF
  ;

any
  :  letter=(LOWER | UPPER) {handle($letter.getText());}
  ;

LOWER
  :  'a'..'z'
  ;

UPPER
  :  'A'..'Z' {$channel=HIDDEN;}
  ;

这是自定义令牌流类:

ChangeableChannelTokenStream.java

import org.antlr.runtime.*;

public class ChangeableChannelTokenStream extends UnbufferedTokenStream {

    public ChangeableChannelTokenStream(TokenSource source) {
        super(source);
    }

    public Token nextElement() {
        Token t = null;
        while(true) {
            t = super.tokenSource.nextToken();
            t.setTokenIndex(tokenIndex++);
            if(t.getChannel() == super.channel) break;
        }
        return t;
    }

    public void setChannel(int ch) {
        super.channel = ch;
    }
}

和一个用于测试所有内容的小 Main 类:

Main.java

import org.antlr.runtime.*;

public class Main {
    public static void main(String[] args) throws Exception {
        ANTLRStringStream in = new ANTLRStringStream("aAbBcqCdDQeE");
        ChannelDemoLexer lexer = new ChannelDemoLexer(in);
        ChangeableChannelTokenStream tokens = new ChangeableChannelTokenStream(lexer);
        ChannelDemoParser parser = new ChannelDemoParser(tokens);
        parser.parse();
    }
}

最后,生成词法分析器/解析器 (1),编译所有源文件(2) 并运行 Main 类 (3):

1

java -cp antlr-3.3.jar org.antlr.Tool ChannelDemo.g

2

javac -cp antlr-3.3.jar *.java

3 (*nix)

java -cp .:antlr-3.3.jar Main

3 (Windows)

java -cp .;antlr-3.3.jar Main

这将导致以下内容打印到控制台:

a
b
c
C
D
e

编辑

您可以将该类包含在您的像这样的语法文件:

grammar ChannelDemo;

@parser::members {
  private void handle(String letter) {
    if("Q".equals(letter)) {
      ((ChangeableChannelTokenStream)input).setChannel(Token.DEFAULT_CHANNEL);
    }
    else if("q".equals(letter)) {
      ((ChangeableChannelTokenStream)input).setChannel(HIDDEN);
    }
    else {
      System.out.println(letter);
    }
  }

  public static class ChangeableChannelTokenStream extends UnbufferedTokenStream {

    private boolean anyChannel;

    public ChangeableChannelTokenStream(TokenSource source) {
      super(source);
      anyChannel = false;
    }

    @Override
    public Token nextElement() {
      Token t = null;
      while(true) {
        t = super.tokenSource.nextToken();
        t.setTokenIndex(tokenIndex++);
        if(t.getChannel() == super.channel || anyChannel) break;
      }
      return t;
    }

    public void setAnyChannel(boolean enable) {
      anyChannel = enable;
    }

    public void setChannel(int ch) {
      super.channel = ch;
    }
  }
}

parse
  :  any* EOF
  ;

any
  :  letter=(LOWER | UPPER) {handle($letter.getText());}
  |  STAR                   {((ChangeableChannelTokenStream)input).setAnyChannel(true);}
  ;

STAR
  :  '*'
  ;

LOWER
  :  'a'..'z'
  ;

UPPER
  :  'A'..'Z' {$channel=HIDDEN;}
  ;

从上面的语法生成的解析器在遇到 "*" 时将启用从所有通道的读取。因此,在解析 "aAbB*cCdDeE" 时:

import org.antlr.runtime.*;

public class Main {
  public static void main(String[] args) throws Exception {
    ANTLRStringStream in = new ANTLRStringStream("aAbB*cCdDeE");
    ChannelDemoLexer lexer = new ChannelDemoLexer(in);
    ChannelDemoParser.ChangeableChannelTokenStream tokens =
        new ChannelDemoParser.ChangeableChannelTokenStream(lexer);
    ChannelDemoParser parser = new ChannelDemoParser(tokens);
    parser.parse();
  }
}

将打印以下内容:

a
b
c
C
d
D
e
E

AFAIK, that is not possible. However, you could extend the UnbufferedTokenStream to change the channel during parsing. You can't use the CommonTokenStream since it buffers a variable amount of tokens (and there can be tokens in the buffer that are on the wrong channel!). Note that you need at least ANTLR 3.3: in previous versions the UnbufferedTokenStream wasn't included yet.

Let's say you want to parse (and display) either lower- or upper case letters. Upper case letters are put on the HIDDEN channel, so by deafult, only lower case letters will be parsed. However, when the parser stumbles upon a lower case "q", we want to change to the HIDDEN channel. Once parsing on the HIDDEN channel, we want the "Q" to bring us back to the DEFAULT_CHANNEL again.

So when parsing the source "aAbBcqCdDQeE", first "a", "b" and "c" are printed, then the channel is changed, then "C" and "D" get printed, then the channel is changed again, and finally "e" is printed to the console.

Here's an ANTLR grammar that does this:

ChannelDemo.g

grammar ChannelDemo;

@parser::members {
  private void handle(String letter) {
    if("Q".equals(letter)) {
      ((ChangeableChannelTokenStream)input).setChannel(Token.DEFAULT_CHANNEL);
    }
    else if("q".equals(letter)) {
      ((ChangeableChannelTokenStream)input).setChannel(HIDDEN);
    }
    else {
      System.out.println(letter);
    }
  }
}

parse
  :  any* EOF
  ;

any
  :  letter=(LOWER | UPPER) {handle($letter.getText());}
  ;

LOWER
  :  'a'..'z'
  ;

UPPER
  :  'A'..'Z' {$channel=HIDDEN;}
  ;

And here's the custom token stream class:

ChangeableChannelTokenStream.java

import org.antlr.runtime.*;

public class ChangeableChannelTokenStream extends UnbufferedTokenStream {

    public ChangeableChannelTokenStream(TokenSource source) {
        super(source);
    }

    public Token nextElement() {
        Token t = null;
        while(true) {
            t = super.tokenSource.nextToken();
            t.setTokenIndex(tokenIndex++);
            if(t.getChannel() == super.channel) break;
        }
        return t;
    }

    public void setChannel(int ch) {
        super.channel = ch;
    }
}

And a small Main class to test it all:

Main.java

import org.antlr.runtime.*;

public class Main {
    public static void main(String[] args) throws Exception {
        ANTLRStringStream in = new ANTLRStringStream("aAbBcqCdDQeE");
        ChannelDemoLexer lexer = new ChannelDemoLexer(in);
        ChangeableChannelTokenStream tokens = new ChangeableChannelTokenStream(lexer);
        ChannelDemoParser parser = new ChannelDemoParser(tokens);
        parser.parse();
    }
}

Finally, generate a lexer/parser (1), compile all source files (2) and run the Main class (3):

1

java -cp antlr-3.3.jar org.antlr.Tool ChannelDemo.g

2

javac -cp antlr-3.3.jar *.java

3 (*nix)

java -cp .:antlr-3.3.jar Main

3 (Windows)

java -cp .;antlr-3.3.jar Main

which will cause the following to be printed to the console:

a
b
c
C
D
e

EDIT

You can include the class in your grammar file like this:

grammar ChannelDemo;

@parser::members {
  private void handle(String letter) {
    if("Q".equals(letter)) {
      ((ChangeableChannelTokenStream)input).setChannel(Token.DEFAULT_CHANNEL);
    }
    else if("q".equals(letter)) {
      ((ChangeableChannelTokenStream)input).setChannel(HIDDEN);
    }
    else {
      System.out.println(letter);
    }
  }

  public static class ChangeableChannelTokenStream extends UnbufferedTokenStream {

    private boolean anyChannel;

    public ChangeableChannelTokenStream(TokenSource source) {
      super(source);
      anyChannel = false;
    }

    @Override
    public Token nextElement() {
      Token t = null;
      while(true) {
        t = super.tokenSource.nextToken();
        t.setTokenIndex(tokenIndex++);
        if(t.getChannel() == super.channel || anyChannel) break;
      }
      return t;
    }

    public void setAnyChannel(boolean enable) {
      anyChannel = enable;
    }

    public void setChannel(int ch) {
      super.channel = ch;
    }
  }
}

parse
  :  any* EOF
  ;

any
  :  letter=(LOWER | UPPER) {handle($letter.getText());}
  |  STAR                   {((ChangeableChannelTokenStream)input).setAnyChannel(true);}
  ;

STAR
  :  '*'
  ;

LOWER
  :  'a'..'z'
  ;

UPPER
  :  'A'..'Z' {$channel=HIDDEN;}
  ;

The parser that gets generated from the grammar above will enable reading from all channels when it encounters a "*". So when parsing "aAbB*cCdDeE":

import org.antlr.runtime.*;

public class Main {
  public static void main(String[] args) throws Exception {
    ANTLRStringStream in = new ANTLRStringStream("aAbB*cCdDeE");
    ChannelDemoLexer lexer = new ChannelDemoLexer(in);
    ChannelDemoParser.ChangeableChannelTokenStream tokens =
        new ChannelDemoParser.ChangeableChannelTokenStream(lexer);
    ChannelDemoParser parser = new ChannelDemoParser(tokens);
    parser.parse();
  }
}

the following gets printed:

a
b
c
C
d
D
e
E
拥抱我好吗 2024-11-09 19:31:23

也许您应该考虑将空格作为语法的一部分。但为什么要用如此不重要的信息来扰乱你的语法呢?好吧,因为这并不不重要。换行符在某些情况下有意义。当您需要 IDE 支持(例如来自 Visual Studio 语言服务器)时,您需要指定一个语言语法器,而不需要低级 ANTLR 自定义的所有花哨功能。

Maybe you should consider making whitespace a part of your gramar instead. But why clutter up your gramar with such unimportant information? Well, because it is NOT unimportant. The newline has meaining in certain contexts. When you want IDE support, e.g. from visual studio language server, you need to specify a language grammer without all the bells and whistles of low-level ANTLR customization.

甜`诱少女 2024-11-09 19:31:23

Antler 4 中,我使用了一个简单的解决方案。我没有在 Antlr 3 中测试它。它是 C#,但您可以轻松地将其转换为 Java。

  1. parser1.g4 更改如下:

    解析器语法Parser1;
    
    选项 { tokenVocab=Lexer1; }
    
    启动规则
    @init { SetWhiteSpacesAcceptence(假); } 
        : (componentWithWhiteSpaces | componentWithoutWhiteSpaces)* EOF
    ;
    
    组件WithWhiteSpaces : { SetWhiteSpacesAcceptence(true); } 
                                组件 1 组件 2 组件 3 
                                { SetWhiteSpacesAcceptence(假); } 
    ;
    
    没有白色空格的组件:组件 4 组件 5 组件 6 
    
  2. lexer1.g4 更改为下一个:

    词法分析器语法 Lexer1;
    WS : [ \t\r\n] { if( this.IsWhiteSpacesAccepted() ) Skip(); };
    
  3. Parser1 类扩展为下一个:

    类 MyParser :Parser1
    {
        公共无效SetWhiteSpacesAcceptence(布尔isAccept)
        {
            if (_input != null && _input.TokenSource != null)
            {
                if (_input.TokenSource 是 MyLexer)
                {
                    MyLexer 词法分析器 = _input.TokenSource as MyLexer;
                    if (词法分析器!= null)
                        lexer.SetWhiteSpacesAcceptence(isAccept);
                }
            }
        }
    
        公共 bool IsWhiteSpacesAccepted()
        {
            if (_input != null && _input.TokenSource != null)
            {
                if (_input.TokenSource 是 MyLexer)
                {
                    MyLexer 词法分析器 = _input.TokenSource as MyLexer;
                    if (词法分析器!= null)
                        返回 lexer.IsWhiteSpacesAccepted();
                }
            }
    
            返回假;
        }
    }
    
  4. Lexer1 类扩展为下一个:

    MyLexer 类:Lexer1
    {
        private bool isWhiteSpacesAccepted;
    
        公共无效 SetWhiteSpacesAcceptence(bool isAccept) { isWhiteSpacesAccepted = isAccept }
    
        公共 bool IsWhiteSpacesAccepted() { return isWhiteSpacesAccepted; }
    }
    
  5. 现在 Main 函数作为下一个:

    static void Main()
    {
        AntlrFileStream 输入 = new AntlrFileStream("pathToInputFile");
        MyLexer 词法分析器 = new MyLexer(input);
        UnbufferedTokenStream 令牌 = new UnbufferedTokenStream(lexer);
        MyParser 解析器 = new MyParser(tokens);
    
        解析器.startRule();
    }
    

In Antler 4 I'm using a simple solution. I didn't test it in Antlr 3. It's C# but you can translate it to Java easily.

  1. Change parser1.g4 as next:

    parser grammar Parser1;
    
    options { tokenVocab=Lexer1; }
    
    startRule
    @init { SetWhiteSpacesAcceptence(false); } 
        : (componentWithWhiteSpaces | componentWithoutWhiteSpaces)* EOF
    ;
    
    componentWithWhiteSpaces : { SetWhiteSpacesAcceptence(true); } 
                                component1 component2 component3 
                                { SetWhiteSpacesAcceptence(false); } 
    ;
    
    componentWithoutWhiteSpaces : component4 component5 component6 
    
  2. Change lexer1.g4 as next:

    lexer grammar Lexer1;
    WS : [ \t\r\n] { if( this.IsWhiteSpacesAccepted() ) Skip(); };
    
  3. Extend Parser1 class as next:

    class MyParser : Parser1
    {
        public void SetWhiteSpacesAcceptence(bool isAccept)
        {
            if (_input != null && _input.TokenSource != null)
            {
                if (_input.TokenSource is MyLexer)
                {
                    MyLexer lexer = _input.TokenSource as MyLexer;
                    if (lexer != null)
                        lexer.SetWhiteSpacesAcceptence(isAccept);
                }
            }
        }
    
        public bool IsWhiteSpacesAccepted()
        {
            if (_input != null && _input.TokenSource != null)
            {
                if (_input.TokenSource is MyLexer)
                {
                    MyLexer lexer = _input.TokenSource as MyLexer;
                    if (lexer != null)
                        return lexer.IsWhiteSpacesAccepted();
                }
            }
    
            return false;
        }
    }
    
  4. Extend Lexer1 class as next:

    class MyLexer : Lexer1
    {
        private bool isWhiteSpacesAccepted;
    
        public void SetWhiteSpacesAcceptence(bool isAccept) { isWhiteSpacesAccepted = isAccept }
    
        public bool IsWhiteSpacesAccepted() { return isWhiteSpacesAccepted; }
    }
    
  5. Now the Main function as next:

    static void Main()
    {
        AntlrFileStream input = new AntlrFileStream("pathToInputFile");
        MyLexer lexer = new MyLexer(input);
        UnbufferedTokenStream tokens = new UnbufferedTokenStream(lexer);
        MyParser parser = new MyParser(tokens);
    
        parser.startRule();
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文