Multi-pass compilation? (for term expansion)

Is what you’re trying to do any different from how the Logtalk port of ECGs does term-expansion? See:
https://github.com/LogtalkDotOrg/logtalk3/blob/fa051f946a5421671674de88142ece2d3dcd833f/library/edcg/edcg.lgt

Followup.

Just for fun. If you a pl.pl plain Prolog file with an EDCG such as:

:- op(1200, xfx, '-->>').

% Declare accumulators
acc_info(adder, X, In, Out, plus(X,In,Out)).

% Declare predicates using these hidden arguments
pred_info(len,0,[adder,dcg]).
pred_info(increment,0,[adder]).

increment -->>
	[1]:adder.

len(Xs,N) :-
	len(0,N,Xs,[]).

len -->>
	[_],
	!,
	increment,
	len.
len -->>
	[].

You can do:

?- {edcg(loader)}.
...
% (0 warnings)
true.

?- logtalk_load('pl.pl', [hook(edcg)]).
% [ /Users/pmoura/Desktop/pl.pl loaded ]
% (0 warnings)
true.

?- len([a,b,a], Len).
Len = 3.