+
+
+
+//////////////////////////////////////////// Grammar stuff
+
+extern "C" value caml_grammar_load(value file, value load_bp)
+{
+ CAMLparam2(file, load_bp);
+ CAMLlocal1(result);
+ Grammar *grammar;
+ int f1 = Int_val(file);
+ int f2 = dup(f1);
+ FILE * fd = fdopen(f2, "r");
+ if (fd == NULL)
+ CAMLRAISEMSG("Error opening grammar file");
+ grammar = Grammar::load(fd, Bool_val(load_bp));
+ fclose(fd);
+ result = sxsi_alloc_custom<Grammar*>();
+ Obj_val<Grammar*>(result) = grammar;
+ CAMLreturn(result);
+}
+
+extern "C" value caml_grammar_get_symbol_at(value grammar, value symbol, value preorder)
+{
+ CAMLparam3(grammar, symbol, preorder);
+ CAMLreturn(Val_long(GRAMMAR(grammar)->getSymbolAt(Long_val(symbol), Int_val(preorder))));
+
+}
+
+extern "C" value caml_grammar_first_child(value grammar, value rule, value pos)
+{
+ CAMLparam1(grammar);
+ CAMLreturn(Val_int(GRAMMAR(grammar)->firstChild(Long_val(rule), Int_val(pos))));
+}
+
+extern "C" value caml_grammar_next_sibling(value grammar, value rule, value pos)
+{
+ CAMLparam1(grammar);
+ CAMLreturn(Val_int(GRAMMAR(grammar)->nextSibling(Long_val(rule), Int_val(pos))));
+}
+
+extern "C" value caml_grammar_is_nil(value grammar, value rule)
+{
+ CAMLparam1(grammar);
+ CAMLreturn(Val_bool(GRAMMAR(grammar)->isNil(Long_val(rule))));
+}
+
+extern "C" value caml_grammar_get_tag(value grammar, value symbol)
+{
+ CAMLparam1(grammar);
+ CAMLlocal1(res);
+ const char * s = (GRAMMAR(grammar)->getTagName(Long_val(symbol) >> 2)).c_str();
+ res = caml_copy_string(s);
+ CAMLreturn(res);
+}