How does one map \100 to d?

How does one map \100 to d?

100 is the ascii code for the character ‘d’. Your question doesn’t make sense without context and without proper quoting. Please polish it up and be mindful of people’s time.

2 Likes

quesiton doesn’t let me edit it, so will respond here.

I meant the string “\100” to the character d. I have a lexer that returns the string “\100” and I want to transform it to the character d.

It’s really hard to help without seeing some code. But to translate a string to a decimal number (if the string has the right form) you can use int_of_string. To translate an integer character code into the character that it represents you can use Char.chr.

Im looking for a lexer for this:

# get_all_tokens "\" \\100 \\001 \"";;
- : Common.token list = [STRING " d \001 "]

my really lame attempt:

rule token = parse
  | [' ' '\t'] { token lexbuf }  (* skip over whitespace *)
  | ['\n']     { token lexbuf }  (* skip over whitespace *)
...
  | numeric+ as s         { INT (int_of_string s) }
  | ("0x"(hexadec)+) as s { INT (int_of_string s) }
  | ((numeric+)'.'(numeric*)('e'(numeric)+)?) as s       { FLOAT (float_of_string s) }
(* your rules go here *)

  | "+" { PLUS }
  | ">" { GT }
  | "]" { RBRAC }
  | "rec" { REC }
  | "fun" { FUN }
  | "raise" { RAISE }

  | "0b" ['0' - '1']+ as s { INT (int_of_string s)  }

  | lowercase (alphanum | "\'" | "_")* as s { IDENT s }

  (* | "\\" as c { print_char c; print_string "\n"; print_string (String.make 1 c); print_string "\n";STRING (String.make 1 c) } *)
  (* | "\"" { STRING "DEBUG" }
  (* | "\\001"|"\\100" as s { STRING s } *)
  | "\"" as c { let s=(String.make 1 c) in print_string ("RULE \" matched "^s^"\n");get_str s lexbuf } *)

(* and get_str accumulator:string = parse *)
and get_str accumulator = parse
  | "\"" as c { let s=(String.make 1 c) in STRING (accumulator^s) }
  (* | "\\\"" as s { get_str (accumulator^s) lexbuf } *)
  (* | "\\"ddd as s {  print_string (s^"\n");get_str (accumulator^s) lexbuf } *)
  (* | "\\" as c { let s=String.make 1 c in get_str (accumulator^s) lexbuf } *)
  | '\\' { print_string "HERE\n";get_stuff_after_backslash accumulator lexbuf }
  | (alphanum|whitespace|",")* as s { print_string ("RULE alphanum space , matched ("^s^")abc\n");get_str (accumulator^s) lexbuf }

and get_stuff_after_backslash accumulator = parse
  | ddd as s {
      let n=int_of_string s in
      let c=char_of_int n in
      let s=String.make 1 c in
      get_str (accumulator^s) lexbuf
    }
  (* | "\"" as s { let s=(String.make 1 c) get_str (accumulator^"\\"^s) lexbuf } *)

ok I think this works:

  | "0b" ['0' - '1']+ as s { INT (int_of_string s)  }

  | lowercase (alphanum | "\'" | "_")* as s { IDENT s }

  (* | "\\" as c { print_char c; print_string "\n"; print_string (String.make 1 c); print_string "\n";STRING (String.make 1 c) } *)
  (* | "\"" { STRING "DEBUG" } *)
  (* | "\\001"|"\\100" as s { STRING s } *)
  | "\"" as c { let s=(String.make 1 c) in print_string ("RULE \" matched "^s^"\n");get_str s lexbuf }

(* and get_str accumulator:string = parse *)
and get_str accumulator = parse
  | "\"" as c { let s=(String.make 1 c) in STRING (accumulator^s) }
  (* | "\\\"" as s { get_str (accumulator^s) lexbuf } *)
  (* | "\\"ddd as s {  print_string (s^"\n");get_str (accumulator^s) lexbuf } *)
  (* | "\\" as c { let s=String.make 1 c in get_str (accumulator^s) lexbuf } *)
  | '\\' { print_string "HERE\n";get_stuff_after_backslash accumulator lexbuf }
  | (alphanum|whitespace|",")* as s { print_string ("RULE alphanum space , matched ("^s^")abc\n");get_str (accumulator^s) lexbuf }