【问题标题】:Ocamlyacc seemingly not returning a full recordOcamlyacc 似乎没有返回完整记录
【发布时间】:2018-05-16 00:01:10
【问题描述】:

我有以下解析器,它应该返回带有globalVarsglobalFns 的记录,但它似乎没有。

%start program
%type <Ast.program> program

%%

program:
    decls EOF { $1 }

decls:
      /* nothing */             { { globalVars = []; globalFns = []; } }
    | decls varDecl             { { $1 with globalVars  = $1::$2.globalVars  } }
    | decls fnDecl              { { $1 with globalFns   = $1::$2.globalFns  } }

ast.ml 将程序定义为:

type program = {
    globalVars  : bind list;
    globalFns   : func_decl list;
}

当我尝试执行以下操作时,我收到的错误是:Error: Unbound record field globalVars

let translate program =
    let global_vars =
        let global_var m (t, n) =
            let init = L.const_int (ltype_of_typ t) 0
            in StringMap.add n (L.define_global n init the_module) m in
            List.fold_left global_var StringMap.empty program.globalVars in

我根本无法弄清楚为什么program.globalVars 在这里不受约束。如果有人能指出我正确的方向,那将不胜感激。

【问题讨论】:

    标签: ocaml ocamlyacc


    【解决方案1】:

    在当前范围内未绑定的是记录字段 globalVars,而不是真正的 program.globalVars 本身:字段标签位于定义它们的模块中。为了访问在当前模块之外定义的类型的记录字段;需要使用完全限定的字段路径,program.Ast.globalVars,其中Ast.globalVars 是模块globalVars 中定义的字段的路径Ast 或将字段纳入范围,例如打开模块或注释程序类型:let translate (program:Ast.program)= ….

    【讨论】:

    • 天哪,我爱你@octachron
    猜你喜欢
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多