使用ml-lex构建一个词法分析器 [英] building a lexical analyser using ml-lex
问题描述
我需要创建与标准输入流绑定的lexer
的新实例.
但是,当我输入
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
是我的源代码中存在的函数名称)
(makeLexer
is a function name present in my source code)
推荐答案
inputLine 返回一个string option
,我猜是一个string
.
inputLine returns a string option
, and my guess is a string
is expected.
您想要做的就是让makeLexer
取一个string option
,就像这样:
What you want to do is either have makeLexer
take a string option
, like so:
fun makeLexer NONE = <whatever you want to do when stream is empty>
| makeLexer (SOME s) = <the normal body makeLexer, working on the string s>
或将您的行更改为:
val lexer = makeLexer( fn n => valOf ( inputLine( stdIn ) ) );
valOf 提取选项类型并将其解压缩
valOf takes an option type and unpacks it.
请注意,由于当流为空时inputLine
返回NONE
,使用第一种方法而不是第二种方法可能是一个更好的主意.
Note that, since inputLine
returns NONE
when the stream is empty, it's probably a better idea to use the first approach, rather than the second.
这篇关于使用ml-lex构建一个词法分析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!