我有一个手写的预测解析器。 每个非终结符都有一个对应的解析方法,每个解析器方法的类型为tokenlist-> tokenlist * Ast`
在每种方法中,我使用约定“ tokenlist_symbol”在使用特定符号后表示令牌列表。在这一行:let typ tokenlist_typ = parseTyp tokenlist in match tokenlist.head with
中,typ是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)
答案 0 :(得分:1)
你有这个:
let (decls_prime, tokenlist_decls_prime) =
next tokenlist |> parseDeclsPrime
从名称来看,看起来parseDeclsPrime
返回类型Ast * tokenlist
。但是在我看来,解析函数应该返回tokenlist * Ast
。
该对中的两个名称很可能颠倒了。