Version française
Home     About     Download     Resources     Contact us    
Browse thread
re-entrance of ocamlyacc parsers
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <xleroy@p...>
Subject: Re: re-entrance of ocamlyacc parsers
> I would like to be able to call the ocamlyacc-generated code
> recursively from an ocamlyacc semantic action.

> 1) I wonder how I can define a function `compile_file'
>    that ends up calling `yacc_start' recursively.
> 
>    Maybe generating a function `yacc_start' defined with
>    `let rec' instead of `let' would do (as with ocamllex).

The "let rec" would need to enclose not only the entry points, but
also the table of actions yyact, since that's where the recursive
calls occur.

A simple, if not very elegant solution, is to use a reference to a
function, initialized to a dummy function and later changed to
"yacc_start". (Same trick as for cross-module recursion, as described
at the end of the chapter on ocamlc in the manual.) It's definitely
less ugly than your perl script.

> 2) Anyway, I noticed that the ocamlyacc-generated code
>    (see stdlib/parsing.ml) uses a global value `env' (which can
>    be emptied with `clear_parser ()').
> 
>    Does this forbid `yacc_start' re-entrance ?
> 
>    I mean, will the inner call to `yacc_start' corrupt `env' value
>    for the outer call to work properly ?
> 
>    Or can I just see `env' as an implementation issue not to
>    be considered by module `Parsing' users ?

The module Parsing is supposed to be reentrant. Yes, there's some
global state inside, but we're careful to save and restore it across
entries. If you have a program demonstrating non-reentrance, please
send it to me privately and I'll try to fix that.

- Xavier Leroy