gpt4 book ai didi

ruby - 我的 Treetop 语法有什么问题?

转载 作者:数据小太阳 更新时间:2023-10-29 08:05:04 25 4
gpt4 key购买 nike

我有语法文件alexa_scrape.tt:

grammar AlexaScrape
rule document
category_listing*
end
rule category_listing
category_line url_line*
end
rule category_line
category "\n"
end
rule category
("/" [^/]+)+
end
rule url_line
[0-9]+ ". " url "\n"
end
rule url
[^\n]*
end
end

我有一个试图使用它的 ruby​​ 文件:

#!/usr/bin/env ruby -I .
require 'rubygems'
require 'polyglot'
require 'treetop'
require 'alexa_scrape.tt'

parser = AlexaScrapeParser.new
p( parser.parse("") || parser.failure_reason )
p( parser.parse("/x\n") || parser.failure_reason )

但我没有得到预期的结果:

SyntaxNode offset=0, ""
"Expected one of /, \n at line 2, column 1 (byte 4) after /x\n"

它正确解析空字符串(作为 document 的简单匹配,零 category_listing),但无法解析 "/x\n"(因为包含单个 category_listing 的文档本身具有零个 url_line)。

我做错了什么?

最佳答案

看起来 category 中的正则表达式正在通过匹配 category_line 所需的空格前进......这样做:

  rule category
("/" [^/\s]+)+ # or perhaps ("/" [^/\n]+)+
end

(而且,哇,Treetop 问题。这是 SO 历史上的第 47 个问题及其 400 万个问题。87,000 个 SO 问题中有一个被标记为 Treetop)

关于ruby - 我的 Treetop 语法有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13650629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com