ocaml编译器是使用编译器前端模块parse来解析源码还是使用ocamlyacc来解析源码?
Is ocaml compiler use compiler front-end module parse to parse the source code or use ocamlyacc to parse source code?
我尝试阅读ocaml编译源码,使用4.07源码
我是从driver/main.ml
读到的
我注意到它使用 driver/pparse.ml 第 161 行函数 "parse" 来解析 lex buf
(我添加了一行 Printf.printf "pparse.parse" 并且在 make world 之后,我使用 boot/ocamlrun ./ocamlc -nostdlib -I stdlib -c a.ml 来测试它,控制台写 "pparse.parse"):
let parse (type a) (kind : a ast_kind) lexbuf: a =
match kind with
| Structure->Parse.implementation lexbuf
| Signature->Parse.interface lexbuf
我想知道Parse.implementation是函数实现是从这里的编译器前端库:
https://caml.inria.fr/pub/docs/manual-ocaml/parsing.html
或
来自 parseing/parse.ml,第 61 行:
let implementation = wrap Parser.implementation
和 "make world" 之后,ocamlyacc 在 floder 解析时生成 parser.ml
在 "make world" 之后,函数 Parser.implementation 是:
let implementation (lexfun:Lexing.lexbuf -> token) (lexbuf : Lexing.lexbuf) =
(Parsing.yyparse yytables 1 lexfun lexbuf : Parsetree.structure)
我不知道 ocaml 编译器真正使用的是哪个。
我像这样更改函数解析:
let parse (type a) (kind : a ast_kind) lexbuf : a =
match kind with
| Structure ->
Printf.printf "pparse.parse";
let a=Parse.implementation lexbuf in
let writehandle = open_out "/home/wk/prog/LocationTest/parsed" in
let fmt = Format.formatter_of_out_channel writehandle in
Format.fprintf fmt "%a@." Pprintast.structure a ;
close_out writehandle;
和"make world"之后,它在boot/ocamlrun之后工作fine.but ./ocamlc -nostdlib -I stdlib -c a.ml,解析的文件与a.ml,连a.ml也是一个复杂的程序
是不是太复杂了?能帮帮我吗?谢谢
有没有书或其他文档可以教我 ocaml 的编译器?谢谢!
解析器本身由 ocamlyacc(或 OCaml 4.08 中的 menhir)生成为 parsing/parser.ml
来自 parsing/parser.mly
。模块 parsing/parse
包装生成的函数以添加一些解析器错误处理。最后,模块 driver/pparse
是 parsing/parse
之上的另一层,负责设置解析器环境和预处理阶段。
compiler-libs 库然后重新导出编译器的一些内部模块。
有关一些补充信息,请参阅 https://github.com/ocaml/ocaml/blob/trunk/parsing/HACKING.adoc。
我尝试阅读ocaml编译源码,使用4.07源码
我是从driver/main.ml
读到的我注意到它使用 driver/pparse.ml 第 161 行函数 "parse" 来解析 lex buf (我添加了一行 Printf.printf "pparse.parse" 并且在 make world 之后,我使用 boot/ocamlrun ./ocamlc -nostdlib -I stdlib -c a.ml 来测试它,控制台写 "pparse.parse"):
let parse (type a) (kind : a ast_kind) lexbuf: a =
match kind with
| Structure->Parse.implementation lexbuf
| Signature->Parse.interface lexbuf
我想知道Parse.implementation是函数实现是从这里的编译器前端库: https://caml.inria.fr/pub/docs/manual-ocaml/parsing.html
或
来自 parseing/parse.ml,第 61 行:
let implementation = wrap Parser.implementation
和 "make world" 之后,ocamlyacc 在 floder 解析时生成 parser.ml 在 "make world" 之后,函数 Parser.implementation 是:
let implementation (lexfun:Lexing.lexbuf -> token) (lexbuf : Lexing.lexbuf) =
(Parsing.yyparse yytables 1 lexfun lexbuf : Parsetree.structure)
我不知道 ocaml 编译器真正使用的是哪个。
我像这样更改函数解析:
let parse (type a) (kind : a ast_kind) lexbuf : a =
match kind with
| Structure ->
Printf.printf "pparse.parse";
let a=Parse.implementation lexbuf in
let writehandle = open_out "/home/wk/prog/LocationTest/parsed" in
let fmt = Format.formatter_of_out_channel writehandle in
Format.fprintf fmt "%a@." Pprintast.structure a ;
close_out writehandle;
和"make world"之后,它在boot/ocamlrun之后工作fine.but ./ocamlc -nostdlib -I stdlib -c a.ml,解析的文件与a.ml,连a.ml也是一个复杂的程序
是不是太复杂了?能帮帮我吗?谢谢
有没有书或其他文档可以教我 ocaml 的编译器?谢谢!
解析器本身由 ocamlyacc(或 OCaml 4.08 中的 menhir)生成为 parsing/parser.ml
来自 parsing/parser.mly
。模块 parsing/parse
包装生成的函数以添加一些解析器错误处理。最后,模块 driver/pparse
是 parsing/parse
之上的另一层,负责设置解析器环境和预处理阶段。
compiler-libs 库然后重新导出编译器的一些内部模块。
有关一些补充信息,请参阅 https://github.com/ocaml/ocaml/blob/trunk/parsing/HACKING.adoc。