プログラミング言語SML#解説 3.7.1版
30.5 smllex出力ファイルのインタフェイスファイル記述
smllexは,字句解析生成関数makeLexerを含むストラクチャを生成する. 生成された字句解析プログラムをSML#で使用するためには,その インタフェイスを書く必要がある. 最小のインタフェイスは以下の形である.
_require "basis.smi"
_require "coreML.grm.smi"
structure CoreMLLex =
struct
val makeLexer : (int -> string) -> unit -> CoreML.Tokens.token
end
makeLexerの第一引数は,最大文字数を引数としてとり文字入力 列を返す関数である. この関数を,makeLexer (fn n => TextIO.inputN(TextIO.stdIn,n) のように文字列入力に適用すると,呼び出される毎に字句を返す字句解析関数が生成される. smlyaccによって生成されたmakeStream関数をこの関数に適用すると, 構文解析器が使用するトークンストリーを生成できる.
追加引数等のユーザ宣言はUserDeclarationsストラクチャに定義される. それらを使う字句解析プログラムのインタフェイスファイルは以下の形である.
_require "basis.smi"
_require "<YaccInputFile>.grm.smi"
structure MLLex =
struct
structure UserDeclarations =
struct
type token = <YaccName>.Tokens.token
type pos = <YaccName>.Tokens.pos
type arg (= boxed)
end
val makeLexer
: (int -> string) -> UserDeclarations.arg -> unit -> UserDeclarations.token
end