使用 ml-lex 构建词法分析器
我需要创建一个与标准输入流绑定的 lexer
的新实例。
但是,当我输入时,
val lexer = makeLexer( fn n => inputLine( stdIn ) );
我收到一个我不明白的错误:(
stdIn:1.5-11.13 Error: operator and operand don't agree [tycon mismatch]
operator domain: int -> string
operand: int -> string option
in expression:
makeLexer
是我的源代码中存在的函数名称)
I need to create a new instance of a lexer
tied to the standard input stream.
However, when I type in
val lexer = makeLexer( fn n => inputLine( stdIn ) );
I get an error that I don't understand:
stdIn:1.5-11.13 Error: operator and operand don't agree [tycon mismatch]
operator domain: int -> string
operand: int -> string option
in expression:
(makeLexer
is a function name present in my source code)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
inputLine 返回一个
字符串选项< /code>,我的猜测是一个
string
是预期的。您想要做的是让
makeLexer
采用string 选项
,如下所示:或将行更改为:
valOf 采用选项类型并将其解包。
请注意,由于当流为空时
inputLine
返回NONE
,因此使用第一种方法可能比第二种方法更好。inputLine returns a
string option
, and my guess is astring
is expected.What you want to do is either have
makeLexer
take astring option
, like so:or change your line to:
valOf takes an option type and unpacks it.
Note that, since
inputLine
returnsNONE
when the stream is empty, it's probably a better idea to use the first approach, rather than the second.用户指南的第 38 页(或论文中的第 32 页)给出了如何制作交互式流的示例ML-Lex 和 ML-Yacc
使用 inputLine 示例代码可以更简单。
因此,我将使用 Sebastian 给出的示例,请记住,如果用户按 CTRL-D,则 inputLine 可能至少使用 stdIn 返回 NONE 。
此外,第 40 页(论文中为 34 页)的计算器示例显示了如何整体使用它。
一般来说,用户指南包含一些很好的示例和解释。
An example of how to make an interactive stream is given on page 38 (or 32 in the paper) of the User's Guide to ML-Lex and ML-Yacc
The example code could be simpler by using inputLine.
So I would use the example given by Sebastian, keeping in mind that inputLine might return NONE using stdIn atleast if the user presses CTRL-D.
Also the calculator example on page 40 (34 in the paper) shows how to use this in a whole
In general the user guide contains some nice examples and explanations.