Can anyone help me on how to enable C-style included file in ocamlyacc? For example: #include "mylib.txt";
.
Asked
Active
Viewed 63 times
0

Herry
- 455
- 3
- 14
-
OCaml doesn't have a preprocessor, so there's nothing corresponding to the C `#include` mechanism. Depending on what you were planning to put into `mylib.txt`, your best bet is probably just to use the OCaml module system in the usual way. – Jeffrey Scofield May 01 '14 at 05:04
-
Probably my question is not clear enough. The #include here is not the OCaml codes, but the target source file defined in other language that is going to be parsed by my codes. In C/C++ Yacc, there is a mechanism on handling this i.e. changing the input stream. However, I cannot find any method to do the same thing in ocamlyacc. – Herry May 01 '14 at 13:45
-
yacc is a preprocessor that generates C output. Some of the code in its input is just straight C code that's passed through to the output. In the pass-through C code you can have `#include "abc.h"`, which is then handled by the C preprocessor when you compile the output. I'm not at all sure what you're trying to do. But since OCaml doesn't have a proprocessor, you'll have to use a different scheme to get the same result. – Jeffrey Scofield May 01 '14 at 15:04
-
Thanks Jeffrey for the answer. But using C preprocessor is not a good practice in my case since the compiler should make sure that the included file has legal statements. Anyway, I found an old mail in the OCaml mailing-list that addresses my problem (with some modification): https://groups.google.com/forum/m/#!topic/fa.caml/_v_k4WTQV_Q – Herry May 06 '14 at 00:56
1 Answers
1
The #include
facility is not part of yacc, it's part of C. You can use it with yacc because yacc generates C as its output. Since OCaml doesn't have a preprocessor, you can't do the same with ocamlyacc.
The usual use of this facility with yacc is for sharing token definitions between your scanner and your parser.
With ocamlyacc, the standard thing to do is to define your token symbols in your ocamlyacc input. Then in your scanner, you use the names from your parser module. Concretely speaking, you might open
your parser module in your scanner code.

Jeffrey Scofield
- 65,646
- 2
- 72
- 108