Common Lisp的格式是否与之相反? [英] Is there an inverse of Common Lisp's FORMAT?

查看:72
本文介绍了Common Lisp的格式是否与之相反?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个困扰我一段时间的问题. Common Lisp format函数是否可逆(至少在某种程度上),因为格式字符串可用于从format的输出中检索原始参数?我知道映射不是一对一的(例如~@:(~a~)会将输入变为大写且不可逆),因此必然会丢失一些信息.我确切地想到的是用于字符串解析的正则表达式的替代方法.例如,我希望能够编写:

I have a question that's been bothering me for some time. Is the Common Lisp format function reversible (at least to some degree) in that the format string could be used to retrieve original arguments from format's output? I am aware that the mapping is not one-to-one (one example is the ~@:(~a~) which turns input to uppercase and is not reversible), so necessarily some information is lost. What I have in mind exactly is rather an alternative to regular expressions for string parsing. For example, I would like to be able to write:

(destructure-format t "[~{~a~^, ~}]" "[0, 1, 2]")

并获得响应:

=> (0 1 2)

您是否知道有任何类似的尝试或论文在讨论类似的方法?

Are you aware of any such attempts or papers discussing a similar approach?

推荐答案

标准中没有

标准中没有这样的内容. Format表达式携带的信息不足以使它在任何实际意义上都有用.对于几乎不绑定 *print-readably* 的所有内容,是难以读取输出的方式.如果您使用列表格式给出

Nothing in the standard

There's nothing like this in the standard. Format expressions don't carry enough information to make this useful in any real sense. For just about everything that doesn't bind *print-readably*, there are ways in which the output would be hard to read back. In the case that you gave, with a list formatting,

(destructure-format t "[~{~a~^, ~}]" "[0, 1, 2]")

任何解决方案都必须检查format指令.那么它能清楚地观察到什么呢?字符串中的第一个字符必须是#\[,最后一个字符必须是#\],并且在~a生成的字符串单独输出中>的 some 出现.那么会出现什么歧义呢?任何会导致在输出中写入", "的内容.例如

any solution would have to examine the format directive. What could it then unambiguously observe? The first character in the string must be a #\[, and the last must be #\], and that some occurrences of ", " within the string separate output generated by ~a. What ambiguities could arise, then? Anything that would cause a ", " to be written in the output. E.g.,

CL-USER> (format t "[~{~a~^, ~}]" '(|, | 2 3))
[, , 2, 3]
NIL
CL-USER> (format t "[~{~a~^, ~}]" '(|, | | ,|))
[, ,  ,]
NIL
CL-USER> (format t "[~{~a~^, ~}]" '(|, | | ,| |,|))
[, ,  ,, ,]
NIL
CL-USER> (format t "[~{~a~^, ~}]" '(|, | | ,| #\,))
[, ,  ,, ,]
NIL

第三方库

尽管库建议在Stack Overflow上是不合时宜的,但这个问题并非一开始就出现,而是在看到Rörd的答案建议使用对C的scanf的外部函数调用,我迅速在CLiki上搜索scanf并发现 format-setf (然后重新阅读评论,我看到

Third party libraries

Although library recommendations are off-topic on Stack Overflow, this question didn't start as one, but after seeing Rörd's answer that suggested using a foreign function call to C's scanf, I quickly searched for scanf on the CLiki and found format-setf (and rereading the comments, I see that Xach found it first), the description of which reads:

scanf()的Common Lisp等效项.

The Common Lisp equivalent of scanf().

comp.lang.lisp上的一个(相对)常见问题是什么是 等价于scanf()?".通常的回答是没有一个, 因为很难确定应该发生什么."这是公平的 足够.

A (relatively) frequently asked question on comp.lang.lisp is "What's the equivalent to scanf()?". The usual answer is "There isn't one, because it's too hard to work out what should happen". Which is fair enough.

但是,克里斯托夫一年在考试中很无聊,所以他写了 format-setf.lisp 可能会这样做你想要什么.

However, one year Christophe was bored during exams, so he wrote format-setf.lisp, which may do what you want.

应该指出,目前该程序的行为 在某种意义上说,未指定的不仅仅是破坏符号中的符号 "CL"包.最好是看看规格 因其行为而出现,所以当人们 说是越野车.

It should be pointed out that currently the behaviour of this program is unspecified, in more senses than just the clobbering of symbols in the "CL" package. What would be nice would be to see a specification appear for its behaviour, so that I don't have an excuse when people say that it's buggy.

其他替代方案

由于您最终会问可能匹配的可能方式是什么,因此,您本质上是在寻求一个正则表达式以及格式化所能实现的其他功能." >

Other alternatives

Since you'd really end up asking "What are the possible ways that this could match, you'd essentially be asking for a regular expression plus the extra things that format makes possible.

我确切地想到的是常规的替代方法 字符串解析的表达式.

What I have in mind exactly is rather an alternative to regular expressions for string parsing.

如果您正在寻找正则表达式,那么正则表达式非常适合.如果您正在寻找不是正则表达式的解析,那么您可能想编写一个真正的解析器.第一次可能令人​​生畏,但是之后,它变得容易得多,Common Lisp使它相对轻松.甚至还有可用的解析器生成库.另一方面,如果您要查找序列化和反序列化,则Common Lisp读取器和写入器会使s表达式成为一个不错的选择.

If you're looking for regular expressions, then regular expressions are a great fit. If you're looking for parsing that's not regular expressions, then you probably want to write a genuine parser. It can be daunting the first time, but after that, it gets much easier, and Common Lisp makes it relatively painless. There are even parser generation libraries available. If, on the other hand, you're looking for serialization and de-serialization, the Common Lisp reader and writer make s-expressions a nice and easy choice.

这篇关于Common Lisp的格式是否与之相反?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆