P.C.
P.C.

Reputation: 649

In an OCaml scanner, why does the regex for negative integers not work?

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.

Upvotes: 1

Views: 696

Answers (2)

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66818

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.

Upvotes: 0

sepp2k
sepp2k

Reputation: 370112

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

Upvotes: 4

Related Questions