Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] parsing included files recursively using ocamllex and ocamlyacc
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Benjamin Geer <ben@s...>
Subject: Re: [Caml-list] parsing included files recursively using ocamllex and ocamlyacc
Michal Moskal wrote:
> On Sat, Sep 20, 2003 at 03:38:22PM +0100, Benjamin Geer wrote:
>>I'm writing an interpreter for a small language (to be released as an 
>>open source library), using ocamllex and ocamlyacc.  I'd like this 
>>language to support an instruction that, at compile time, recursively 
>>includes source code from another file.
>>I searched the list archives and found a message 
>>( [snip]
> Wouldn't it work to simply call parsing function from parser rule?

Well, the message mentioned above gave me the impression that it 
wouldn't be quite so easy, apparently becuase the generated parsing 
function isn't defined with "let rec".  So I just wondered: has anyone 
done this?

>>I considered using (or writing) a preprocessor to do the includes, but I 
>>couldn't think of a way to do this without losing the ability to report 
>>filenames and line numbers in run-time error messages.
> Maybe you should consider C-like:
>   # lineno "filename"
> Then you could simply use cpp.

I'd like not to have to depend on cpp, because this interpreter is for a 
specialised scripting language meant to be embedded in Caml programs. 
If those Caml programs had to spawn a process to run cpp before loading 
a script, that would probably be seen as too much overhead.  I also want 
to make things as easy as possible for people who write scripts, so I 
don't want them to have to run cpp themselves.

I just thought of another approach, which wouldn't use recursion in the 
parser.  Each time the parser gets to an 'include filename' statement, 
it can store the filename in a list, and put a the filename in a node in 
the parse tree.  After parsing a file, a function can parse (and cache) 
any files named in that list, and repeat until the list is empty.  When 
the interpreter runs a program and finds a reference to an included 
file, it can get that file's parse tree from the cache.


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: