OCaml 解析器代码

标签 ocaml ocamlyacc

我的代码:

Term :
...
| VAR { try Hashtbl.find var_table $1
         with Not_found ->
      printf "no such variable '%s'\n" $1; 0.0 }    /*(Line:75)*/
...

当我运行它时,在 ocamlc -c parser.ml 我明白了:

Error: This expression has type float but an expression was expected of type
         Syntax.term

大家能帮我证明一下这个问题吗? 我知道第 75 行的类型与 Syntax.ml 和 Syntax.mll 中定义的类型 Syntax.term 不匹配,但我想向 Syntax.term 指定 0.0 的类型来证明这一点。我可以做到吗??

--------------------编辑------------------:

术语类型:

type term =
    TmTrue
  | TmFalse
  | TmIf of term * term * term
  | TmAnd of term * term
  | TmOr of term * term
  | TmXor of term * term
  | TmSum of term * term
  | TmSub of term * term
  | TmMult of term * term
  | TmPow of term * term
  | TmZero
  | TmSucc of term
  | TmPred of term
  | TmIsZero of term
  | TmNot of term

一切工作正常,我想在代码中添加赋值,我使用上面的代码将 VAR 添加到 Term 中。我为它创建了哈希表和其他东西,但这部分让我感到困惑......

----------------------/编辑------------------------ -

tnx;)

最佳答案

您没有可以表示值 0.0 的术语。也许你应该使用TmZero

关于OCaml 解析器代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14187054/

相关文章:

functional-programming - 多态型与弱型

graphics - 如何实现函数绘图仪

ocaml - 指定 ocamllex 编码

parsing - 使用 ocamllex/ocamlyacc 解析部分语法

ocaml - ocamllex 正则表达式的外部定义

ocaml - 关于 ocamlyacc,函数应用语法和优先级

merge - 自定义目录/文件夹合并工具

keyboard - OCaml 在没有图形窗口的情况下读取按键

functional-programming - 将我的头环绕在 OCaml 上

ocaml - 从显式 token 列表中输入 ocamlyacc 解析器?