Ruby 使用正则表达式的树顶语法问题

Ruby 使用正则表达式的树顶语法问题,ruby,parsing,regex-negation,treetop,Ruby,Parsing,Regex Negation,Treetop,我有一个简单的语法设置,如下所示: grammar Test rule line (adjective / not_adjective)* { def content elements.map{|e| e.content } end } end rule adjective ("good" / "bad" / "excellent") { def content

我有一个简单的语法设置,如下所示:

grammar Test rule line (adjective / not_adjective)* { def content elements.map{|e| e.content } end } end rule adjective ("good" / "bad" / "excellent") { def content [:adjective, text_value] end } end rule not_adjective !adjective { def content [:not_adjective, text_value] end } end end 语法测试 规则线 (形容词/非形容词)*{ def含量 elements.map{| e | e.content} 结束 } 结束 规则形容词 (“好”/“坏”/“优秀”){ def含量 [:形容词,文本值] 结束 } 结束 规则非形容词 !形容词{ def含量 [:非形容词,文本值] 结束 } 结束 结束 假设我的输入是“这是一个好球,让我们使用它”。这就产生了一个错误,我现在不提这个错误,因为我想先了解一下为什么它是错误的理论。 那个么,我如何创建rule not_形容词,以便它匹配任何不能被rule形容词匹配的东西呢?一般来说,如何编写“不”匹配另一个命名规则的I规则(特别是在Treetop中)?

Treetop是一个从称为
的操作解释!expression
是指如果
expression
失败,它将成功;如果
expression
成功,它将失败,但它不消耗任何输入。
要匹配规则
表达式
不匹配的任何内容,请将点运算符(匹配任何内容)与否定运算符结合使用,以避免某些“单词”:


前面的答案对于OP的问题是不正确的,因为它将匹配任何单个字符序列到任何形容词。因此,如果您看到字符串xyzgood,它将匹配xyz,下面的规则将匹配作为形容词的“good”部分。同样,OP的形容词规则将匹配“badge”的前三个字符作为形容词“bad”,这不是他们想要的

相反,形容词规则应该如下所示:

rule adjective
  a:("good" / "bad" / "excellent") ![a-z] {
    def content
      [:adjective, a.text_value]
    end
  }
end
rule not_adjective
  !adjective w:([a-z]+) {
    def content
      [:not_adjective, w.text_value]
    end
  }
end
非形容词规则如下:

rule adjective
  a:("good" / "bad" / "excellent") ![a-z] {
    def content
      [:adjective, a.text_value]
    end
  }
end
rule not_adjective
  !adjective w:([a-z]+) {
    def content
      [:not_adjective, w.text_value]
    end
  }
end
必要时包括处理大写字母、连字符、撇号等。当然,您还需要处理空白