简体   繁体   English

如何在ANTLR3词法分析器规则中定义多字符排除?

[英]How to define multi-character exclusion in ANTLR3 lexer rule?

I'm trying to create a lexer rule for Antlr3, which would match a triple-quoted strings. 我正在尝试为Antlr3创建一个词法分析器规则,该规则将匹配三引号引起来的字符串。 For example: 例如:

"""this is some text"""

This is how I'm doing it: 这就是我的做法:

TEXT:
  '"""' ('\\"' | ~'"')+ '"""'
  { 
    this.setText(
      this.getText()
        .substring(3, this.getText().length() - 3)
        .replace("\\\"", "\"")
    ); 
  }
  ;

Works good, but every single quote mark has to be escaped in the input text, like this: 效果很好,但是每个单引号都必须在输入文本中转义,如下所示:

"""this is the same text, but with \"escaped quotes\" inside"""

I'm trying to get rid of this mandatory escaping of quotes, and parse anything (!) between triple-quote marks, like this: 我试图摆脱强制转义的引号,并解析三引号之间的任何内容(!),如下所示:

"""sample text again, with "quotes" inside"""

I'm trying to change the rule to: 我正在尝试将规则更改为:

TEXT:
  '"""' (~'"""')+ '"""'

And Antlr3 3.5 complains: 而Antlr3 3.5抱怨:

error(100): Spec.g:153:13: syntax error: buildnfa: NoViableAltException(58@[])
error(100): Spec.g:0:1: syntax error: buildnfa: MismatchedTreeNodeException(3!=29)
error(100): Spec.g:0:: syntax error: buildnfa: NoViableAltException(3@[])
error(100): Spec.g:0:1: syntax error: buildnfa: MismatchedTreeNodeException(29!=28)
error(10):  internal error: Spec.g : java.lang.NullPointerException
org.antlr.tool.NFAFactory.build_Aplus(NFAFactory.java:516)
...

What is wrong? 怎么了? What is a possible workaround? 有什么可能的解决方法?

The best way is probably with a predicate. 最好的方法可能是使用谓词。

TEXT
  : '"""'
    ( ~'"'
    | {input.LA(2) != '"' || input.LA(3) != '"'}? '"'
    )*
    '"""'
  ;

This works in ANTLR 4 as well as long as you change input to _input in the predicate. 只要在谓词_input input更改为_input ,此方法就可以在ANTLR 4中使用。

Since .* and .+ are ungreedy by default, have tried simply doing: 由于.*.+默认情况下是不同意的,因此尝试简单地这样做:

TEXT
 : '"""' .* '"""'
   { 
     ... 
   }
 ;

?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM