sahilbansal17
sahilbansal17

Reputation: 46

File Input in ML-Lex

How to take input from a file after running ml-lex filename.lex and then using makeLexer function?

I am trying to do : val lexer = makeLexer(fn n => valOf(inputLine(openIn("test.txt"))));

[I have already done open TextIO; so that I am able to use openIn.]

But, this gives me an error after I run lexer(); :

uncaught exception Io [Io: openIn failed on "test.txt", Too many open files]

raised at: Basis/Implementation/IO/text-io-fn.sml:783.25-783.71

Upvotes: 1

Views: 215

Answers (1)

matt
matt

Reputation: 5634

Not seeing the problem in your posted code, I'm guessing that you are opening multiple files in a loop, and failing to close them,

fun foo _ 0 = ()
  | foo filenm n = let val _ = TextIO.openIn(filenm) in foo filenm (n - 1) end

fun bar _ 0 = ()
  | bar filenm n =
   let val ins = TextIO.openIn(filenm)
       val _ = TextIO.inputLine(ins)
       val _ = TextIO.closeIn(ins)
    in bar filenm (n - 1) end

In the following transcript, the call to bar works with 1024, as it opens then closes each input stream sequentially, however the subsequent call to foo fails with the error, as it hits the platform defined limit on the number of open file descriptors.

val foo = fn : string -> int -> unit
val bar = fn : string -> int -> unit
- bar "toomanyfiles.sml" 1024;
val it = () : unit
- foo "toomanyfiles.sml" 1024;

uncaught exception Io [Io: openIn failed on "toomanyfiles.sml", Too many open files]
  raised at: Basis/Implementation/IO/text-io-fn.sml:783.25-783.71

Upvotes: 2

Related Questions