Building a lexical analyzer using ml-lex

I need to create a new instance lexerbound to standard input streams.
However, when I print

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:

( makeLexeris the function name present in my source code)

+3
source share
2 answers

inputLine returns string option, and I assume that is expected string.

What you want to do, or has makeLexerto take string option, for example:

fun makeLexer  NONE    = <whatever you want to do when stream is empty>
  | makeLexer (SOME s) = <the normal body makeLexer, working on the string s>

or change your line to:

val lexer = makeLexer( fn n => valOf ( inputLine( stdIn ) ) );

valOf takes the type of option and unpacks it.

, inputLine NONE, , , , .

+3

, , . 38 ( 32 ) ML-Lex ML- Yacc

inputLine. , , , inputLine NONE stdIn , CTRL-D.

val lexer =
let 
  fun input f =
      case TextIO.inputLine f of
        SOME s => s
      | NONE => raise Fail "Implement proper error handling."
in 
  Mlex.makeLexer (fn (n:int) => input TextIO.stdIn)
end

. 40 (34 ) ,

, .

+2

Source: https://habr.com/ru/post/1787684/


All Articles