我们正在尝试(巧妙地)生成一个解析器和词法分析器,从字符串而不是标准输入中读取字符。
我们开始修改代码中包含的计算器示例: 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);"
)
但是不起作用,它仍然等待标准输入字符。
文档缺乏详细信息,因此我们无法弄清楚如何解决此问题。
谢谢
最佳答案
您已经非常非常接近解决方案了!嗯,有点像。但这是一个开始。查看原始代码,了解您修改的地方:
(let* ((location (make-source-location "*stdin*" (port-line (current-input-port)) (port-column (current-input-port)) -1 -1))
(c (read-char)))
...)
在这里,您将所有 (current-input-port)
更改为字符串端口(顺便说一句,不要多次调用 open-input-string
,因为您每次都会创建一个新的字符串端口,每个端口都有独立的光标),但它并不是唯一实际使用 (current-input-port)
的地方。
你看到了吗?它实际上是在 (read-char)
调用中!该函数实际上采用端口参数,默认为 (current-input-port)
。
事实上,如果您查看上面并搜索 (read-char)
和 (peek-char)
的实例,您会注意到使用 (current-input-port)
几乎融入了整个 make-lexer
函数。所以你需要改变它。
我建议您为 make-lexer
函数指定一个输入端口:
(define* (make-lexer errorp #:optional (input (current-input-port)))
...
然后更改 (read-char)
和 (peek-char)
的所有实例以使用输入端口。另外,也不要忘记更改您的 make-source-location
调用:
(let* ((location (make-source-location "*stdin*" (port-line input) (port-column input) -1 -1))
(c (read-char input)))
...)
现在,您可以使用 (make-lexer errorp (open-input-string program))
并且它应该可以工作。 (我没有测试过。)
关于parsing - 方案 - LALR 解析器生成 - 来自字符串的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13396782/