gpt4 book ai didi

parsing - 如何用 Rebol PARSE 方言表达分支?

转载 作者:行者123 更新时间:2023-12-04 16:59:37 27 4
gpt4 key购买 nike

我有一个像下面这样的 mysql 架构:

data: {
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(10) DEFAULT '' COMMENT 'the name',
`content` text COMMENT 'something',
}

现在我想从中提取一些信息:文件名,类型和注释(如果有的话)。见下文:
["id" "int" "" "name" "varchar" "the name" "content" "text" "something" ]

我的代码是:
parse data [
any [
thru {`} copy field to {`} {`}
thru some space copy field-type to [ {(} | space]
(comm: "")
opt [ thru {COMMENT} thru some space thru {'} copy comm to {'}]
(repend temp field repend temp field-type either comm [ repend temp comm ][ repend temp ""])
]
]

但我得到这样的东西:
["id" "int" "the name" "content" "text" "something"]

我知道线路 opt ..是不正确的。

我要 express 如果找到 COMMENT先关键字,再提取评论信息;如果首先找到lf,则继续下一个循环。 但是我不知道怎么表达。任何人都可以帮忙吗?

最佳答案

我非常喜欢(在可能的情况下)用肯定的术语来构建一组语法规则来匹配目标输入——我发现它更有文字、更精确、更灵活并且更容易调试。在上面的代码段中,我们可以确定五个核心组件:

space: use [space][
space: charset "^-^/ "
[some space]
]

word: use [letter][
letter: charset [#"a" - #"z" #"A" - #"Z" "_"]
[some letter]
]

id: use [letter][
letter: complement charset "`"
[some letter]
]

number: use [digit][
digit: charset "0123456789"
[some digit]
]

string: use [char][
char: complement charset "'"
[any [some char | "''"]]
]

定义了术语后,编写一个描述输入语法的规则就相对简单了:
result: collect [
parsed?: parse/all data [ ; parse/all for Rebol 2 compatibility
opt space
some [
(field: type: none comment: copy "")
"`" copy field id "`"
space
copy type word opt ["(" number ")"]
any [
space [
"COMMENT" space "'" copy comment string "'"
| word | "'" string "'" | number
]
]
opt space "," (keep reduce [field type comment])
opt space
]
]
]

作为额外的奖励,我们可以验证输入。
if parsed? [new-line/all/skip result true 3]
new-line 的一个小应用来让事情变得更聪明一点应该会产生:
== [
"id" "int" ""
"name" "varchar" "the name"
"content" "text" "something"
]

关于parsing - 如何用 Rebol PARSE 方言表达分支?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30423040/

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