1

I have the following ocamllex code:

let flt = ['-' '+']?['0'-'9']+ ['.'] ['0'-'9']+
rule token = parse
  [' ' '\t' '\r' '\n']      { token lexbuf } (* Whitespace *)
| ['0'-'9']+ as lxm         { INTEGER(int_of_string lxm) }
| flt as lxm    { FLOAT(float_of_string lxm) }

This works!

But the minute I want to allow + and - signs for the INTEGER, it gives me an error.

let flt = ['-' '+']?['0'-'9']+ ['.'] ['0'-'9']+
rule token = parse
  [' ' '\t' '\r' '\n']      { token lexbuf } (* Whitespace *)
| ['+' '-']['0'-'9']+ as lxm        { INTEGER(int_of_string lxm) }
| flt as lxm    { FLOAT(float_of_string lxm) }

The error is as follows:

Fatal error: exception Failure("int_of_string")
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64

The funny thing is that in my .ml file, I am using "float_of_string", but I am NOT using "int_of_string" anywhere.

P.C.
  • 651
  • 13
  • 30

2 Answers2

4

int_of_string does not handle leading + signs, so you have to take that out before you pass your string to int_of_string.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • int_of_string doesn't and float_of_string does? – P.C. Aug 08 '14 at 21:27
  • 1
    @P.C. I had to double check this right now because it surprised me as well, but yes: `float_of_string` does handle leading plusses while `int_of_string` does not. – sepp2k Aug 08 '14 at 21:36
0

The regex is working fine, it's your call to int_of_string that is failing. You are using int_of_string. Look at the fourth line of either chunk of code.

sepp2k explains why you get the exception.

I don't see how you can run your program without a main, so I can't explain the undefined symbol error.

You might want to make the leading +/- optional for your integers. In your definition one of them is required.

Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108