【发布时间】:2012-11-04 00:13:16
【问题描述】:
我们正在尝试(以诡计)生成一个解析器和一个从字符串而不是标准输入读取字符的词法分析器。
我们开始修改代码中包含的计算器示例 http://code.google.com/p/lalr-scm/source/browse/trunk/calc.scm?r=52
问题似乎出在以下行:
(let* ((location (make-source-location "*stdin*"
(port-line (current-input-port))
(port-column (current-input-port)) -1 -1))
我们尝试定义一个新的输入端口:
(let* ((location (make-source-location "*stdin*"
(port-line (open-input-string program))
(port-column (open-input-string program)) -1 -1))
而变量program是这样定义的:
(define program
"int x = 2;
int y = 0;
y= x*(2+3);"
)
但它不起作用,它仍然在等待标准输入字符。
文档缺乏细节,所以我们无法弄清楚如何解决这个问题。
谢谢
【问题讨论】:
-
Chris Jester-Young 关于 Scheme 中的大多数 I/O 如何使用默认的 current-input-port 的注释是正确的。您需要将字符串端口显式传递给每个 I/O 函数。请参阅
read-char的文档,例如:gnu.org/software/guile/manual/guile.html#Reading。请注意,port参数在括号中:这是文档对“可选参数”的表示法。