在通用的LISP(SBCL)中,您如何解析文件?
我有一个文件:test.lisp
带有此内容:
(+ 3 3)
(let ((n 3)) (+ n 1))
(let ((n 4))
(+ n 1))
(defun d (f)
(let ((n 3)
(f 4)
(g 3))
(if (= n 0)
(+ n f g)
(* n f g))))
我试图读取这样的文件:
(let ((in (open dd)))
(when in
(loop for line = (read-line in nil)
while line do (format t "~a~%" line))
(close in)))
这给了我明显正确的输出:
(+ 3 3)
(let ((n 3)) (+ n 1))
(let ((n 4))
(+ n 1))
(defun d (f)
(let ((n 3)
(f 4)
(g 3))
(if (= n 0)
(+ n f g)
(* n f g))))
我想调用我的函数parse-parse-exp
parse-exp < /代码>在文件的每一行上,与上面的每个表达式相对应。
从stackoverflow上的另一个答案中,我尝试执行此操作:
* (defun get-file (filename)
(let ((in (open filename)))
(loop for line = (read-line in nil)
while line
collect line)))
GET-FILE
* (get-file dd)
("(+ 3 3)" "" "(let ((n 3)) (+ n 1))" "" "(let ((n 4))" " (+ n 1))" ""
"(defun d (f)" " (let ((n 3)" " (f 4)" " (g 3))" " (if (= n 0)"
" (+ n f g)" " (* n f g))))")
将每行转换为字符串,而不是我想要的格式。
我也尝试这样做:
(uiop:read-file-lines "file.txt")
但是我获得了与上一个示例相同的输出。
我希望能够将每行/表达式用作parse-exp
的输入 - 像这样:
(parse-exp'(+ 3 3))
或 (parse-exp'(let((n 3))(+ n 1)))
等等。
我该怎么做?
谢谢。
I have a file: test.lisp
with this content:
(+ 3 3)
(let ((n 3)) (+ n 1))
(let ((n 4))
(+ n 1))
(defun d (f)
(let ((n 3)
(f 4)
(g 3))
(if (= n 0)
(+ n f g)
(* n f g))))
I am trying to read the file like this:
(let ((in (open dd)))
(when in
(loop for line = (read-line in nil)
while line do (format t "~a~%" line))
(close in)))
which gives me the apparent correct output:
(+ 3 3)
(let ((n 3)) (+ n 1))
(let ((n 4))
(+ n 1))
(defun d (f)
(let ((n 3)
(f 4)
(g 3))
(if (= n 0)
(+ n f g)
(* n f g))))
I want to call my function parse-exp
on each line of the file corresponding to each expression above.
From a different answer here on Stackoverflow I tried to do this:
* (defun get-file (filename)
(let ((in (open filename)))
(loop for line = (read-line in nil)
while line
collect line)))
GET-FILE
* (get-file dd)
("(+ 3 3)" "" "(let ((n 3)) (+ n 1))" "" "(let ((n 4))" " (+ n 1))" ""
"(defun d (f)" " (let ((n 3)" " (f 4)" " (g 3))" " (if (= n 0)"
" (+ n f g)" " (* n f g))))")
which converts each line to a string and its not in the format that I want.
I also tried doing this:
(uiop:read-file-lines "file.txt")
but I get the same output as the previous example.
I want to be able to use each line/expression as input to parse-exp
- like this:
(parse-exp '(+ 3 3))
or(parse-exp '(let ((n 3)) (+ n 1)))
and so on.
How do I do this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您搜索的是
读取
而不是read-line
读取expression-wise(并由LISP解释器使用)。如果您希望表达式为字符串而不是符号列表,请使用
(格式nil“ 〜a” ...)
:What you search for is
read
instead ofread-line
which reads expression-wise (and is used by lisp interpreters).If you want the expressions to be strings instead of list of symbols, use
(format nil "~a" ...)
:Common Lisp具有许多功能可以处理的功能:
读取
从流中读取S-表达。读取从字符串读取
从字符串print
,prin1
和pprint
打印S-表达。后者漂亮打印出来。eval
评估S-表达。with-open-file
是一个宏,它以输入以流以流为“流” 是一个宏来打开并关闭文件Common Lisp has a bunch of functions to deal with that:
read
reads an s-expression from a stream.read-from-string
reads an s-expression from a stringprint
,prin1
andpprint
prints an s-expression. The latter pretty prints it.eval
evaluates an s-expression.with-open-file
is a macro which opens and closes a file as a streamwith-input-from-string
is a macro which creates a stream, so that one can read from that string