我正在尝试编写一个简单的解析器,它从字符串创建一个 sxml 表达式,例如
"This is a [Test]" ===> (item "This is a" (subitem "Test"))
任何想知道给定示例中的方括号的人都可以查看所谓的Leiden 约定。
这是我到目前为止写的代码:
(define my-sequence '("this" "[" "is" "a" "]" "test"))
(define (left-square-bracket? item)
(or (equal? item "[")
(eq? item #\x005b)))
(define (right-square-bracket? item)
(or (equal? item "]")
(eq? item #\x005d)))
(define (parse-sequence sequence)
(cond ((null? sequence) '())
((left-square-bracket? (car sequence))
(let ((subsequence (get-subsequence (cdr sequence))))
(list subsequence)))
(else
(cons (car sequence)
(parse-sequence (cdr sequence))))))
(define (get-subsequence sequence)
(if (right-square-bracket? (car sequence))
'()
(cons (car sequence)
(get-subsequence (cdr sequence)))))
评估(parse-sequence my-sequence)
产量("this" ("is" "a"))
。已创建嵌套表达式,但程序在未计算最后一项的情况下完成"test"
。问题是,我如何从get-subsequence
to返回parse-sequence
?
任何帮助表示赞赏,非常感谢提前!:)