无法使用沙丘编译MLY文件

发布于 2025-01-29 07:01:39 字数 1081 浏览 3 评论 0原文

我正在写下我的第一个Ocamllex和ocamlyacc程序,通过以下 this tutorial

my dune我的dune files看起来像

(executable
 (public_name Calculator)
 (name main))
(ocamllex lexer)
(ocamlyacc parser)

我的lexer.mll文件,

{
  open Parser
}

rule read = parse
  | eof { EOF }

parser.mly文件是

%token EOF

%start <unit> prog

%%

prog:
  | EOF { () }
  ;

type expr = unit 

(** [parse s] parses string [s] into an AST. *)
let parse (s: string): expr = 
  let lexbuf = Lexing.from_string s in 
  let ast = Parser.prog Lexer.read lexbuf in 
  ast

(** [interp s] interprets [s] by Lexing and parsing it, 
    evaluating it, and converting the result into string *)
let interp (s: string) : string = 
  failwith "unimplemented"

File "bin/parser.mly", line 3: syntax error
%start <unit> prog

我的 它显示了如何解析和空字符串。我对这个错误不太了解,因为它没有告诉我语法错误是什么。我已经完全编写了代码,如视频所示。

I am writing my first Ocamllex and Ocamlyacc program by following this tutorial

My dune file looks like

(executable
 (public_name Calculator)
 (name main))
(ocamllex lexer)
(ocamlyacc parser)

My lexer.mll file is

{
  open Parser
}

rule read = parse
  | eof { EOF }

And parser.mly file is

%token EOF

%start <unit> prog

%%

prog:
  | EOF { () }
  ;

My main.ml file is

type expr = unit 

(** [parse s] parses string [s] into an AST. *)
let parse (s: string): expr = 
  let lexbuf = Lexing.from_string s in 
  let ast = Parser.prog Lexer.read lexbuf in 
  ast

(** [interp s] interprets [s] by Lexing and parsing it, 
    evaluating it, and converting the result into string *)
let interp (s: string) : string = 
  failwith "unimplemented"

When I say dune build I just get an error

File "bin/parser.mly", line 3: syntax error
%start <unit> prog

However the tutorial I am following (linked above) the same code compiles and runs. and it shows how to parse and empty string. I don't know much about this error because its not tell me what the syntax error is. I have written the code exactly as shown in the video.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情释 2025-02-05 07:01:39

我认为OcamlyAcc没有该%start&lt; type&gt;符号语法。您应该使用%type单独指定类型:

%type <unit> prog
%start prog

I think ocamlyacc doesn’t have that %start <type> symbol syntax. You should separately specify the type using %type:

%type <unit> prog
%start prog
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文