0

I am trying to construct a LL1 parse table in ocaml. I'd like for the key to be a Nonterminal, input_symbol tuple. Is this possible?

I know you can do a stack of tuples:

let (k : (string*string) Stack.t) = Stack.create ();;

Thank you in advance!!

maddie
  • 1,854
  • 4
  • 30
  • 66

1 Answers1

2

The key of a hash table in OCaml can have any type that can be compared for equality and can be hashed to an integer. The "vanilla" interface uses the built-in polymorphic comparison to compare for equality, and a built-in polymorphic hash function.

The built-in polymorphic comparison function fails for function types and for cyclic values.

There is also a functorial interface that lets you define your own equality and hash functions. So you can even have a hash table with keys that contain functions if you do a little extra work (assuming you don't expect to compare the functions for equality).

It is not difficult to make a hash table with tuples as keys:

# let my_table = Hashtbl.create 64;;
val my_table : ('_weak1, '_weak2) Hashtbl.t = <abstr>
# Hashtbl.add my_table (1, 2) "one two";;
- : unit = ()
# Hashtbl.add my_table (3, 4) "three four";;
- : unit = ()
# Hashtbl.find my_table (1, 2);;
- : string = "one two"
Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108