递归函数的返回语句中的 Ocaml 类型错误

麦迪

我有一个手写的预测解析器。每个非终结符都有一个对应的解析方法。每个解析器方法的类型都是 tokenlist -> tokenlist * Ast`

在每个方法中,我使用约定“tokenlist_symbol”在使用特定符号后暗示令牌列表。在这一行中:let typ tokenlist_typ = parseTyp tokenlist in match tokenlist.head withtyp 是一个 AST,而 tokenlist_typ 是 parseTyp 消耗了 typ 前缀后的令牌列表的剩余部分。

但是,我收到This expression has type 'a -> token_list * Ast.typ but an expression was expected of type Ast.typ行错误,(Ast.Declaration(typ, identifier, decls_prime), tokenlist_decls_prime)

type token_list = 
  {head : Lexer.token; (** head token. *)
   lexbuf : Lexer.token list}  (** lexer buffer. *)
(** Represents a parser buffer used during parsing of various productions. *)

let default_tokenlist s = {head = Lexer.EOF; lexbuf = Lexer.tokenize s}
(* Create a default [parse_buffer] with the given string [s]. *)

let next tokenlist =
    let {head = _; lexbuf = buf} = tokenlist in
    {head = List.hd buf; lexbuf = List.tl buf}
(** Retrieves a new parser buffer with the next lookahead token. *)

let parseTyp tokenlist = 
    match tokenlist.head with 
    | Lexer.Int -> (next tokenlist, Ast.Int) 
    | Lexer.Bool -> (next tokenlist, Ast.Bool)
    | Lexer.Void -> (next tokenlist, Ast.Void)  
    | Lexer.EOF -> (tokenlist, Ast.Epsilon)
    | _-> let err_msg = "Syntax Error" in
          raise (Syntax_error err_msg)

(*decls = typ “id” decls_prime | epsilon *)
let rec parseDecls tokenlist = 
    let (tokenlist_typ, typ, ) = parseTyp tokenlist in 
        match tokenlist.head with 
        | Lexer.ID identifier -> let (tokenlist_decls_prime, decls_prime) = next tokenlist |> parseDeclsPrime in 
                                (tokenlist_decls_prime, Ast.Declaration(typ, identifier, decls_prime))
        | Lexer.EOF -> (tokenlist, [])
        | _-> let err_msg = Printf.sprintf "Syntax Error" in
              raise (Syntax_error err_msg)

(* decls_prime = vdecl decls | fdecl decls *)
and parseDeclsPrime tokenlist =
    match tokenlist.head with 
    | Lexer.Semicolon -> let tokenlist_vdecl) = next tokenlist in
                let (tokenlist_decls, decls) = parseDecls tokenlist_vdecl in 
                (tokenlist_decls, Ast.DeclsPrime(Lexer.Semicolon, vdecl, decls)) 
    | Lexer.LeftParens -> let (tokenlist_fdecl, fdecl) = next tokenlist |> parseFdecl in 
                let (tokenlist_decls, decls) = parseDecls tokenlist_fdecl in 
                (tokenlist_decls, Ast.DeclsPrime(Lexer.Semicolon, fdecl, decls)) 
    | _-> let err_msg = Printf.sprintf "Syntax Error" in
          raise (Syntax_error err_msg)
杰弗里·斯科菲尔德

你有这个:

let (decls_prime, tokenlist_decls_prime) = 
    next tokenlist |> parseDeclsPrime

从名称来看,这看起来像是parseDeclsPrime返回 type Ast * tokenlist但在我看来,解析函数应该返回tokenlist * Ast

这对中的两个名字很可能是颠倒的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

基本的OCaml函数返回类型错误

来自分类Dev

OCaml 用户定义类型和函数返回错误

来自分类Dev

OCaml 多态递归错误

来自分类Dev

在OCaml中返回类型

来自分类Dev

OCaml类型错误(折叠)

来自分类Dev

OCaml详细类型错误

来自分类Dev

类型错误OCaml

来自分类Dev

递归函数中的OCaml语法错误

来自分类Dev

OCaml错误:“变量类型没有构造函数::”

来自分类Dev

递归函数返回类型错误

来自分类Dev

Ocaml 和返回类型(图论)

来自分类Dev

在OCaml的assert语句中使用<>会导致错误

来自分类Dev

OCaml:在IF语句中返回字符串

来自分类Dev

跨越“模块类型=”的OCaml递归类型

来自分类Dev

Ocaml 错误列出高阶函数

来自分类Dev

带有构造函数的ocaml递归类型记录

来自分类Dev

OCaml中的相互递归类型

来自分类Dev

OCaml中的多态递归对象类型

来自分类Dev

递归模块中的OCaml类型绑定

来自分类Dev

AST 类型可以在 ocaml 中递归吗?

来自分类Dev

OCaml-未绑定类型构造函数

来自分类Dev

OCaml函数类型('a->'b)->'a->'b

来自分类Dev

OCaml:如何确定函数参数的类型?

来自分类Dev

简单的递归函数-OCAML

来自分类Dev

OCaml中的递归函数

来自分类Dev

OCaml的静态类型检查器错误

来自分类Dev

函数中的OCaml语法错误

来自分类Dev

嵌套函数中的Ocaml语法错误

来自分类Dev

嵌套在循环中的 if 语句中的 OCaml 返回值